0

I am experiencing relocation truncated to fit kind of error for my embedded ARM application compiled and linked with with GCC 4.9.3. I am using code relocation for this function from external flash (0x70000000) to internal RAM (0x08000000) to improve performance of my application, and this is one of the causes of the problem.

I have a small inline-assembly naked function to perform a short loop:

void ThreeCycleDelay(uint32_t count) __attribute__((naked))
{
  __asm("  subs  r0, #1\n"
        "  bne   ThreeCycleDelay\n"
        "  bx    lr");
}

But when linking, I receive the following error from ld:

D:/app/app.a(app_utils.obj):(.ARM.exidx.text.ThreeCycleDelay+0x0): relocation truncated to fit: R_ARM_PREL31 against `.text.ThreeCycleDelay'

I have seen suggestions on the internet to solve this issue, but none of them were helpful. Trying to "remove" .ARM.exidx section by -funwind-tables -fno-exceptions made no difference.

The error disappears when I perform no code relocation, and it does not show for any other function. Removing the __attribute__((naked)) does not solve the issue either, so I was suspicious it is linekd with the inline assembly jump, but the real question is - how can I solve this issue?

Petr
  • 486
  • 7
  • 19
  • 1
    Add a label to your first line in the asm block, and use that? – Jester Jan 26 '17 at 18:19
  • The compiler shouldn't be generating unwind info for naked functions since it has no idea how to unwind it. You should just write the function ins plain assembly so you don't have to worry about the compiler does or doesn't do. – Ross Ridge Jan 26 '17 at 19:07
  • since the function has `void` return type, no registers should be changed, so before entering the loop, push r1 and after the loop, pop r1. Also, do not branch back to the function entry point. rather branch to a label on the `subs` instruction – user3629249 Jan 27 '17 at 00:41
  • @Jester Doing only that unfortunately made no difference. – Petr Jan 27 '17 at 09:54
  • @user3629249 Are you sure changing R0 is a problem here? I thought R0 is for input parameters, and so that is would be absolutely fine to change it. – Petr Jan 27 '17 at 09:56
  • Does it work with an empty function body even? – Jester Jan 27 '17 at 16:30
  • if it works correctly when no relocated, then the problem is more likely that the code is not PIC. Are you using a linker command file to handle the linking? Does that command file contain the necessary relocation statement(s)? – user3629249 Jan 27 '17 at 17:47
  • @Jester empty body is working as expected – Petr Feb 01 '17 at 10:39
  • @user3629249 Yes, I am using linker script, and the necessary relocation for my function ThreeCycleDelay is mentioned there - that is the reason why .ARM.exidx (which is not relocated) is complaining. – Petr Feb 01 '17 at 10:41
  • @RossRidge Thanks for the hint. I am not very used to assembly, but your advice turned out to be the only one solving the issue. Would you like to turn it into an answer? I would happily accept. – Petr Feb 01 '17 at 10:42

0 Answers0