This is an archive of the discontinued LLVM Phabricator instance.

[LLD] Order writable executable sections before writable ones
ClosedPublic

Authored by kettenis on Apr 23 2017, 8:17 AM.

Details

Summary

I'm working on SPARC support in lld. I've got a working diff against lld 4.0.0, and I'm working on moving the changes over into the current source tree. Here is a first diff that addresses one of the more fundamental issues I ran into.

On SPARC, .plt is both writeable and executable. The current way sections are sorted means that lld puts it after .data/.bss. but it really needs to be close to .test to make sure branches into .plt don't overflow. I'd argue that because .bss is supposed to come last on all architectures, we should change the default sort order such that writable and executable sections come before sections that are just writeable. read-only executable sections should still come after sections that are just read-only of course. This diff makes this change.

With this diff, the following tests fail:

lld :: ELF/amdgpu-globals.s
lld :: ELF/gc-sections.s
lld :: ELF/i386-tls-ie-shared.s
lld :: ELF/relocation-size-shared.s
lld :: ELF/relocation-size.s
lld :: ELF/section-layout.s
lld :: ELF/tls-i686.s

The ELF/section-layout.s test fails because it explicitly tests that writeable and executable sections come after sections that are just writeable. Once adjusted that tests will act as a regression test for this diff.
I think most of the other tests just accidentally include a executable and writeable section. I'm not sure about amdgpu-globals.s though as I'm not really familliar with the requirements of that architecture.

Diff Detail

Event Timeline

kettenis created this revision.Apr 23 2017, 8:17 AM
ruiu added inline comments.Apr 24 2017, 10:34 AM
lld/ELF/Writer.cpp
688–695

Is there any problem if you just return AIsExec?

kettenis added inline comments.Apr 24 2017, 2:23 PM
lld/ELF/Writer.cpp
688–695

Yes. Then read-only executable sections would be sorted before read-only sections, which is not what we want. Basically the current ordering is:

R
RX
RW <- contains .bss
RWX

I propose we change this to

R
RX
RWX
RW <- contains .bss

ruiu added inline comments.Apr 24 2017, 3:05 PM
lld/ELF/Writer.cpp
688–695

I believe this code is correct, but this function seems a bit puzzling to me. Each decision-making code block is well-commented and clear about what it does, but as a whole it's not easy to predict what this function returns for some two sections. I want to make this as simple as your example

R
RX
RWX
RW.

Do you think you can improve this function? If you don't have a bandwidth to handle it, I'll try to simplify.

kettenis updated this revision to Diff 99982.May 23 2017, 1:40 PM

Updated diff adapted to the changes that have been made to the function that determines the section ordering. Also adjusts the testsuite for the changed ordering,

ruiu accepted this revision.May 23 2017, 1:48 PM

LGTM

lld/ELF/Writer.cpp
637

nit: RF_WRITE_EXEC is probably more natural order, as in rwx.

681

Add "(such as SPARC)" after "writable"

693–694

} else if {

This revision is now accepted and ready to land.May 23 2017, 1:48 PM
kettenis closed this revision.Jul 1 2017, 6:10 AM

Diff has been committed in r304008.