Given the following, static char const rcsid[] = "$Id: f.c,v 5.4 1993/11/09 17:40:15 eggert Exp $"; int main() {}
When compiled with GCC 3.4.3, at -O2, the ident string above will _not_ appear in the executable. This is apparently expected behavior. However, interestingly, gcc -fkeep-static-consts -O2 t.c did not retain the ident string, rcsid, defined above. Shouldn't -fkepp-static-consts have ensured that this static constant would appear in the executable? I also tried adding a section attribute to the string, with the hope that the compiler would retain the static constant because it had been explicitly targeted to a named section, static char const __attribute__ ((section("ident_sect"))) rcsid[] = "$Id: f.c,v 5.4 1993/11/09 17:40:15 eggert Exp $"; int main() {} but this didn't prevent the elimination of the const static defintion. Any suggestions on another method to ensure that this static const string makes it into the executable when compiled at -O2? And shouldn't -fkeep-static-consts have ensured that the static const string wasn't eliminated? Bug? The logic in wrapup_global_declarations (toplev.c) doesn't look quite right: else if (TREE_READONLY (decl) && !TREE_PUBLIC (decl) && (optimize || !flag_keep_static_consts || DECL_ARTIFICIAL (decl))) needed = 0; If 'optimize' is asserted above then flag_keep_static_consts will not be tested. Perhaps it should read as follows? && ((optimize && !flag_keep_static_consts) Alternatively, I wonder if flag_keep_static_consts should be tested earlier at a higher level, for example: if (flag_keep_static_consts) /* needed */; but I'm not sure about which of the earlier tests which assert needed = 0; are mandatory and which are optional. Enhancement request: assert node->needed if an explicit section attribute is supplied for the declaration associated with node, on the assumption that the data is being placed in a named section for a reason.