[lld] lld: add support for NOCROSSREFS(_TO) (PR #95714)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 12 15:52:44 PDT 2024


================
@@ -2358,3 +2358,45 @@ template void elf::scanRelocations<ELF32LE>();
 template void elf::scanRelocations<ELF32BE>();
 template void elf::scanRelocations<ELF64LE>();
 template void elf::scanRelocations<ELF64BE>();
+
+static void forEachAllocInputSectionDescription(
+    ArrayRef<OutputSection *> outputSections,
+    llvm::function_ref<void(OutputSection *, InputSectionDescription *)> fn) {
+  for (OutputSection *os : outputSections) {
+    if (!(os->flags & SHF_ALLOC))
+      continue;
+    for (SectionCommand *bc : os->commands)
+      if (auto *isd = dyn_cast<InputSectionDescription>(bc))
+        fn(os, isd);
+  }
+}
+
+static void checkSectionNoCrossRefs(OutputSection *outSec,
+                                    InputSectionDescription *inSecDescr) {
+  for (const auto &list : script->noCrossRefLists) {
+    if (!list.matchesRefFromSection(outSec))
+      continue;
+
+    for (const auto &inSection : inSecDescr->sections) {
+      for (const auto &relocation : inSection->relocations) {
+        auto *destOutSec = relocation.sym->getOutputSection();
+        if (!destOutSec)
+          continue;
+
+        // Relocations from section to itself are allowed.
+        if (destOutSec->name == outSec->name ||
+            !list.matchesRefToSection(destOutSec))
+          continue;
+
+        error(inSection->getLocation(relocation.offset) +
----------------
MaskRay wrote:

In my branch I'll use this:
```
+      std::string toSymName;
+      if (!r.sym->isSection())
+        toSymName = toString(*r.sym);
+      else if (auto *d = dyn_cast<Defined>(r.sym))
+        toSymName = d->section->name;
+      errorOrWarn(sec->getLocation(r.offset) +
+                  ": prohibited cross reference to '" + toSymName + "' in " +
+                  dstOsec->name.str());
```

I think `from section` can be omitted since `getLocation` carries the location information.

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


More information about the llvm-commits mailing list