This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Add constant for querying the image base in linker scripts
AbandonedPublic

Authored by jhenderson on Sep 25 2017, 7:20 AM.

Details

Reviewers
ruiu
rafael
Summary

LLD has defaults for the image base of all its targets, and switches to modify what these bases are (e.g. --image-base). These defaults and switches are ignored when processing linker scripts, and in the typical case, a linker script opens by setting the location counter to the desired base value.

In our case, we ship linker scripts, but we want our customers to be able to vary the image base whilst still using these scripts. This is not possible currently.

An alternative approach would be to set the location counter to the image base initially, instead of zero. However, this would be a change in existing behaviour that could affect users who are assuming the default value is zero, and are therefore not setting the value explicitly.

Diff Detail

Event Timeline

jhenderson created this revision.Sep 25 2017, 7:20 AM
ruiu edited edge metadata.Sep 25 2017, 10:46 AM

OK, so the current behavior of -image-base is that it doesn't have any effect if a linker script is used, is this correct. I can imagine that that is perceived as a pretty odd behavior from the user's perspective. I think a natural assumption of the -image-base behavior to start . with an address given by -image-base.

We don't need to think about backward compatibility because -image-base is a new option, no?

grimar added a subscriber: grimar.Sep 26 2017, 1:06 AM
In D38238#880313, @ruiu wrote:

OK, so the current behavior of -image-base is that it doesn't have any effect if a linker script is used, is this correct. I can imagine that that is perceived as a pretty odd behavior from the user's perspective. I think a natural assumption of the -image-base behavior to start . with an address given by -image-base.

We don't need to think about backward compatibility because -image-base is a new option, no?

Assuming I use Config->ImageBase (which would seem natural), to set the initial value of ., this will affect the behaviour of links even without the use of -image-base, as the value is non-zero for most targets by default. For example, a single linker script that looks like the following for x86_64 would produce a different value for "mysym" (0 previously, 0x200000 with that change):

SECTIONS {
  mysym = .;
}

Another thing I just realised - setting . to Config->ImageBase could break people's links if they also explicitly set ., assuming an initial value of zero, to some value less than the default. In the following case, the data section will appear before the text section in the address space, but the two program segments will be out-of-order in an x86_64 link (according to the ELF specification, loadable segments must appear in address-order):

SECTIONS {
    .text : {
        *(.text .text.*)
    }
    . = 0x100000;
    .data : {
        *(.data .data.*)
    }
}
ruiu added a comment.Sep 27 2017, 9:23 PM

We could set . to a non-zero value only when -image-base is given, so I don't think we need to worry too much about changing the default behavior.

But I feel like I don't actually understand the problem you are working for. The -image-base option is a lld-specific option, so your program depends on lld. But you are worried that its linker script may not work with lld. That seems like a bit odd situation. Why can't you just update your linker script?

In D38238#882941, @ruiu wrote:

But I feel like I don't actually understand the problem you are working for. The -image-base option is a lld-specific option, so your program depends on lld. But you are worried that its linker script may not work with lld. That seems like a bit odd situation. Why can't you just update your linker script?

We have an existing linker, which uses linker scripts by default, and has options that as part of their mechanism change the base address. To avoid having different scripts (one for each different possible base address), we use something similar to the script extension suggested in this review. Our LLD port uses the --image-base mechanism (although not the switch directly) to achieve the change in base address, but we still plan to use linker scripts, so need to have something to avoid needing multiple different scripts. Having considered it, I think that the alternative change you propose should work for us as well. I will try experimenting with that.

jhenderson abandoned this revision.Oct 10 2017, 3:29 AM

Abandoning in favour of D38360.