[lld] [lld][ELF] Allow implicit wildcard in archive file name (PR #119293)

Peter Smith via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 12 03:36:51 PST 2024


smithp35 wrote:

I happened to run across the same problem yesterday and didn't realise that this existed https://github.com/llvm/llvm-project/issues/119584 . I started a fix myself which I've attached. It is only work in progress, but uses a slightly different approach. I've pasted it here just in case you want to adopt it.

This also fixes the `:file` syntax which LLD doesn't support either. 

EXCLUDE_FILE can also use the same problem. My plan for that was to extract the MatchType and filePat into a separate struct so that for each EXCLUDE_FILE we could use similar logic.

```
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 7d24c6750b0d..47ec329e5a84 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -407,11 +407,21 @@ static inline StringRef getFilename(const InputFile *file) {
 }

 bool InputSectionDescription::matchesFile(const InputFile *file) const {
-  if (filePat.isTrivialMatchAll())
+  if (matchType != FileOnly && filePat.isTrivialMatchAll())
     return true;

+  StringRef fileName = getFilename(file);
+  if (matchType == FileOnly && !file->archiveName.empty())
+    return false;
+
+  if (matchType == LibOnly) {
+    if (file->archiveName.empty())
+      return false;
+    fileName = file->archiveName;
+  }
+
   if (!matchesFileCache || matchesFileCache->first != file)
-    matchesFileCache.emplace(file, filePat.match(getFilename(file)));
+    matchesFileCache.emplace(file, filePat.match(fileName));

   return matchesFileCache->second;
 }
diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h
index 328368fd3b43..b83feda2bea2 100644
--- a/lld/ELF/LinkerScript.h
+++ b/lld/ELF/LinkerScript.h
@@ -195,7 +195,7 @@ public:

 class InputSectionDescription : public SectionCommand {
   SingleStringMatcher filePat;
-
+  enum MatchType { LibAndFile, FileOnly, LibOnly } matchType = LibAndFile;
   // Cache of the most recent input argument and result of matchesFile().
   mutable std::optional<std::pair<const InputFile *, bool>> matchesFileCache;

@@ -206,6 +206,13 @@ public:
         classRef(classRef), withFlags(withFlags), withoutFlags(withoutFlags) {
     assert((filePattern.empty() || classRef.empty()) &&
            "file pattern and class reference are mutually exclusive");
+    if (filePattern.starts_with(':')) {
+      filePat = filePattern.drop_front();
+      matchType = FileOnly;
+    } else if (filePattern.ends_with(':')) {
+      filePat = filePattern.drop_back();
+      matchType = LibOnly;
+    }
   }

   static bool classof(const SectionCommand *c) {
```

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


More information about the llvm-commits mailing list