Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linker: sort sections by alignment #14183

Merged
merged 2 commits into from
Mar 13, 2019

Conversation

dcpleung
Copy link
Member

@dcpleung dcpleung commented Mar 8, 2019

This turns on the linker flag to sort sections by alignment in decreasing size of symbols. This helps to minimize padding between symbols.

This also adds the linker options to sort common symbols by alignment in descending to minimize the need for padding.

@codecov-io
Copy link

codecov-io commented Mar 8, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@017446b). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##             master   #14183   +/-   ##
=========================================
  Coverage          ?   51.99%           
=========================================
  Files             ?      308           
  Lines             ?    45569           
  Branches          ?    10553           
=========================================
  Hits              ?    23693           
  Misses            ?    17069           
  Partials          ?     4807

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 017446b...93e5b5e. Read the comment docs.

@SebastianBoe
Copy link
Collaborator

Very nice, would you mind looking into if -Wl,--sort-section=alignment could be used instead?

Then the optimization will be applied even more broadly, and we don't need to remember to invoke SORT_BY_ALIGNMENT for new sections.

I tried to get this in a while back, but encountered a runtime issue in xtensa that I was not able to debug.

#4296

If there are any issues with defaulting to sorting wildcard sections (there shouldn't in a well-designed system), then the proposed patchset is still better than what we are doing today.

@galak galak requested a review from ioannisg March 8, 2019 12:00
@galak
Copy link
Collaborator

galak commented Mar 8, 2019

Adding @ioannisg, need to make sure we are keep various partitions together to not break userspace. @ioannisg knows the details.

Copy link
Member

@ioannisg ioannisg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ok to me; AFAIU, the patch only sorts-by-alignment the kernel SRAM sections (noint, bss, etc.) and the kernel objects.

For ARM, changing the order of kernel SRAM sections does not break User Mode/MPU etc., as long as the sections still form a consecutive memory area, together, also, with the APP MEM partitions.

So looks safe enough - in the mean time, I am running the kernel test suite to see if anything is broken. Will report here.

Copy link
Member

@ioannisg ioannisg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving this one. I ran the tests on all Cortex-M & MPU arches and things look good.

Copy link
Collaborator

@SebastianBoe SebastianBoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-Wl,--sort-section=alignment should be used if possible

@dcpleung
Copy link
Member Author

dcpleung commented Mar 8, 2019

-Wl,--sort-section=alignment should be used if possible

Test tests/application_development/gen_inc_file failed with qemu_nios2 with *(SORT_BY_ALIGNMENT(".rodata.*")). According to the map file, the linker moved the whole section two bytes forward, and the test never booted (AFAICT).

Also, the device init section (.init_*) requires strict ordering by name, and also IRQ vector sections. And I am not totally sure the linker would leave those sections sorted by name only without reshuffling them.

I am a bit hesitant to enable it for everything, for now.

@andrewboie
Copy link
Contributor

-Wl,--sort-section=alignment

Does this override the SORT_BY_NAME() invocations we have in the linker scripts?
Also it's not clear to me that we need to change this many lines in the linker scripts. I would imagine, that for most text/data, everything is already 4 byte aligned. The kernel object sections are simply arrays of kernel objects.

Can we identify what areas we are enforcing something other than 4 byte alignment, and just sort by alignment there?

@andrewboie
Copy link
Contributor

Adding @ioannisg, need to make sure we are keep various partitions together to not break userspace.

This patch is orthogonal to userspace's application shared memory feature.

@dcpleung dcpleung force-pushed the linker_sort_by_alignment branch 2 times, most recently from d5f1c47 to 4d40301 Compare March 9, 2019 09:00
@dcpleung dcpleung changed the title Linker: Sort by Alignment for common text/data/bss/etc. sections and kernel objects Linker: Sort by Alignment for common text/data/bss/etc. sections Mar 9, 2019
@dcpleung
Copy link
Member Author

dcpleung commented Mar 9, 2019

Updated the PR and removed the patches for kernel objects. As @andrewboie said, those objects are of fixed size already so there is no gain. Also went through numerous sanitycheck where sorting text sections has no benefits except ARM. On ARM, sorting text sections shows saving of around 30 bytes for rom_size, so keeping those. Sorting by alignment for data, noinit, rodata and bss showed improvements for both ROM and RAM size.

@SebastianBoe
Copy link
Collaborator

Does this override the SORT_BY_NAME() invocations we have in the linker scripts?

Fortunately, it behaves as expected, and does not override SORT_BY_NAME.

https://sourceware.org/binutils/docs/ld/Input-Section-Wildcards.html#Input-Section-Wildcards

"When both command line section sorting option and linker script section sorting command are used, section sorting command always takes precedence over the command line option."

@SebastianBoe
Copy link
Collaborator

SebastianBoe commented Mar 11, 2019

Also, the device init section (.init_*) requires strict ordering by name, and also IRQ vector sections. And I am not totally sure the linker would leave those sections sorted by name only without reshuffling them.

These sections can and should explicitly use SORT_BY_NAME, it will leave them sorted by name.

@SebastianBoe
Copy link
Collaborator

Test tests/application_development/gen_inc_file failed with qemu_nios2 with (SORT_BY_ALIGNMENT(".rodata.")). According to the map file, the linker moved the whole section two bytes forward, and the test never booted (AFAICT).

This sounds like a bug in our linker scripts that should be fixed independently of whether wildcards (without explicit sorting commands) are sorted by alignment or by the order that the linker sees the section.

@dcpleung dcpleung changed the title Linker: Sort by Alignment for common text/data/bss/etc. sections linker: sort sections by alignment Mar 12, 2019
@dcpleung
Copy link
Member Author

-Wl,--sort-section=alignment should be used if possible

Updated the PR to do this.

Copy link
Collaborator

@galak galak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we shouldn't just do this alway? Why make it a Kconfig option?

@dcpleung
Copy link
Member Author

Is there a reason we shouldn't just do this alway? Why make it a Kconfig option?

  1. Given that it broke Nios2 without changing the linker script, having a kconfig option provides a quick way to exclude a board or platform to un-break the build while a solution is being worked on.
  2. The kconfig also provides flexibility in case someone wants to optimize their own placement, without having to revert this patch on their tree.

@galak
Copy link
Collaborator

galak commented Mar 13, 2019

  • Given that it broke Nios2 without changing the linker script, having a kconfig option provides a quick way to exclude a board or platform to un-break the build while a solution is being worked on.
  • The kconfig also provides flexibility in case someone wants to optimize their own placement, without having to revert this patch on their tree.

I'd rather we always do this an once someone want's that flexibility they can ask or submit the change. Seems like a Kconfig option that we'll have around and never change.

During testing with sorting section by alignment with qemu_nios2,
if rodata section is not aligned on 4-byte boundary and its size
not of multiple of 4, it would never boot correctly. So align
the rodata here. This is in preparation to enable the linker
option to sort sections by alignment.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This turns on the linker flag to sort sections by alignment
in decreasing size of symbols. This helps to minimize
padding between symbols.

This also adds the linker options to sort common symbols by
alignment in descending to minimize the need for padding.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
@dcpleung
Copy link
Member Author

Removed the kconfig option.

@galak galak merged commit af10d16 into zephyrproject-rtos:master Mar 13, 2019
@dcpleung dcpleung deleted the linker_sort_by_alignment branch March 13, 2019 20:58
SebastianBoe added a commit to SebastianBoe/zephyr that referenced this pull request Mar 15, 2019
We have users that have problems in their linker scripts where the
order of input sections matters.

To allow users to use the latest Zephyr while working on a fix to
their linker scripts we add an option to allow leaving the sections
unsorted.

See discussion here for more details
zephyrproject-rtos#14183

Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
carlescufi pushed a commit that referenced this pull request Apr 24, 2019
We have users that have problems in their linker scripts where the
order of input sections matters.

To allow users to use the latest Zephyr while working on a fix to
their linker scripts we add an option to allow leaving the sections
unsorted.

See discussion here for more details
#14183

Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
nashif pushed a commit that referenced this pull request May 3, 2019
We have users that have problems in their linker scripts where the
order of input sections matters.

To allow users to use the latest Zephyr while working on a fix to
their linker scripts we add an option to allow leaving the sections
unsorted.

See discussion here for more details
#14183

Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
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.

None yet

6 participants