How to use --gc-sections?
Grant Edwards
grante@visi.com
Mon Dec 29 21:06:00 GMT 2003
I'm stumped: I can't figure out how to discard unused functions
using the -ffunction-sections and --gc-sections options to gcc
and ld. I know I've gotten it to work in the past with an ARM
target, but now I can't seem to get it to work with an H8
target.
Here's a small example program that illustrates my problem.
After I compile and link test.c, the function named
"functionWeDontUse" should be discarded, but it isn't.
------------------------------test.c------------------------------
int functionWeUse(int i)
{
return 0x3456 * i;
}
int functionWeDontUse(int i)
{
return 0x1234 * i;
}
int start(void)
{
return functionWeUse(75);
}
------------------------------------------------------------------
------------------------------makefile----------------------------
GCC = h8300-elf-gcc -mh
GLD = h8300-elf-ld
LIBGCC = $(shell $(GCC) -print-libgcc-file-name)
test.elf : test.o test.ld makefile
$(GLD) -static --gc-sections -Map test.map \
-T test.ld -o $@ \
test.o $(LIBGCC)
%.o: %.c
$(GCC) -Wall -g -O2 \
-ffunction-sections -fdata-sections \
-c $< -o $@
clean:
rm -f *.elf *.mot *.map *.o *.o *~
------------------------------------------------------------------
------------------------------test.ld------------------------------
OUTPUT_ARCH(h8300h)
ENTRY(_start)
MEMORY
{
rom (r) : o = 0x200000, l = 128k
ram (rw) : o = 0x220000, l = 120k
}
SECTIONS
{
.text 0x200100 :
{
*(.text.*)
etext = .;
} > rom
.rodata :
{
*(.rodata)
_erodata = .;
_mdata = .;
} > rom
.data : AT (_mdata)
{
_data = .;
*(.data)
_edata = .;
} > ram
.bss :
{
_bss = . ;
*(.bss)
*(COMMON)
_ebss = . ;
_end = . ;
} > ram
}
-------------------------------------------------------------------
$ h8300-elf-gcc -v
Reading specs from /opt/gnu/lib/gcc-lib/h8300-elf/3.3-GNUSH_v0304/specs
Configured with: /home/grante/toolchain/src/gcc-3.3.1-kpit3.04/configure --target=h8300-elf --prefix=/opt/gnu --enable-languages=c --with-gnu-as --with-gnu-ld --with-newlib -v
Thread model: single
gcc version 3.3-GNUSH_v0304
$ h8300-elf-ld -v
GNU ld version GNUSH_v0304 20030612
$ make
h8300-elf-gcc -mh -Wall -g -O2 \
-ffunction-sections -fdata-sections \
-c test.c -o test.o
h8300-elf-ld -static --gc-sections -Map test.map \
-T test.ld -o test.elf \
test.o /opt/gnu/lib/gcc-lib/h8300-elf/3.3-GNUSH_v0304/h8300h/libgcc.a
$ file test.elf
test.elf: ELF 32-bit MSB executable, Hitachi H8/300, version 1 (SYSV), statically linked, not stripped
$ nm -a test.elf | grep Dont
00200114 T _functionWeDontUse
$ head -25 test.map
Memory Configuration
Name Origin Length Attributes
rom 0x00200000 0x00020000 r
ram 0x00220000 0x0001e000 rw
*default* 0x00000000 0xffffffff
Linker script and memory map
.text 0x00200100 0x3c
*(.text.*)
.text.functionWeUse
0x00200100 0x14 test.o
0x00200100 _functionWeUse
.text.functionWeDontUse
0x00200114 0x14 test.o
0x00200114 _functionWeDontUse
.text.start 0x00200128 0x14 test.o
0x00200128 _start
0x0020013c etext = .
.rodata 0x0020013c 0x0
*(.rodata)
--
Grant Edwards
grante@visi.com
------
Want more information? See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sources.redhat.com
More information about the crossgcc
mailing list