This is an archive of the discontinued LLVM Phabricator instance.

ELF: Don't use LayoutPass.
Needs ReviewPublic

Authored by ruiu on Jan 29 2015, 4:38 PM.

Details

Summary

Previously we applied the LayoutPass to order atoms and then
apply elf::ArrayOrderPass to sort them again. The first pass is
basically supposed to sort atoms in the normal fashion (which
is to sort symbols in the same order as the input files).
The second pass sorts atoms in {init,fini}_array.<priority> by
priority.

The problem is that the LayoutPass is overkill. It analysize
references between atoms to make a decision how to sort them.
It's slow, hard to understand, and above all, it doesn't seem
that we need its feature for ELF in the first place.

This patch remove the LayoutPass from ELF pass list. Now all
reordering is done in elf::OrderPass. That pass sorts atoms by
{init,fini}_array, and if they are not in the special section,
they are ordered as the same order as they appear in the command
line. The new code is far easier to understand, faster, and
still able to create valid executables.

I might have simplified things too much, because previously
we sorted atoms not only by position but by its permissions etc.
Because I didn't see any legitimate need to do so, I just decided
not to do that in this patch.

This patch changes the order of final output, although that's
benign. I didn't update affected tests yet as I'd like to get
feedback first.

Diff Detail

Event Timeline

ruiu updated this revision to Diff 19006.Jan 29 2015, 4:38 PM
ruiu retitled this revision from to ELF: Don't use LayoutPass..
ruiu updated this object.
ruiu edited the test plan for this revision. (Show Details)
ruiu added reviewers: Bigcheese, atanasyan, shankarke.
ruiu added a project: lld.
ruiu added a subscriber: Unknown Object (MLST).
Bigcheese edited edge metadata.Jan 30 2015, 4:07 AM

I believe this is correct. We do permission binning in the ELF writer.

shankar.easwaran requested changes to this revision.Jan 30 2015, 5:08 AM

You cannot move the array order pass outside the elf writer. You might have a linker script or an atom order file that selects aroms from .init_array and move it to to the ,text section after all the .text. The layout pass needs to make sure atoms are sorted by link order and leave everything else to the ELF writer.

This revision now requires changes to proceed.Jan 30 2015, 5:08 AM

The array order pass is already outside of the elf writer.

atanasyan edited edge metadata.Jan 30 2015, 7:13 AM

I agree with Michael, the idea looks correct. By the way I have checked the patch using the LLVM test suite for MIPS target and found no regressions.

shankarke edited edge metadata.Jan 30 2015, 8:58 AM

This is a bug that apparently will be seen when using linker scripts.

Linker scripts control this behavior by using SORT_BY_INIT_PRIORITY. This is usually not present on targets that dont support init array by priority.

This is not safe to be done before the writer, and makes it hard to support with linker scripts IMO, and the pass should be moved inside the ELFWriter.

We might want to consider enabling this pass only for targets that need support of running initializers by priority (Example : X86_64)

This is a great idea though, that sorting by file ordinals / atom ordinals should be enough for the general case. If an order file is used then it makes sense to use the complete layout pass.

ruiu added a comment.Jan 30 2015, 11:06 AM

Thank you for reviewing!

As to {init,fini}_array, sorting by {init,fini}_array priority is already there for all architectures and this patch doesn't change that. Currently LLD doesn't interpret SORT_BY_INIT_PRIORITY linker script. I believe there's no bug there (because the feature doesn't exist yet).

Looks like this patch is in the right direction. I'll update the tests and upload a new patch to Phab.

ruiu updated this revision to Diff 19051.Jan 30 2015, 12:27 PM
ruiu edited edge metadata.

Updated test as this patch changes the order of output, although that change is benign.

shankarke accepted this revision.Jan 30 2015, 1:04 PM
shankarke edited edge metadata.

Can you run the llvm test suite and build lld with lld to make sure they are working properly.

ruiu added a comment.Jan 30 2015, 5:40 PM

All tests that passed before applying this patch still pass.

Thanks for the info.

LGTM

atanasyan resigned from this revision.Feb 3 2016, 12:45 AM
atanasyan removed a reviewer: atanasyan.
test/elf/Mips/rel-dynamic-02.test