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

    <tr>
        <th>Summary</th>
        <td>
            [avr] Wrong code to indirect call functions located beyond the first 128KiB code segment.
        </td>
    </tr>

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

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

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

<pre>
    Consider the following C code:
```c
int func (void);

int main (void)
{
    int (*f)(void) = func;
    __asm ("" : "+r" (f));
    return f();
}
```

and compiled with:

```none
> clang --target=avr foo.c -Os -save-temps -mmcu=atmega2560 -Os -o foo.elf -Wl,--defsym,func=0x22222
> avr-objdump -d foo.elf > foo.lst
> avr-objdump -dr foo.o > foo.lss
```
So that the code mimics a function `func` located at 0x22222. There are problems with the generated code:

* main is using `icall` to execute `f()`, but `icall` will always target the low 128KiB segment. The correct instruction is `eicall` resp. `eijmp` for tail-calls.
* The compiler uses the wrong relocations like `pm_lo8(sym)` and `pm_hi8(sym)`, where the correct code will use `lo8(gs(sym))` and `hi8(gs(sym))`; so the GNU linker will generate stubs as needed. Correct RELOCs are `R_AVR_LO8_LDI_GS` resp. `R_AVR_HI8_LDI_GS`.
* If main was located beyond the first 128KiB segment, `icall`/`ijmp` won't work either, correct are `eicall`/`eijmp`, cf. also startup-code.

 


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyFVduSozYQ_Rp4UUGB8AU_8OCxdzZTmcpUzW6yjy6BGqwZgShJjHf-Pi3JF7xJKi7bIHS6-_TpI7tW_LPaqcEIDprYI5BWSalOYujIjjSKQ1Rso2wfZdtolYV3E9ZisKSdhoZEtPxQgkd0ExUPZ-wV0TMxzBFhe33GEXw5FAIium0d4AolUbH3Ba5ZHfpwYKYPeIpvBG2Jv33QfknLkGVzF6XBTnogrY-b0Vzvf-ltTp8NHBXoR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