[lld] [wip][LLD] Support RISCV vendor-specific relocations. (PR #168497)

Sam Elliott via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 18 00:56:41 PST 2025


================
@@ -1725,6 +1738,47 @@ void RelocationBaseSection::computeRels() {
       return std::tie(a.r_sym, a.r_offset) < std::tie(b.r_sym, b.r_offset);
     });
   }
+  // Insert R_RISCV_VENDOR relocations very late, so that it doesn't interfere
+  // with relocation sorting above.
+  if (ctx.arg.emachine == EM_RISCV) {
+    SmallVector<DynamicReloc, 0> processedRelocs;
+    processedRelocs.reserve(relocs.size());
+    for (auto reloc : relocs) {
+      auto vendorString = getRISCVVendorString(reloc.type);
+      if (vendorString) {
+        // Symbol *vendorSym = ctx.symtab->find(*vendorString);
+        auto *vendorSym = ctx.symtab->find(*vendorString);
+        if (!vendorSym || !vendorSym->isDefined()) {
+          vendorSym = ctx.symtab->addSymbol(Defined{ctx, nullptr, *vendorString,
+                                                    STB_GLOBAL, STV_HIDDEN,
+                                                    STT_NOTYPE, 0, 0, nullptr});
+          symTab->addSymbol(vendorSym);
+        }
+        vendorSym->isUsedInRegularObj = true;
+        vendorSym->isExported = true;
+        processedRelocs.push_back({llvm::ELF::R_RISCV_VENDOR, reloc.inputSec,
+                                   reloc.offsetInSec, true, *vendorSym, 0,
+                                   R_ABS});
+        processedRelocs.back().finalize(ctx, symTab);
+      }
+
+      reloc.type.v &= ~INTERNAL_RISCV_VENDOR_MASK;
+      processedRelocs.push_back(reloc);
+    }
+
+    relocs = std::move(processedRelocs);
+  }
+}
+
+void RelocationBaseSection::maybeAddRISCVendorRelocation(
+    const DynamicReloc &reloc, SmallVector<DynamicReloc, 0> &outRelocs) {
+  auto riscvVendorString = getRISCVVendorString(reloc.type);
+  if (ctx.arg.emachine == llvm::ELF::EM_RISCV && riscvVendorString) {
+    Symbol &vendorSym = *ctx.symtab->addSymbol(Defined{
+        ctx, ctx.internalFile, *riscvVendorString, llvm::ELF::STB_GLOBAL,
----------------
lenary wrote:

```suggestion
        ctx, ctx.internalFile, *riscvVendorString, llvm::ELF::STB_LOCAL,
```

I think we've said these need to be local, but maybe that's not right for dynamic relocs.

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


More information about the llvm-commits mailing list