Skip to content

[LoongArch] ADD/SUB and TLS relocation should move away from FirstLiteralRelocationKind + offset #135521

@MaskRay

Description

@MaskRay

The current implementation of R_LARCH_SUB{8,16,32,64} and TLS relocation types relies on fixup kinds FirstLiteralRelocationKind + offset (originally intended for .reloc directives). While this is clever and prevents switch cases like

case fixup_...sub8:
  return ELF::R_LARCH_SUB8;

it needs revision.

GNU Assembler treats .reloc directives differently from standard relocations, notably by skipping

  • Skipping STT_SECTION adjustments (when a referenced symbol is local and satisfies certain conditions, it can be redirected to a section symbol).
  • Skipping STT_TLS symbol type setting for TLS relocations.

I intend to fix a .reloc behavior in ELFObjectWriter (#135519) and notice that llvm/test/MC/LoongArch/Relocations/relax-addsub.s (#76433) will be broken. @MQ-mengqing @SixWeining

unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx, 1 b.ref | 3 refs
                                                const MCValue &Target, 2 refs
                                                const MCFixup &Fixup, 4 refs
                                                bool IsPCRel) const { 2 refs
 /// !!!!!!!! TLS fixups should not use FirstLiteralRelocationKind+offset, so that we could move `if (Kind >= FirstLiteralRelocationKind)` before this switch.
  switch (Target.getSpecifier()) {
  case LoongArchMCExpr::VK_TLS_LE_HI20:
  case LoongArchMCExpr::VK_TLS_IE_PC_HI20:
  case LoongArchMCExpr::VK_TLS_IE_HI20:
  case LoongArchMCExpr::VK_TLS_LD_PC_HI20:
  case LoongArchMCExpr::VK_TLS_LD_HI20:
  case LoongArchMCExpr::VK_TLS_GD_PC_HI20:
  case LoongArchMCExpr::VK_TLS_GD_HI20:
  case LoongArchMCExpr::VK_TLS_DESC_PC_HI20:
  case LoongArchMCExpr::VK_TLS_DESC_HI20:
  case LoongArchMCExpr::VK_TLS_LE_HI20_R:
  case LoongArchMCExpr::VK_TLS_LD_PCREL20_S2:
  case LoongArchMCExpr::VK_TLS_GD_PCREL20_S2:
  case LoongArchMCExpr::VK_TLS_DESC_PCREL20_S2:
    if (auto *SA = Target.getAddSym()) 2 refs
      cast<MCSymbolELF>(SA)->setType(ELF::STT_TLS);
    break;
  default:
    break;
  }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions