[lld] Revert "[LLD] Add support for statically resolved vendor-specific RISCV relocations. (#169273)" (PR #181336)

via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 13 01:10:06 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld

Author: Fangrui Song (MaskRay)

<details>
<summary>Changes</summary>

This reverts commit 0c6d7a40187e5e6cbdff1cf5dbdb6fe91054bef4 and
follow-up 7dfa132936a89a966befb6045f306cb9905c6dab.

It landed prematurely with multiple issues with the implementation and
test.


---
Full diff: https://github.com/llvm/llvm-project/pull/181336.diff


4 Files Affected:

- (modified) lld/ELF/Arch/RISCV.cpp (+8-38) 
- (removed) lld/ELF/Arch/RISCVInternalRelocations.h (-113) 
- (modified) lld/ELF/Target.cpp (-9) 
- (modified) lld/test/ELF/riscv-vendor-relocations.s (+5-12) 


``````````diff
diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index 597ab602d3bd0..bf32d97e42a6d 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -8,7 +8,6 @@
 
 #include "InputFiles.h"
 #include "OutputSections.h"
-#include "RISCVInternalRelocations.h"
 #include "RelocScan.h"
 #include "Symbols.h"
 #include "SyntheticSections.h"
@@ -345,15 +344,8 @@ RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s,
   case R_RISCV_SUB_ULEB128:
     return RE_RISCV_LEB128;
   default:
-    if (type.v & INTERNAL_RISCV_VENDOR_MASK) {
-      Err(ctx) << getErrorLoc(ctx, loc)
-               << "unsupported vendor-specific relocation " << type
-               << " against symbol " << &s;
-      return R_NONE;
-    }
-    Err(ctx) << getErrorLoc(ctx, loc) << "unknown relocation ("
-             << (type.v & ~INTERNAL_RISCV_VENDOR_MASK) << ") against symbol "
-             << &s;
+    Err(ctx) << getErrorLoc(ctx, loc) << "unknown relocation (" << type.v
+             << ") against symbol " << &s;
     return R_NONE;
   }
 }
