[PATCH] D106887: [LTO][Legacy] Add new API to check presence of ctor/dtor functions

wael yehia via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 27 10:16:30 PDT 2021


w2yehia updated this revision to Diff 362083.
w2yehia added a comment.

address code review comment.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106887/new/

https://reviews.llvm.org/D106887

Files:
  llvm/include/llvm-c/lto.h
  llvm/include/llvm/LTO/legacy/LTOModule.h
  llvm/lib/LTO/LTOModule.cpp
  llvm/tools/lto/lto.cpp
  llvm/tools/lto/lto.exports


Index: llvm/tools/lto/lto.exports
===================================================================
--- llvm/tools/lto/lto.exports
+++ llvm/tools/lto/lto.exports
@@ -8,6 +8,7 @@
 lto_module_create_from_memory_with_path
 lto_module_create_in_local_context
 lto_module_create_in_codegen_context
+lto_module_has_ctor_dtor
 lto_module_get_linkeropts
 lto_module_get_macho_cputype
 lto_module_get_num_symbols
Index: llvm/tools/lto/lto.cpp
===================================================================
--- llvm/tools/lto/lto.cpp
+++ llvm/tools/lto/lto.cpp
@@ -580,6 +580,10 @@
   unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists);
 }
 
+lto_bool_t lto_module_has_ctor_dtor(lto_module_t mod) {
+  return unwrap(mod)->hasCtorDtor();
+}
+
 // ThinLTO API below
 
 thinlto_code_gen_t thinlto_create_codegen(void) {
Index: llvm/lib/LTO/LTOModule.cpp
===================================================================
--- llvm/lib/LTO/LTOModule.cpp
+++ llvm/lib/LTO/LTOModule.cpp
@@ -688,3 +688,16 @@
 Expected<uint32_t> LTOModule::getMachOCPUSubType() const {
   return MachO::getCPUSubType(Triple(Mod->getTargetTriple()));
 }
+
+bool LTOModule::hasCtorDtor() const {
+  for (auto Sym : SymTab.symbols()) {
+    if (auto *GV = Sym.dyn_cast<GlobalValue *>()) {
+      StringRef Name = GV->getName();
+      if (Name.consume_front("llvm.global_")) {
+        if (Name.equals("ctors") || Name.equals("dtors"))
+          return true;
+      }
+    }
+  }
+  return false;
+}
Index: llvm/include/llvm/LTO/legacy/LTOModule.h
===================================================================
--- llvm/include/llvm/LTO/legacy/LTOModule.h
+++ llvm/include/llvm/LTO/legacy/LTOModule.h
@@ -167,6 +167,8 @@
 
   Expected<uint32_t> getMachOCPUSubType() const;
 
+  bool hasCtorDtor() const;
+
 private:
   /// Parse metadata from the module
   // FIXME: it only parses "llvm.linker.options" metadata at the moment
Index: llvm/include/llvm-c/lto.h
===================================================================
--- llvm/include/llvm-c/lto.h
+++ llvm/include/llvm-c/lto.h
@@ -46,7 +46,7 @@
  * @{
  */
 
-#define LTO_API_VERSION 28
+#define LTO_API_VERSION 29
 
 /**
  * \since prior to LTO_API_VERSION=3
@@ -312,6 +312,16 @@
                                                unsigned int *out_cputype,
                                                unsigned int *out_cpusubtype);
 
+/**
+ * This function can be used by the linker to check if a given module has
+ * any constructor or destructor functions.
+ *
+ * Returns true if the module has either the @llvm.global_ctors or the
+ * @llvm.global_dtors symbol. Otherwise returns false.
+ *
+ * \since LTO_API_VERSION=29
+ */
+extern lto_bool_t lto_module_has_ctor_dtor(lto_module_t mod);
 /**
  * Diagnostic severity.
  *


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106887.362083.patch
Type: text/x-patch
Size: 2772 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210727/3c689242/attachment.bin>


More information about the llvm-commits mailing list