On 2007-03-27, Grant Edwards <[email protected]> wrote:
> When I use the -fdata-sections option, the compiler crashes
> whenever it sees a global initialized variable. Any ideas on
> how to fix this?
OK, I've re-written unique_section() in msp430.c.
Now -fdata-sections works correctly with initialized variables
(and they do get garbage collected properly by the linker).
However, I can't figure out how to get uninitialized variables
to work right. Here's the new unique_section() code:
/* Sets section name for declaration DECL */
void
unique_section (decl, reloc)
tree decl;
int reloc ATTRIBUTE_UNUSED;
{
int len;
const char *name, *prefix;
char *string;
name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
STRIP_NAME_ENCODING (name, name);
if (TREE_CODE (decl) == FUNCTION_DECL)
prefix = ".text.";
else if (DECL_READONLY_SECTION (decl, 0))
prefix = ".text.";
else if (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node)
prefix = ".bss.";
else
prefix = ".data.";
len = strlen (name) + strlen (prefix);
string = alloca (len + 1);
sprintf (string, "%s%s", prefix, name);
DECL_SECTION_NAME (decl) = build_string (len, string);
}
I've verified that the ".bss." path is taken for uninitialized
variables, but it just doesn't work the same as the other three
paths. The above code makes -fdata-sections work correctly for
uinitialized variables (both const and regular), but
uninitialized ones are still in a single .bss section.
How do I get gcc to pay attention to the DECL_SECTION_NAME
attribute of uninitialized variables?
On a slightly different topic, can anybody explain what the old
code was supposed to be doing?
2014 /* Sets section name for declaration DECL */
2015 void
2016 unique_section (decl, reloc)
2017 tree decl;
2018 int reloc ATTRIBUTE_UNUSED;
2019 {
2020 int len;
2021 const char *name, *prefix;
2022 char *string;
2023 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2024 /* Strip off any encoding in name. */
2025 STRIP_NAME_ENCODING (name, name);
2026
2027 if (TREE_CODE (decl) == FUNCTION_DECL)
2028 {
2029 if (flag_function_sections)
2030 prefix = ".text.";
2031 else
2032 prefix = ".text";
2033 }
2034 else
2035 abort ();
2036
2037 if (flag_function_sections)
2038 {
2039 len = strlen (name) + strlen (prefix);
2040 string = alloca (len + 1);
2041 sprintf (string, "%s%s", prefix, name);
2042 DECL_SECTION_NAME (decl) = build_string (len, string);
2043 }
2044 }
1) Why the check for flag_function_sections at line 2029? If
it's not set, the unique_section() function won't get called
for functions. IOW, the if () condition at 2029 has to be
true.
2) Why the abort()? Why does it refuse to handle anything
other than a function?
3) Why the check of flag_function_sections again at 2037?
There's no way to get to that line unless
flag_function_sections is true.
--
Grant Edwards grante Yow! I'm DESPONDENT... I
at hope there's something
visi.com DEEP-FRIED under this
miniature DOMED STADIUM...