How to insert external global variable declarations and pointer assignment statements through GCC plugin GIMPLE pass

Abhijit Nandy abhijit.nandy@gmail.com
Tue Jun 14 17:29:00 GMT 2011


Hi Ian,

Thanks so much for the pointers. I was able to insert most of the lines 
except for the global extern declaration which is the following :

extern char _binary_ccu_start;


My current plugin code is as follows :

void insert_elfset_call()
{
    basic_block bb = ENTRY_BLOCK_PTR->next_bb;
    gimple_stmt_iterator gsi = gsi_start_bb(bb);

    tree temp = create_tmp_var(TREE_TYPE(TREE_TYPE(elfset_decl)), "p");

    tree var_decl = build_decl(UNKNOWN_LOCATION, VAR_DECL, 
get_identifier("_binary_ccu_start"), char_type_node);
    TREE_STATIC(var_decl) = 1;
    DECL_EXTERNAL(var_decl) = 1;                            //this does not 
cause var_decl to also be declared as extern globally
    rest_of_decl_compilation (var_decl, 1, 0);

    tree addr_var_decl = build_fold_addr_expr(var_decl);

    gimple assign = gimple_build_assign_stat (temp, addr_var_decl);
    gsi_insert_before(&gsi, assign, GSI_SAME_STMT);

    //Build a function call from the compiled molen_elfset() code first : 
function has unique attribute marked with __attribute__((user("replace"))) 
which helps pick it using elfset_decl
    gimple elfcall = gimple_build_call(elfset_decl,  1, temp);

    //gsi_insert_after (&gsi, elfcall, GSI_CONTINUE_LINKING);//can be used 
to insert the call after 1st statement, still needs function call graph edge
    gsi_insert_before(&gsi, elfcall, GSI_SAME_STMT);

    cgraph_create_edge(cgraph_node(current_function_decl),
            cgraph_node(elfset_decl), elfcall, bb->count,
            compute_call_stmt_bb_frequency(current_function_decl, bb), 
bb->loop_depth);
}


I get the following output from it :

;; Function main (main)

main ()
{
  char * p.1;

<bb 2>:
.....
  p.1 = &_binary_ccu_start;
  molen_elfset (p.1);
....
}
------------------------------------------------------------------------------------

To recall I am trying to insert the following in a C program :

extern char _binary_ccu_start;

int main (int argc, char **argv)
{
....
    char *p = &_binary_ccu_start;
    molen_elfset(p);
...
}


Thanks,
Abhi


-----Original Message----- 
From: Ian Lance Taylor
Sent: Tuesday, May 31, 2011 4:36 PM
To: Abhijit Nandy
Cc: gcc
Subject: Re: How to insert external global variable declarations and pointer 
assignment statements through GCC plugin GIMPLE pass

"Abhijit Nandy" <abhijit.nandy@gmail.com> writes:

> Thanks for the pointers. I have been trying to make a simple VAR_DECL
> using the following call :
>
> tree type_node = make_node(POINTER_TYPE);
> //TYPE_NAME(type_node) = type_id;
> tree var_decl = build0(VAR_DECL, type_node);

Use build_decl.

> testcode.c:33:1: internal compiler error: Segmentation fault

When you get a segmentation fault, your first step should be to use the
debugger to find out what went wrong.  That will normally point you in
the right direction.

Ian 



More information about the Gcc-help mailing list