<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64573>64573</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            LTO disrespecting moving the dot in linker scripts for LTO
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld:ELF,
            LTO
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          nickdesaulniers
      </td>
    </tr>
</table>

<pre>
    Consider the following C code:
```c
// x.c
__attribute__((section("foo")))
int bar = 42;

int main () {
  return bar;
}
```
 (So `bar` is in section `foo`, while `main` is in `.text`).


and poorly written (minimal?) linker script:
```
# foo.lds
PHDRS {
  text PT_LOAD ;
}
SECTIONS {
  .text : {
    *(.text)
    . = 0x100000;
    *(.foo)
  } : text
}
```
The goal here is to separate `bar` from `main` by 0x100000B.

Let's see what happens with various linkers without LTO first, so that we can see clearly that LTO's behavior is quite surprising.

First let's look at GNU ld.bfd:
```
$ clang x.c -Wl,-T,foo.lds -O0
$ llvm-nm a.out | grep -e bar -e ' main'
00000000001005d8 D bar
00000000000000f0 T main
```
Well, they're not exactly 0x100000B apart, but they are at least 0x100000B apart. Ok.

And LLVM ld.lld:
```
$ clang x.c -Wl,-T,foo.lds -O0 -fuse-ld=lld
$ llvm-nm a.out | grep -e bar -e ' main'
0000000000100510 D bar
00000000000000f0 T main
```
Slightly different address for `bar`, but again, at least 0x100000B apart.

Now let's see what lld does with LTO:
```
$ clang x.c -Wl,-T,foo.lds -O0 -fuse-ld=lld -flto
$ llvm-nm a.out | grep -e bar -e ' main'
0000000000100530 d bar
0000000000100030 T main
```
This is a bit unexpected; suddenly both symbols are offset by 0x100000B.  (at least the relative order of symbols hasn't changed, I guess).

This is forked from https://github.com/ClangBuiltLinux/linux/issues/1909 in which Linux kernel security mitigations for AMD SRSO hardware vulnerabilities tries to use a linker script to keep two symbols `0x104104` B apart.  I don't think this can be done portably via linker script, but "it happened to have worked with ld.bfd" and now such kernel patches are merged in virtually every relevant tree, breaking every x86 build with lld.

It's pretty common in the Linux kernel's linker scripts to realign various sections using:

```
. = ALIGN(some constant)
```
if LTO is messing this up, some sections might not have the proper or expected alignment.

Is there another way to specify that a specific section (or symbol) should have an alignment (for ELF objects)?  These sections in the kernel sources a defined in assembler.

cc @MaskRay 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVktz4joW_jVicwrKyATwgkUCzZ1U0c2tDjN3mZKtY6xBljySDOHfTx3ZvHK7ezHTrpSJJZ3X952HhPdqbxAX7OmFPa0Gog2VdQujioNEL1ptFDo_yK08L5bWeCXRQagQSqu1PSmzhyUUViJLn1myYskzmybdX9F_8zXja_gY9d_v7yIEp_I24Ps743PG5x6LoKyJH7y0lnHOeNb_RSFlAuTCAUtXMOEsfel1XzdroQxEBRmwWb8N4DC0zpDoTWa2-uRof5jx-ZsFNk3o9DQB5UEZ6H2jdfJsmjC-hFOlNNISmb2dZdNkFPAjxFPZ6N7H7i2MhMZap89wcioEjD7XyqhaaJauyXutzAEd-MKpJvwd1QuoKZTWjrT03cKf_1h9f7uPnPyAP3fvm-3zCv4e_NuX5e51--1BJPoOLH2-XwRg_JnxeRfYhQ9aH0U2ko9xQs_VxJ1IpPIqwWarqDwq-iUVuwphb4WGCh0StsGCx0Y4EfCOoNLZ-p6E_Hz15uUB_A0GxmcePCKcKhGgEk2DxsNJhQqOwinb-h74btG2ATa7LZTK-UCMewuBJE8IhTBRU6FREJNxfbPbRhM5VuKorCOv_9OqgOBb1zjlldk_-LQmzaB7z7S1BxAB_vj2T9BylJfy58xPoNDC7KmiYPiXZnw53DG-7NMBhtu7k1of66GpQYwoIjZbwt5hA0OM1TREYHwWa4fxWSeVXJ9xkjzJOaxi9Xzao6dMYNfJ_sjPv1CTa9QrzozPHIKxAfBDFEHf8QSiES4inLchHgbhkKDQKHz4fHAE28MDjM9Gwmbzr6-Emtb_F2owLFuP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