[PATCH] D72197: [MC][ELF] Emit a relocation if target is defined in the same section and is non-local

James Y Knight via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 6 08:31:35 PST 2020


jyknight added a comment.

In D72197#1805825 <https://reviews.llvm.org/D72197#1805825>, @jyknight wrote:

> I don't see the behavior you say gnu as has.


Oh..the behavior of gnu as is apparently different for `jmp` (and conditional variants thereof), versus `call`. It behaves as you say for `call`. I wonder if it's a bug that it doesn't do so for jmp.

It does cause some odd inconsistencies:

  #include <stddef.h>
  #include <string.h>
  
  __attribute__((noinline)) int memcmp2(const void *s1, const void *s2, size_t n) {
    return memcmp(s1, s2, n);
  }
  
  int memcmp3(const void *s1, const void *s2, size_t n) {
    return memcmp2(s1, s2, n);
  }
  
  int memcmp3_plus1(const void *s1, const void *s2, size_t n) {
    return memcmp2(s1, s2, n) + 1;
  }

Running `gcc -c -o /tmp/test.o /tmp/test.c -O2 && objdump -dr /tmp/test.o`, you can see that a relocation was emitted for the tail-call JMP from memcmp2 to memcmp, and for the CALL from memcmp3_plus1 to memcmp2, but NOT for the tail-call JMP from memcmp3 to memcmp2.

  Disassembly of section .text:
  
  0000000000000000 <memcmp2>:
     0: e9 00 00 00 00        jmpq   5 <memcmp2+0x5>
     1: R_X86_64_PLT32 memcmp-0x4
     5: 66 66 2e 0f 1f 84 00  data16 nopw %cs:0x0(%rax,%rax,1)
     c: 00 00 00 00 
  
  0000000000000010 <memcmp3>:
    10: eb ee                 jmp    0 <memcmp2>
    12: 66 66 2e 0f 1f 84 00  data16 nopw %cs:0x0(%rax,%rax,1)
    19: 00 00 00 00 
    1d: 0f 1f 00              nopl   (%rax)
  
  0000000000000020 <memcmp3_plus1>:
    20: 48 83 ec 08           sub    $0x8,%rsp
    24: e8 00 00 00 00        callq  29 <memcmp3_plus1+0x9>
     25: R_X86_64_PLT32 memcmp2-0x4
    29: 48 83 c4 08           add    $0x8,%rsp
    2d: 83 c0 01              add    $0x1,%eax
    30: c3                    retq   


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72197/new/

https://reviews.llvm.org/D72197





More information about the llvm-commits mailing list