[llvm] MC: Centralize X86 PC-relative fixup adjustment in MCAssembler (PR #147113)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 8 00:46:08 PDT 2025


================
@@ -587,22 +587,11 @@ void X86MCCodeEmitter::emitImmediate(const MCOperand &DispOp, SMLoc Loc,
     }
   }
 
-  // If the fixup is pc-relative, we need to bias the value to be relative to
-  // the start of the field, not the end of the field.
-  if (PCRel) {
-    ImmOffset -= Size;
-    // If this is a pc-relative load off _GLOBAL_OFFSET_TABLE_:
-    // leaq _GLOBAL_OFFSET_TABLE_(%rip), %r15
-    // this needs to be a GOTPC32 relocation.
-    if (Size == 4 && startsWithGlobalOffsetTable(Expr) != GOT_None)
-      FixupKind = X86::reloc_global_offset_table;
----------------
MaskRay wrote:

This has no effect on all the `_GLOBAL_OFFSET_TABLE_` uses LLVM emits or hand-written assembly uses in practice

In 2020, I added this piece of code in [reviews.llvm.org/D47507](https://reviews.llvm.org/D47507). I am actually not sure I got everything correct, but moving the code from MCCodeEmitter (parse time) to AsmBackend (relocation decision time) is the right direction.

```
% rg '(lea|mov).*_GLOBAL' lld/test/ELF llvm/test/MC/X86/
lld/test/ELF/x86-64-reloc-gotpc64.s
14:  movabsq $_GLOBAL_OFFSET_TABLE_-., %r11

lld/test/ELF/relocation-i686.s
66: movl $_GLOBAL_OFFSET_TABLE_, %eax

lld/test/ELF/x86-64-reloc-gotoff64.s
23:  leaq _GLOBAL_OFFSET_TABLE_(%rip), %rdx

lld/test/ELF/i386-gotpc.s
7:movl $_GLOBAL_OFFSET_TABLE_, %eax
```

https://github.com/llvm/llvm-project/pull/147113


More information about the llvm-commits mailing list