Skip to content

Split text and data sections if possible#723

Closed
Kijewski wants to merge 3 commits intoRIOT-OS:masterfrom
Kijewski:fdata-sections
Closed

Split text and data sections if possible#723
Kijewski wants to merge 3 commits intoRIOT-OS:masterfrom
Kijewski:fdata-sections

Conversation

@Kijewski
Copy link
Copy Markdown
Contributor

This patch adds -ffunction-sections and -fdata-sections to the
CFLAGS if the compiler understands these flags (at least clang and GCC
do).

These flags cause every function and every static variable to go into
a section of its own in the object file. The static linker is then able
to drop functions/variables if they are not used.

The results are quite impressive ("default", BOARD=native, CFLAGS=-Os):

With patch:
   text    data     bss     dec     hex filename
  36686     552  108520  145758   2395e …/default.elf

Without patch:
   text    data     bss     dec     hex filename
  43944     580  109544  154068   259d4 …/default.elf

In #664 I added a test that determines if the supplied compiler
understands the `-fno-delete-null-pointer-checks` flag. The problem is
that the `$(CC)` supplied on command line or in the application's
Makefile is used, but not the one the `$(BOARD)`'s Makefile sets.

That problem was overlooked as all the boards use GCC, and GCC happens
to know the flag. But if some future board does not use GCC, then the
wrong order of the checks could pose a problem.
This patch adds `-ffunction-sections` and `-fdata-sections` to the
`CFLAGS` if the compiler understands these flags (at least clang and GCC
do).

These flags cause every function and every static variable to go into
a section of its own in the object file. The static linker is then able
to drop functions/variables if they are not used.

The results are quite impressive ("default", BOARD=native, CFLAGS=-Os):
```
With patch:
   text    data     bss     dec     hex filename
  36686     552  108520  145758   2395e …/default.elf

Without patch:
   text    data     bss     dec     hex filename
  43944     580  109544  154068   259d4 …/default.elf
```
@Kijewski
Copy link
Copy Markdown
Contributor Author

The PR is based on #722.

@OlegHahm
Copy link
Copy Markdown
Member

I'm not sure if this is gonna work on ARM7. With patch:

   text    data     bss     dec     hex filename
      4       0   98304   98308   18004 examples/default/bin/msba2/default.elf

Without:

   text    data     bss     dec     hex filename
 115036    2768   93805  211609   33a99 examples/default/bin/msba2/default.elf

@OlegHahm
Copy link
Copy Markdown
Member

--print-gc-sections shows some over-motivated garbage collecting by the ARM linker:
E.g.:

/usr/lib/gcc/arm-none-eabi/4.8.2/../../../../arm-none-eabi/bin/ld: Removing unused section '.text.thread_getpid' in file '/home/oleg/git/RIOT/examples/default/bin/msba2/core.a(thread.o)'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../../arm-none-eabi/bin/ld: Removing unused section '.text.thread_getlastpid' in file '/home/oleg/git/RIOT/examples/default/bin/msba2/core.a(thread.o)'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../../arm-none-eabi/bin/ld: Removing unused section '.text.thread_getstatus' in file '/home/oleg/git/RIOT/examples/default/bin/msba2/core.a(thread.o)'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../../arm-none-eabi/bin/ld: Removing unused section '.text.thread_getname' in file '/home/oleg/git/RIOT/examples/default/bin/msba2/core.a(thread.o)'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../../arm-none-eabi/bin/ld: Removing unused section '.text.thread_sleep' in file '/home/oleg/git/RIOT/examples/default/bin/msba2/core.a(thread.o)'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../../arm-none-eabi/bin/ld: Removing unused section '.text.thread_wakeup' in file '/home/oleg/git/RIOT/examples/default/bin/msba2/core.a(thread.o)'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../../arm-none-eabi/bin/ld: Removing unused section '.text.thread_measure_stack_free' in file '/home/oleg/git/RIOT/examples/default/bin/msba2/core.a(thread.o)'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../../arm-none-eabi/bin/ld: Removing unused section '.text.thread_create' in file '/home/oleg/git/RIOT/examples/default/bin/msba2/core.a(thread.o)'

@OlegHahm
Copy link
Copy Markdown
Member

Maybe https://github.com/Kijewski/RIOT/pull/2 helps but I'm not so sure if this is enough.

@Kijewski
Copy link
Copy Markdown
Contributor Author

Hm, the linker scripts need to be fixed to enabled gc-sections. The problem is only not only that the entry point needs to be specified, but also that the entry code has to be proper.
For the linker script of the msba2 the Reset_Handler spans over multiple sections, none of them called .text. :) So the linker script assumes _start to be the entry function, sees _start jumping into Reset_Handler, which in turn appears to be empty, because right after the symbol a new section starts. That explains the .text size of 4 bytes: just one jump. Everything else was determined to be dead.

I think I have fixed the startup code for msba2, but I cannot test it.

With patch:
   text    data     bss     dec     hex filename
 118320    1876   94689  214885   34765 …/default.elf
Without:
   text    data     bss     dec     hex filename
 124512    1896   94677  221085   35f9d …/default.elf

The scraped functions look about right.

@OlegHahm
Copy link
Copy Markdown
Member

I cannot find Reset_Handler in the linkerscript of the lpc2387.

@Kijewski
Copy link
Copy Markdown
Contributor Author

Reset_Handler is defined in the startup.s of the lpc2387: https://github.com/Kijewski/RIOT/blob/fdata-sections/cpu/lpc2387/startup.s#L77

@haukepetersen
Copy link
Copy Markdown
Contributor

I think this is a very good idea, but the linkerflags should be set inside the boards Makefile instead of the global Makefile.include - because for this optimization to work the linker/startupscripts for each platform needs to be adjusted...

@Kijewski
Copy link
Copy Markdown
Contributor Author

I will test this on a per module board basis.
@haukepetersen is right, it's no use to add this flag if it is far more likely to break things than not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants