[llvm] a43bf8a - [JITLink][ELF] Add error checks to the ELFLinkGraphBuilder.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 29 13:56:08 PDT 2023


Author: Lang Hames
Date: 2023-08-29T13:55:03-07:00
New Revision: a43bf8a8898cac8cc9648327f27f948e59a94c13

URL: https://github.com/llvm/llvm-project/commit/a43bf8a8898cac8cc9648327f27f948e59a94c13
DIFF: https://github.com/llvm/llvm-project/commit/a43bf8a8898cac8cc9648327f27f948e59a94c13.diff

LOG: [JITLink][ELF] Add error checks to the ELFLinkGraphBuilder.

We now check for:
1. Duplicate definitions of the same section name with different permissions.
2. Symbols whose size extends past the end of the containing block.

It's not clear to me whether either of these is strictly prohibited by the ELF
spec, but they seem pathalogical and JITLink doesn't currently handle them. For
now we'll make them recoverable errors so that if we do encounter these in the
wild we can report the issue(s).

Added: 
    

Modified: 
    llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h b/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h
index a2958d16c50a44..f221497b5efe4d 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h
+++ b/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h
@@ -374,7 +374,14 @@ template <typename ELFT> Error ELFLinkGraphBuilder<ELFT>::graphifySections() {
       }
     }
 
-    assert(GraphSec->getMemProt() == Prot && "MemProt should match");
+    if (GraphSec->getMemProt() != Prot) {
+      std::string ErrMsg;
+      raw_string_ostream(ErrMsg)
+          << "In " << G->getName() << ", section " << *Name
+          << " is present more than once with 
diff erent permissions: "
+          << GraphSec->getMemProt() << " vs " << Prot;
+      return make_error<JITLinkError>(std::move(ErrMsg));
+    }
 
     Block *B = nullptr;
     if (Sec.sh_type != ELF::SHT_NOBITS) {
@@ -499,6 +506,22 @@ template <typename ELFT> Error ELFLinkGraphBuilder<ELFT>::graphifySymbols() {
         TargetFlagsType Flags = makeTargetFlags(Sym);
         orc::ExecutorAddrDiff Offset = getRawOffset(Sym, Flags);
 
+        if (Offset + Sym.st_size > B->getSize()) {
+          std::string ErrMsg;
+          raw_string_ostream ErrStream(ErrMsg);
+          ErrStream << "In " << G->getName() << ", symbol ";
+          if (!Name->empty())
+            ErrStream << *Name;
+          else
+            ErrStream << "<anon>";
+          ErrStream << " (" << (B->getAddress() + Offset) << " -- "
+                    << (B->getAddress() + Offset + Sym.st_size) << ") extends "
+                    << formatv("{0:x}", Offset + Sym.st_size - B->getSize())
+                    << " bytes past the end of its containing block ("
+                    << B->getRange() << ")";
+          return make_error<JITLinkError>(std::move(ErrMsg));
+        }
+
         // In RISCV, temporary symbols (Used to generate dwarf, eh_frame
         // sections...) will appear in object code's symbol table, and LLVM does
         // not use names on these temporary symbols (RISCV gnu toolchain uses


        


More information about the llvm-commits mailing list