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

    <tr>
        <th>Summary</th>
        <td>
            llvm-objcopy should follow physical addresses even if they are unaligned
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          quic-akaryaki
      </td>
    </tr>
</table>

<pre>
    When physical addresses are not aligned as per sections' alignment value, llvm-objcopy inserts padding, which moves the data unexpectedly. This is different from GNU objcopy, which keeps sections in the image according to their physical addresses. Consider this example:

main.c
```
 int g __attribute__ ((aligned (8))) = 0xabcd;
  int main() {
    return g;
  }
```

script.ld
```
SECTIONS {
  ROM_START = 0x00200000;
  RAM_START = 0x00400000;
  .text (ROM_START): AT(ROM_START) {
    *(*.text*)
    . = ALIGN(8);
    . = . + 0x4;
  }
  INIT_DATA_START = LOADADDR(.text) + SIZEOF(.text);
 .data (RAM_START): AT(INIT_DATA_START) {
    *(*.data*)
 }
}

```

```
clang main.c -c
ld.lld -T script.ld main.o
objcopy -O binary --only-section=.text --only-section=.data a.out a-gnu.bin
llvm-objcopy -O binary --only-section=.text --only-section=.data a.out a.bin

```

a.bin is four bytes longer than a-gnu,out with four zeroes of padding inserted between text and data. During runtime, the INIT_DATA_START symbol will not point to data but to the padding before it. This example is modelled after an embedded image where initial data is copied by the startup routine and it's alignment is not important. 

This behaviour was introduced in https://reviews.llvm.org/D128961 which intended to apply alignment to sections that were not present in the original elf and might not have correct address. However, it's effect was broader. I have a patch that narrows down D128961, will create it shortly.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVl1vqzgT_jXOzSiIQJqPi1zQ5uS8lc7bSm1WK-1NZfAAPjU2aw9J2V-_sqFJms2em60QDfbwzDzz8RjunKw04obd3bO77YR3VBu7-bOTxZS_c9vzdznJjeg3v9eooa17Jw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