[lld] [LLD][ELF] Don't instantiate ObjFile function but rather import it (PR #68025)

Alexandre Ganea via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 3 06:05:12 PDT 2023


https://github.com/aganea updated https://github.com/llvm/llvm-project/pull/68025

>From 2681a245da33b8692bd74e95731b9532253e0fbb Mon Sep 17 00:00:00 2001
From: Alexandre Ganea <alex_toresh at yahoo.fr>
Date: Mon, 2 Oct 2023 15:46:40 -0400
Subject: [PATCH 1/2] [LLD][ELF] Don't instantiate ObjFile function but rather
 import it

Before this patch, with MSVC I was seeing the following warnings:
```
[304/334] Building CXX object tools\lld\ELF\CMakeFiles\lldELF.dir\InputFiles.cpp.obj
C:\git\llvm-project\lld\ELF\InputFiles.h(327): warning C4661: 'void lld::elf::ObjFile<llvm::object::ELF32LE>::importCmseSymbols(void)': no suitable definition provided for explicit template instantiation request
C:\git\llvm-project\lld\ELF\InputFiles.h(291): note: see declaration of 'lld::elf::ObjFile<llvm::object::ELF32LE>::importCmseSymbols'
C:\git\llvm-project\lld\ELF\InputFiles.h(327): warning C4661: 'void lld::elf::ObjFile<llvm::object::ELF32LE>::redirectCmseSymbols(void)': no suitable definition provided for explicit template instantiation request
C:\git\llvm-project\lld\ELF\InputFiles.h(292): note: see declaration of 'lld::elf::ObjFile<llvm::object::ELF32LE>::redirectCmseSymbols'
```

This patch removes `redirectCmseSymbols` which is not defined. And it imports `importCmseSymbols` in InputFiles.cpp, because it is already explicitly instantiated in ARM.cpp.
---
 lld/ELF/InputFiles.cpp | 7 +++++++
 lld/ELF/InputFiles.h   | 1 -
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index fa0731854689b94..bb86269d49499cd 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -40,6 +40,13 @@ using namespace llvm::support::endian;
 using namespace lld;
 using namespace lld::elf;
 
+// This function is explicity instantiated in ARM.cpp, don't do it here to avoid
+// warnings with MSVC.
+extern template void ObjFile<ELF32LE>::importCmseSymbols();
+extern template void ObjFile<ELF32BE>::importCmseSymbols();
+extern template void ObjFile<ELF64LE>::importCmseSymbols();
+extern template void ObjFile<ELF64BE>::importCmseSymbols();
+
 bool InputFile::isInGroup;
 uint32_t InputFile::nextGroupId;
 
diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index cc658bdc231988e..41c2ba4e307b36b 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -289,7 +289,6 @@ template <class ELFT> class ObjFile : public ELFFileBase {
   void initSectionsAndLocalSyms(bool ignoreComdats);
   void postParse();
   void importCmseSymbols();
-  void redirectCmseSymbols();
 
 private:
   void initializeSections(bool ignoreComdats,

>From c29f0dcf29ccff444c72c14f0fa06886c9bfd373 Mon Sep 17 00:00:00 2001
From: Alexandre Ganea <alex_toresh at yahoo.fr>
Date: Tue, 3 Oct 2023 08:55:30 -0400
Subject: [PATCH 2/2] Move extern declarations closer to the point of usage.

---
 lld/ELF/InputFiles.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index bb86269d49499cd..a2c83adc18f54f0 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -322,6 +322,13 @@ template <class ELFT> static void doParseFile(InputFile *file) {
 // Add symbols in File to the symbol table.
 void elf::parseFile(InputFile *file) { invokeELFT(doParseFile, file); }
 
+// This function is explicity instantiated in ARM.cpp. Mark it extern here,
+// to avoid warnings when building with MSVC.
+extern template void ObjFile<ELF32LE>::importCmseSymbols();
+extern template void ObjFile<ELF32BE>::importCmseSymbols();
+extern template void ObjFile<ELF64LE>::importCmseSymbols();
+extern template void ObjFile<ELF64BE>::importCmseSymbols();
+
 template <class ELFT> static void doParseArmCMSEImportLib(InputFile *file) {
   cast<ObjFile<ELFT>>(file)->importCmseSymbols();
 }



More information about the llvm-commits mailing list