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

    <tr>
        <th>Summary</th>
        <td>
            [lld] Cannot pad ELF32 to 4GB
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    `entry.S`:

```asm
    .code16
    .section .reset, "ax", %progbits
    .code16
    .global reset_vector
reset_vector:
    hlt
    jmp reset_vector

    .align 0x10, 0xFF
```

`link.ld`:

```
OUTPUT_FORMAT("elf32-i386")
OUTPUT_ARCH(i386)
ENTRY(reset_vector)

SECTIONS
{
    /* Reset vector on IA32 starts at 16 bytes below 4 GiB */
    . = 0xFFFFFFF0;
    .reset : {
        *(.reset)
    }

    /* NOTE: LLD requires shstrtab */
    .shstrtab : { *(.shstrtab) }
    .strtab : { *(.strtab) }
    .symtab : { *(.symtab) }

    /* Discard everything else */
    /DISCARD/ : { *(*) }
}
```

Compiled with:

```
clang -fuse-ld=lld -m16 -ffreestanding -nostdlib -Wl,-Tlink.ld -Wl,--nmagic -Wl,--build-id=none -o entry.o entry.S
```

Error:

```
ld.lld: error: section .reset at 0xFFFFFFF0 of size 0x10 exceeds available address space
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
```

Section starting at 0xFFFF_FFF0 with a length of 16 bytes will have its last byte at 0xFFFF_FFFF. lld just adds the length to the start so it thinks it ends at 0x1_0000_0000, making it outside of the 4GB limit.

Caused by: 1fc9f39bd544dc757323d2ee8830134275e382ec
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx9VVtvqzgQ_jXkZQTiHvLAQ5rLOZW67SrJ0WqfKoMniVsDWWzSZH_9GUPIpc0pQsb23L5vPGOyih9TK3ax1PXRWdLMCsaWO7XcfqSd9mWq6HaAHievO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