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

    <tr>
        <th>Summary</th>
        <td>
            [LLD] Overflow related to program headers
        </td>
    </tr>

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

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

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

<pre>
    Considering the following example (just a small reproducer):
```
.section data,"wa",@nobits
.zero 1
.section .second_in_section,"ax",@progbits
.zero 1
.section .first_in_section,"ax",@progbits
.zero 1
.globl _start
_start:
```
If I use the following linker script:
```
MEMORY { mem (r): org = 0x10000000, len = 0x00100000 }

SECTIONS {
  first_section 0x10000000 : { KEEP (*(.first_in_section)); } > mem
  second_section (0x10000000 +64) : { KEEP (*(.second_in_section)); } > mem
  /DISCARD/ : { KEEP (*(.text, data .comment, .symtab, .strtab)); }
}
```
I got the expected result:
```
Program Headers:
  Type           Offset   VirtAddr PhysAddr           FileSiz  MemSiz   Flg Align
  LOAD 0x001000 0x0000000010000000 0x0000000010000000 0x000041 0x000041 R E 0x1000
  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x0

 Section to Segment mapping:
  Segment Sections...
   00     first_section second_section 
   01     
 None   .shstrtab 
```
However, if I invert the output section assignment:
```
MEMORY { mem (r): org = 0x10000000, len = 0x00100000 }

SECTIONS {
  second_section (0x10000000 +64) : { KEEP (*(.second_in_section)); } > mem
  first_section 0x10000000 : { KEEP (*(.first_in_section)); } > mem
  /DISCARD/ : { KEEP (*(.text, data .comment, .symtab, .strtab)); }
}
```
I ended up with the following result, with overflowed FileSiz and MemSiz:
```
Program Headers:
 Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz Flg Align
  LOAD           0x001040 0x0000000010000040 0x0000000010000040 0xffffffffffffffc1 0xffffffffffffffc1 R E 0x1000
 GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x0

 Section to Segment mapping:
  Segment Sections...
   00     
   01     
   None   second_section first_section .shstrtab 
```
Gnu ld can handle this case:
```
Program Headers:
  Type           Offset   VirtAddr PhysAddr           FileSiz  MemSiz   Flg Align
  LOAD 0x001000 0x0000000010000000 0x0000000010000000 0x000041 0x000041 R E 0x1000

 Section to Segment mapping:
  Segment Sections...
   00 second_section first_section 
   None   .symtab .strtab .shstrtab 
```
This reflects a silent behavior, that can appear in some complex linker scripts that sometimes can be really difficult to catch by inspecting the output sections (it can be scattered in several included linker script files).

Original discussion: https://discourse.llvm.org/t/overflow-related-to-program-headers/75150
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcV0tv4zYQ_jX0ZRBDouXYPvjg-LEb7G4cxGmLngJKHEncUqRAUom9v76gHn4l3rRF20VLBPFoRM4M55v5SDFrRaYQp2R4Q4aLHqtcrs2UKZ4wUwjFnOjFmu-mc62s4GiEysDlCKmWUr_4J9yyopQIhI6_VtYBA1swKcFgaTSvEjSETshgRoIFCWbkOmj_6se-xcQJrYAzxwidE0pfGKHUi1GgdCycbWd-Q6MhPFvmBa34k1BPraoxwrZ7I6XR2btmUmGs-6tWMqljCU_WMeMaVStf2PRtCrdQWTxLpBTqNzRgEyPKi2u_LL-sH34FMrqBAguf9Da9oE0GZLCAYBsGzSB0DhJVqw2CRg9ktGhN1_83y_nj7fpu4202GoAmHV12DhbBO_K-Py2X9945oTNCx2-kb1JHdeOdARksfbCd8Rayzjqh42MH9OY6InRyydMbeH_HFaGrxe1mPntYELq6ZNLh1vlU-RKEfqKLAlWt6Ntd4VjciM7U4pGzNol74QxkyLSrEcZtiYlDDgZtJS8ie290ZlgBH5FxNHY_DeBxVyIcxjpNLToA-FkYN-PcwH2-s7VwGCshcSO-AXzBov6FlcxgJkWmOrOf17NDXdRCM_ZYXFRF4UF4gGVbIJ3dD3c_PW0eZ_NPTSSdlSNzwWs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