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

    <tr>
        <th>Summary</th>
        <td>
            Possible objcopy regression in LLVM 15
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    `objcopy -O binary` produces a broken binary from the [test.elf](https://github.com/llvm/llvm-project/files/9800058/test.zip) file.

Regressed version:
```shell
$ rust-objcopy -V                        
llvm-objcopy, compatible with GNU objcopy
LLVM (http://llvm.org/):
  LLVM version 15.0.2-rust-1.66.0-nightly
  Optimized build.
  Default target: x86_64-pc-windows-msvc
  Host CPU: znver1

```
Version it worked on:
```shell
$ rust-objcopy -V
llvm-objcopy, compatible with GNU objcopy
LLVM (http://llvm.org/):
  LLVM version 14.0.6-rust-1.64.0-stable
  Optimized build.
  Default target: x86_64-pc-windows-msvc
  Host CPU: znver1
```

Note that I was using LLVM tools shipped with Rust, but presumably this doesn't make a difference here?

[Corresponding Rust issue](https://github.com/rust-lang/rust/issues/102983)

Quoting @adamgreig 's initial analysis:

> The elf in question has the following sections:

> ```
> Program Header:
>     LOAD off    0x000000f4 vaddr 0x08004000 paddr 0x08004000 align 2**2
>          filesz 0x000000c0 memsz 0x000000c0 flags r--
>     LOAD off    0x000001b4 vaddr 0x080040c0 paddr 0x080040c0 align 2**2
>          filesz 0x00006764 memsz 0x00006764 flags r-x
>     LOAD off    0x00006924 vaddr 0x0800a824 paddr 0x0800a824 align 2**4
>          filesz 0x00000ff8 memsz 0x00000ff8 flags r--
>     LOAD off    0x0000791c vaddr 0x20000000 paddr 0x20000000 align 2**2
>          filesz 0x00000000 memsz 0x00000444 flags rw-
> ```
> Notably the third section has an offset of 0x6924 and an alignment of 16 which doesn't match, but since the first section has an offset of 0xf4 the resulting start of the third section in the output is 0x6830 which is aligned. In the working binary, which is identical between GCC, LLVM14, and probe-rs, that third section starts at byte 0x6830, while in the new LLVM15-produced binary the third section starts at address 0x683C, 12 bytes later, which would be 16-byte aligned if it weren't for the initial 0xf4 offset.

> I think the only relevant commit in LLVM is https://github.com/llvm/llvm-project/commit/0c01f42fad413163320c24ab4e513fbd282e4168.

> I am not entirely sure what the correct behaviour is but I guess it's the old behaviour. I don't know why this ELF has such a weird set of offsets and different alignments, either.
















</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFVktznDgQ_jXMpYspEI9hDnNw7Dxc5U2yqSTXLQHNoFggVhKejH_9tgSMPc4mXh92l6IAtUT3p68f6lLVx12QR6r8VqnhCOEHKEXP9ZFkMGhVjxUa4FBqdYv9PAeNVh3YFiHIXlk0do2yCbKrgBWttYMJkouAvaF7L2w7lutKdTSQ8m55haT5G1aWho2QaOi9LaIoygr68grvxRCwLbjZdRBdBdHF9PyEe43GYA13qI1QvbM1zefRdJsWpZxlLAU9GhuetvcVfnJNP3hw8-KAXQIhH7gVpUQ40F7g7fsvsEz7H25uvv4G875P23Za1krv_Wh7QgjgV8_AIc7W0ZqFHl-8zvN1FPZi31p5XJZ_GKzoxD1tthyFrNeL_AobPkoLlus9WtIP34v8jzwNhyo8iL5WBxN25q5a1r9TxsLlxy9u5X1P9uPHlJ6Im4ZfZ3jCwkHpWzL-cpL_BzJTIjM_kUmj0FhOpv4zLs9ZnJ7vlUXKE27hGg7cwGhEv5-AW6WkAdOKYSBMno9PBN7xVI6WUg_N2NEGjvS_MFArNH3ANhY6fouUkLVoGtTYVwgtvYPkzZlPs1eXSpOOQfW1s-l0gzBmxGfz1HMoeb-fv-nlf3RZGkdsWyTOD4-M_T4q62wEacRr3lGCChqwjQHRCyu4BN5zeTTCPMTR9Exew2eqIlQ9aCn8STasc2ZLVLnq0igp1cGpNlQsaOaRAvr10dcT8kn0Uau95h28Q16jPvvPXTcfLq5ANY37jr5H_mp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