On Friday, 3 January 2014 at 18:14:58 UTC, Mike wrote:
I ran into a problem recently that resulted in a segmentation
fault in my program whenever I called a member function of one
of my classes. Sometimes it occurred and sometimes it didn't
depending on the order of certain things in my code.
I eventually tracked it down to the fact that I was compiling
with -ffunction-sections and -fdata-sections and linking with
--gc-sections and symbols like...
.data._D38TypeInfo_E14TypeInfo_Class10ClassFlags6__initZ
.data._D40TypeInfo_E15TypeInfo_Struct11StructFlags6__initZ
... were being discarded. I'm assuming this is the mangled
.init values of these types, yes?
My linker script contained...
.data : AT (__data_rom_begin)
{
. = ALIGN(4);
__data_ram_begin = .;
. = ALIGN(4);
*(.data)
*(.data*)
. = ALIGN(4);
__data_ram_end = .;
} >SRAM
... so I was forced to conclude that the reason they were being
discarded was because it couldn't find any code that was
reaching these symbols.
After adding...
KEEP(*(.data.*init*))
... to my linker script, the problem was resolved.
I'm guessing these are generated implicitly by the GDC
compiler, but it does appear that my code never reaches these
symbols, so discarding them should be OK. However, it seems
discarding them causes dislocation in memory.
I'm still a novice with GCC-based toolchains, so forgive the
ignorance of this question, but is this to be expected, or is
this an indication of a problem with the compiler?
Mike
Compiler:
Latest GDC 4.8 compiled for arm-none-eabi (ARM Cortex-M)
Again, I am guessing a little, but...
In dmd and ides it is common to compile and link everything at
once. the compiler has all information available and may remove
unused code and data.
The gcc system is made for separate compilation. When compiling a
file, the compiler has no idea how other files call functions and
objects in this file. So there has to be at least the default set
of resources. If they are used is known only at linking phase. I
do not know if the linker is able to remove unused code or data
and what flags are needed.
Because tha data is referenced from other files, there has to be
a common naming system. Maybe it would be possible to use named
variables but for some reason they have decided to name a
separate section for every piece of info. Every class, struct etc
will have its own sections and there will be lots of them. I have
just included all of them without thinking.
It may also be possible that the code or data is in use. In asm
file there is a table of data after each funtion. The code may
get a word from the table. This may be a pointer to another table
in another function in another module. There may be an offset to
another place in the table and there may be a pointer to this
strange section. Without looking the whole program in debugger it
is impossible to say whether the code and data are actually used
or not.