[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 09:03:07 PDT 2021


w2yehia created this revision.
Herald added a reviewer: deadalnix.
Herald added subscribers: ormris, steven_wu, hiraditya, inglorion.
w2yehia requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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,15 @@
 Expected<uint32_t> LTOModule::getMachOCPUSubType() const {
   return MachO::getCPUSubType(Triple(Mod->getTargetTriple()));
 }
+
+bool LTOModule::hasCtorDtor() const {
+  for (auto Sym : SymTab.symbols()) {
+    auto *GV = Sym.dyn_cast<GlobalValue *>();
+    if (GV->getName().startswith("llvm.global_")) {
+      StringRef Suffix = GV->getName().drop_front(sizeof("llvm.global_") - 1);
+      if (Suffix.equals("ctors") || Suffix.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.362056.patch
Type: text/x-patch
Size: 2795 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210727/e671b572/attachment.bin>


More information about the llvm-commits mailing list