[llvm] [BOLT] Support instrumentation hook via DT_FINI_ARRAY (PR #67348)

Vladislav Khmelevsky via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 4 02:04:09 PDT 2023


================
@@ -1276,6 +1282,68 @@ void RewriteInstance::discoverFileObjects() {
   registerFragments();
 }
 
+Error RewriteInstance::discoverRtFiniAddress() {
+  // If FiniFunctionAddress is already set, we got if from DT_FINI. We use
+  // DT_FINI instead of DT_FINI_ARRAY if it's available.
+  if (BC->FiniFunctionAddress)
+    return Error::success();
+
+  if (!BC->FiniArrayAddress || !BC->FiniArraySize) {
+    return createStringError(
+        std::errc::not_supported,
+        "Instrumentation needs either DT_FINI or DT_FINI_ARRAY");
+  }
+
+  if (*BC->FiniArraySize < BC->AsmInfo->getCodePointerSize()) {
+    return createStringError(std::errc::not_supported,
+                             "Need at least 1 DT_FINI_ARRAY slot");
+  }
+
+  ErrorOr<BinarySection &> FiniArraySection =
+      BC->getSectionForAddress(*BC->FiniArrayAddress);
+  if (auto EC = FiniArraySection.getError())
+    return errorCodeToError(EC);
+
+  BC->FiniArraySection = &*FiniArraySection;
+
+  if (const Relocation *Reloc = FiniArraySection->getDynamicRelocationAt(0)) {
+    BC->FiniFunctionAddress = Reloc->Addend;
+    return Error::success();
+  }
+
+  if (const Relocation *Reloc = FiniArraySection->getRelocationAt(0)) {
+    BC->FiniFunctionAddress = Reloc->Value;
+    return Error::success();
+  }
+
+  return createStringError(std::errc::not_supported,
+                           "No relocation for first DT_FINI_ARRAY slot");
+}
+
+void RewriteInstance::updateRtFiniReloc() {
+  if (!BC->FiniArraySection)
----------------
yota9 wrote:

Thanks for the fix! It seems to be good now. Only the tests seems to left. Just a hint, I would build the single test with 3 different states: only DT_FINI, only DT_FINI_ARRAY, and both for pie and non-pie cases, each time checking the static and if needed dynamic relocs were updated.

>> I believe this is not necessary 

Well it is just extra check that everything is in consistent state, I don't really insist on it, but I would stay with it :)


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


More information about the llvm-commits mailing list