This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Creating a VAR_DECL in a named section.


Hi all,

I have been trying to place some data into a named section of a .o
file. I can do it currently by hooking into various of the RTL to
assembly routines and emitting the asm code directly, however I am now
trying to do it from within the C++ front end by inserting a VAR_DECL
node and setting it as belonging into a named section.

I have some code that looks like:


void bjc_add_var_decl(tree node)
{
   tree identifier;
   tree section_name;
   tree var_decl;
   tree var_decl_type;
   size_t data_len;
   const char* data = "Some data to save to the named section.";
   const char* id_name = "BJC_SOME_ID";
   tree init;


   identifier = get_identifier(id_name);
   data_len = strlen(data);
   var_decl_type = build_array_type(char_type_node,
      build_index_type(size_int(data_len)));
   var_decl_type = c_build_qualified_type(var_decl_type,
      TYPE_QUAL_CONST);

   var_decl = build_decl(VAR_DECL, identifier, var_decl_type);

   TREE_STATIC(var_decl) = 1;
   TREE_READONLY(var_decl) = 1;
   DECL_ARTIFICIAL(var_decl) = 1;


   init = build_string(data_len + 1, data);
   TREE_TYPE(init) = var_decl_type;
   DECL_INITIAL(var_decl) = init;
   TREE_USED(var_decl) = 1;

   section_name = build_string(strlen(".edoc"), ".edoc");
   DECL_SECTION_NAME(var_decl) = section_name;

   LogFine("Need to attach it somewhere in the tree.");
   bind(identifier, var_decl, node, false, false);

   finish_decl(var_decl, init, NULL_TREE);
   pushdecl(var_decl);
}

I initially had it without the pushdecl() call and thought that the
bind call would bind the new var_decl to the node passed into the
function.

I got most of the code above from looking at a few different places.
One was some documentation for GEM, and another was from the code used
in creating var decls for the nodes created when encountering
__FUNCTION__ in the source.


I have tried calling this method using a few different nodes, one was
the global namespace node of type NAMESPACE_DECL just after
finish_translation_unit() (I think that was the entry point), and
another just after a call to finish function with the FUNCTION_DECL node.

In all situations there has been nothing emitted in the resulting
assembly source code.

Up until now my hacking of GCC has only been reading values from the
tree. I have not tried generating nodes and inserting them into the
tree so this is really a first for me.

Any ideas what I am doing wrong?

Thanks for any help.
Brendon.






Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]