GNU C Compiler Internals/GCC Hacks

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Function overloading as in C++[edit | edit source]

This example was presented previously.

toString() method for each structure as in Java[edit | edit source]

Invoke a block of code from a function as in Ruby[edit | edit source]

Linux implementation of lists allows to invoke a block of code on each element of the list:

pagelist.c:

       list_for_each_prev(pos, head) {
                struct nfs_page *p = nfs_list_entry(pos);
                if (page_index(p->wb_page) < pg_idx)
                        break;
       }

list_for_each_prev takes the code in the brackets as a parameter. The trick is to use a macro that rolls out to a for() loop whose body becomes the code in the brackets. The goal of this project is to allow programmers to use code blocks in function calls.

Dereference function results when a structure is returned[edit | edit source]

C allows one to dereference the return value of a function if it is a pointer to a structure:

 get_struct()->field=0;

If the function returns the structure, not a pointer to it, then a compile-time error is generated:

 get_struct().field=0;
 > request for member `field' in something not a structure or union

This extension addresses the problem of dereferencing structures that are return values.

Use functions to initialize a variable[edit | edit source]

When a variable is defined and initialized the initializers is constant. You will get an error if you try to use a function, no matter what this function is:

 int getint() { return 1; }
 int i=getint();
 > initializer element is not constant

When a variable is used the function it was initialized with is called.

Default values of function arguments as in C++[edit | edit source]

 void func(int a=0) {
   printf("a=%d\n", a);
 }
 int main() {
   func();
 }
 > syntax error before '=' token

Reference parameters as in C++[edit | edit source]

 void test(int &a, int &b);
 int x,y;
 test(x,y);

Return address protection (RAD)[edit | edit source]

Rad

Repair of control-hijacking attacks (DIRA)[edit | edit source]

Dira

Array bounds checking using segmentation hardware (CASH)[edit | edit source]

Cash

Detecting interger overflows (DIVINE)[edit | edit source]

Divine

Develop in userland, install in kernel (DUSK)[edit | edit source]

Dusk

Dynamic Information Flow Tracking (GDIF)[edit | edit source]

Gdif

GCC switches in object file[edit | edit source]

GCC switches in object file

The -fstack-protector feature[edit | edit source]

GCC 4.1 implements a protection mechanism against buffer overflow attacks and exposes it to a programmer with a -fstack-protector option. This is an example of functionality that benefits from modular implementation because it has nothing to do with core functionality of the compiler. Does GCC have enough hooks to implement -fstack-protector module?