@@ -871,7 +863,7 @@ static bool relax(Ctx &ctx, int pass, InputSection &sec) {
 
   std::fill_n(aux.relocTypes.get(), relocs.size(), R_RISCV_NONE);
   aux.writes.clear();
-  for (auto [i, r] : llvm::enumerate(riscv_vendor_relocs(relocs))) {
+  for (auto [i, r] : llvm::enumerate(relocs)) {
     const uint64_t loc = secAddr + r.offset - delta;
     uint32_t &cur = aux.relocDeltas[i], remove = 0;
     switch (r.type) {
@@ -1515,19 +1507,12 @@ void RISCV::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
       rvVendor = sym.getName();
       continue;
     } else if (!rvVendor.empty()) {
-      uint32_t VendorFlag = getRISCVVendorRelMarker(rvVendor);
-      if (!VendorFlag) {
-        Err(ctx) << getErrorLoc(ctx, loc)
-                 << "unknown vendor-specific relocation (" << type.v
-                 << ") in namespace '" << rvVendor << "' against symbol '"
-                 << &sym << "'";
-        rvVendor = "";
-        continue;
-      }
-
+      Err(ctx) << getErrorLoc(ctx, loc)
+               << "unknown vendor-specific relocation (" << type.v
+               << ") in namespace '" << rvVendor << "' against symbol '" << &sym
+               << "'";
       rvVendor = "";
-      assert((type.v < 256) && "Out of range relocation detected!");
-      type.v |= VendorFlag;
+      continue;
     }
 
     rs.scan<ELFT, RelTy>(it, type, rs.getAddend<ELFT>(*it, type));
@@ -1547,18 +1532,3 @@ void RISCV::scanSection(InputSectionBase &sec) {
   else
     elf::scanSection1<RISCV, ELF32LE>(*this, sec);
 }
-
-uint32_t elf::getRISCVVendorRelMarker(StringRef rvVendor) {
-  return StringSwitch<uint32_t>(rvVendor)
-      .Case("QUALCOMM", INTERNAL_RISCV_VENDOR_QUALCOMM)
-      .Case("ANDES", INTERNAL_RISCV_VENDOR_ANDES)
-      .Default(0);
-}
-
-std::optional<StringRef> elf::getRISCVVendorString(RelType ty) {
-  if ((ty.v & INTERNAL_RISCV_VENDOR_MASK) == INTERNAL_RISCV_VENDOR_QUALCOMM)
-    return "QUALCOMM";
-  if ((ty.v & INTERNAL_RISCV_VENDOR_MASK) == INTERNAL_RISCV_VENDOR_ANDES)
-    return "ANDES";
-  return std::nullopt;
-}
diff --git a/lld/ELF/Arch/RISCVInternalRelocations.h b/lld/ELF/Arch/RISCVInternalRelocations.h
deleted file mode 100644
index 35e2f53b03b35..0000000000000
--- a/lld/ELF/Arch/RISCVInternalRelocations.h
+++ /dev/null
@@ -1,113 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLD_ELF_ARCH_RISCVINTERNALRELOCATIONS_H
-#define LLD_ELF_ARCH_RISCVINTERNALRELOCATIONS_H
-
-#include "Relocations.h"
-#include "Symbols.h"
-
-namespace lld::elf {
-
-// Bit 8 of RelType is used to indicate linker-internal relocations that are
-// not vendor-specific.
-// These are internal relocation numbers for GP/X0 relaxation. They aren't part
-// of the psABI spec.
-constexpr uint32_t INTERNAL_R_RISCV_GPREL_I = 256;
-constexpr uint32_t INTERNAL_R_RISCV_GPREL_S = 257;
-constexpr uint32_t INTERNAL_R_RISCV_X0REL_I = 258;
-constexpr uint32_t INTERNAL_R_RISCV_X0REL_S = 259;
-
-// Bits 9 -> 31 of RelType are used to indicate vendor-specific relocations.
-constexpr uint32_t INTERNAL_RISCV_VENDOR_MASK = 0xFFFFFFFF << 9;
-constexpr uint32_t INTERNAL_RISCV_VENDOR_QUALCOMM = 1 << 9;
-constexpr uint32_t INTERNAL_RISCV_VENDOR_ANDES = 2 << 9;
-
-constexpr uint32_t INTERNAL_RISCV_QC_ABS20_U =
-    INTERNAL_RISCV_VENDOR_QUALCOMM | llvm::ELF::R_RISCV_QC_ABS20_U;
-constexpr uint32_t INTERNAL_RISCV_QC_E_BRANCH =
-    INTERNAL_RISCV_VENDOR_QUALCOMM | llvm::ELF::R_RISCV_QC_E_BRANCH;
-constexpr uint32_t INTERNAL_RISCV_QC_E_32 =
-    INTERNAL_RISCV_VENDOR_QUALCOMM | llvm::ELF::R_RISCV_QC_E_32;
-constexpr uint32_t INTERNAL_RISCV_QC_E_CALL_PLT =
-    INTERNAL_RISCV_VENDOR_QUALCOMM | llvm::ELF::R_RISCV_QC_E_CALL_PLT;
-
-constexpr uint32_t INTERNAL_RISCV_NDS_BRANCH_10 =
-    INTERNAL_RISCV_VENDOR_ANDES | llvm::ELF::R_RISCV_NDS_BRANCH_10;
-
-uint32_t getRISCVVendorRelMarker(llvm::StringRef rvVendor);
-std::optional<llvm::StringRef> getRISCVVendorString(RelType ty);
-
-class vendor_reloc_iterator {
-public:
-  using iterator_category = std::forward_iterator_tag;
-  using value_type = Relocation;
-  using difference_type = std::ptrdiff_t;
-  using pointer = Relocation *;
-  using reference = Relocation; // returned by value
-
-  vendor_reloc_iterator(MutableArrayRef<Relocation>::iterator i,
-                        MutableArrayRef<Relocation>::iterator e)
-      : it(i), end(e) {}
-
-  // Dereference
-  Relocation operator*() const {
-    Relocation r = *it;
-    r.type.v |= rvVendorFlag;
-    return r;
-  }
-
-  struct vendor_reloc_proxy {
-    Relocation r;
-    const Relocation *operator->() const { return &r; }
-  };
-
-  vendor_reloc_proxy operator->() const {
-    return vendor_reloc_proxy{this->operator*()};
-  }
-
-  vendor_reloc_iterator &operator++() {
-    ++it;
-    if (it != end && it->type == llvm::ELF::R_RISCV_VENDOR) {
-      rvVendorFlag = getRISCVVendorRelMarker(it->sym->getName());
-      ++it;
-    } else {
-      rvVendorFlag = 0;
-    }
-    return *this;
-  }
-
-  vendor_reloc_iterator operator++(int) {
-    vendor_reloc_iterator tmp(*this);
-    ++(*this);
-    return tmp;
-  }
-
-  bool operator==(const vendor_reloc_iterator &other) const {
-    return it == other.it;
-  }
-  bool operator!=(const vendor_reloc_iterator &other) const {
-    return it != other.it;
-  }
-
-  Relocation *getUnderlyingRelocation() const { return &*it; }
-
-private:
-  MutableArrayRef<Relocation>::iterator it;
-  MutableArrayRef<Relocation>::iterator end;
-  uint32_t rvVendorFlag = 0;
-};
-
-inline auto riscv_vendor_relocs(MutableArrayRef<Relocation> arr) {
-  return llvm::make_range(vendor_reloc_iterator(arr.begin(), arr.end()),
-                          vendor_reloc_iterator(arr.end(), arr.end()));
-}
-
-} // namespace lld::elf
-
-#endif
diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp
index 3fc3e3f16e9e0..89e4dbeed3109 100644
--- a/lld/ELF/Target.cpp
+++ b/lld/ELF/Target.cpp
@@ -24,7 +24,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "Target.h"
-#include "Arch/RISCVInternalRelocations.h"
 #include "InputFiles.h"
 #include "OutputSections.h"
 #include "RelocScan.h"
@@ -41,14 +40,6 @@ using namespace lld::elf;
 
 std::string elf::toStr(Ctx &ctx, RelType type) {
   StringRef s = getELFRelocationTypeName(ctx.arg.emachine, type);
-  if (ctx.arg.emachine == EM_RISCV && s == "Unknown") {
-    auto VendorString = getRISCVVendorString(type);
-    if (VendorString)
-      s = getRISCVVendorRelocationTypeName(type & ~INTERNAL_RISCV_VENDOR_MASK,
-                                           *VendorString);
-    if (s == "Unknown")
-      return ("Unknown vendor-specific (" + Twine(type) + ")").str();
-  }
   if (s == "Unknown")
     return ("Unknown (" + Twine(type) + ")").str();
   return std::string(s);
diff --git a/lld/test/ELF/riscv-vendor-relocations.s b/lld/test/ELF/riscv-vendor-relocations.s
index 88825756797b7..b0f3c4a30d060 100644
--- a/lld/test/ELF/riscv-vendor-relocations.s
+++ b/lld/test/ELF/riscv-vendor-relocations.s
@@ -1,6 +1,6 @@
 # REQUIRES: riscv
 # RUN: llvm-mc -triple riscv32 %s -filetype=obj -o %t.o
-# RUN: not ld.lld -pie %t.o -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=error:
+# RUN: not ld.lld -pie %t.o -o /dev/null 2>&1 | FileCheck %s
 
   .option exact
 
@@ -8,19 +8,12 @@
 TARGET:
   nop
 
-.local INVALID_VENDOR
-.local QUALCOMM
-.local ANDES
+.global INVALID_VENDOR
 .reloc 1f, R_RISCV_VENDOR, INVALID_VENDOR+0
 .reloc 1f, R_RISCV_VENDOR, INVALID_VENDOR+0
 .reloc 1f, R_RISCV_CUSTOM255, TARGET
-# CHECK: error: {{.*}}:(.text+0x4): malformed consecutive R_RISCV_VENDOR relocations
-# CHECK: error: {{.*}}:(.text+0x4): unknown vendor-specific relocation (255) in namespace 'INVALID_VENDOR' against symbol 'TARGET'
-.reloc 1f, R_RISCV_VENDOR, QUALCOMM+0
-.reloc 1f, R_RISCV_CUSTOM192, TARGET
-# CHECK: error: {{.*}}:(.text+0x4): unsupported vendor-specific relocation R_RISCV_QC_ABS20_U against symbol TARGET
-.reloc 1f, R_RISCV_VENDOR, ANDES+0
-.reloc 1f, R_RISCV_CUSTOM241, TARGET
-# CHECK: error: {{.*}}:(.text+0x4): unsupported vendor-specific relocation R_RISCV_NDS_BRANCH_10 against symbol TARGET
 1:
   nop
+
+# CHECK: error: {{.*}}:(.text+0x4): malformed consecutive R_RISCV_VENDOR relocations
+# CHECK: error: {{.*}}:(.text+0x4): unknown vendor-specific relocation (255) in namespace 'INVALID_VENDOR' against symbol 'TARGET'

``````````

</details>


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


More information about the llvm-commits mailing list