This is an archive of the discontinued LLVM Phabricator instance.

[LLD][LinkerScript] Allow non-alloc sections to be assigned to segments.
ClosedPublic

Authored by andrewng on Jun 14 2017, 6:41 AM.

Details

Summary

This patch makes changes to allow sections without the SHF_ALLOC bit to be
assigned to segments in a linker script.

The assignment of output sections to segments is performed in
LinkerScript::createPhdrs. Previously, this function would bail as soon as it
encountered an output section which did not have the SHF_ALLOC bit set, thus
preventing any output section without SHF_ALLOC from being assigned to a
segment.

This restriction has now been removed from LinkerScript::createPhdrs and instead
a check for SHF_ALLOC has been added to LinkerScript::adjustSectionsAfterSorting
to not propagate program headers to sections without SHF_ALLOC which matches the
behaviour of bfd linker scripts.

Diff Detail

Event Timeline

andrewng created this revision.Jun 14 2017, 6:41 AM
ruiu edited edge metadata.Jun 19 2017, 12:36 PM

Hi Andrew,

What are you actually trying to address with this patch? I'd like to know more about the background as to why you want this. Is there any reason you can't fix your program rather than the linker?

In D34204#784448, @ruiu wrote:

Hi Andrew,

What are you actually trying to address with this patch? I'd like to know more about the background as to why you want this. Is there any reason you can't fix your program rather than the linker?

Hi Rui,

Basically, we have custom program headers in our binaries that are used by some of our tools. We need to be able to populate these segments with the sections containing the appropriate data. These sections are not allocated as they are not required to be loaded at runtime. The reason we need these program headers is that the section headers may be stripped from the binaries but our tools still need access to the data.

Note that GNU ld supports this behaviour too.

Cheers,
Andrew

ruiu added a comment.Jun 20 2017, 7:39 PM

Can you change your program?

I guess that the GNU linker is not designed to support this, but it's just that it happens to work with their linker. We are not aiming 100% compatibility with GNU as it is impossible (and doesn't make sense) to copy all the corner cases like this. So even if it works with the GNU linker, we sometimes want to stop and think whether it semantically makes sense or not.

In D34204#786240, @ruiu wrote:

Can you change your program?

There are a number tools that require this information, some are not in our control.

I guess that the GNU linker is not designed to support this, but it's just that it happens to work with their linker. We are not aiming 100% compatibility with GNU as it is impossible (and doesn't make sense) to copy all the corner cases like this. So even if it works with the GNU linker, we sometimes want to stop and think whether it semantically makes sense or not.

What we're trying to achieve is to place a non allocatable section into a custom PHDR, which to us makes perfect sense and isn't a corner case. However, we do accept that enabling this could cause interesting corner cases if mixing alloc and non-alloc sections in a PHDR.

Cheers,
Andrew

Hi,

Is there any more feedback on this patch or is it good to land?

Cheers,
Andrew

ruiu added a comment.Jun 23 2017, 9:42 AM

This is probably OK but I'd like to hear from Rafael as he wrote the code you are updating.

ELF/LinkerScript.cpp
758

Describe not what this code does but why you want it.

761–762

Please format according to the LLVM coding style.

andrewng edited the summary of this revision. (Show Details)Jun 26 2017, 2:18 AM
andrewng updated this revision to Diff 103897.Jun 26 2017, 2:22 AM

Updated for review comments.

Hi Rafael,

Is this patch now good to land?

Cheers,
Andrew

Hi Rafael,

Is this patch now good to land?

Cheers,
Andrew

Ping.

This revision was automatically updated to reflect the committed changes.