[lld] 118ceea - Crt files are special cased by name when dealing with ctor and dtor

Sterling Augustine via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 25 11:06:55 PDT 2019


Author: Sterling Augustine
Date: 2019-10-25T11:04:56-07:00
New Revision: 118ceea5c364bd69c52b2a24acd543c28fb35fcb

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

LOG: Crt files are special cased by name when dealing with ctor and dtor
sections, but the current code misses certain variants. In particular, those
named when clang takes the code path in
clang/lib/Driver/ToolChain.cpp:416, where crtfiles are named:

clang_rt.<component>-<arch>-<env>.<suffix>

Previously, the code only handled:
clang_rt.<component>.<suffix>
<component>.<suffix>

This revision fixes that.

Added: 
    

Modified: 
    lld/ELF/OutputSections.cpp
    lld/test/ELF/ctors_dtors_priority.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index ea7c96eb676a..d735456310b9 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -20,6 +20,7 @@
 #include "llvm/Support/MD5.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/SHA1.h"
+#include <regex>
 
 using namespace llvm;
 using namespace llvm::dwarf;
@@ -384,18 +385,23 @@ void OutputSection::finalize() {
   flags |= SHF_INFO_LINK;
 }
 
-// Returns true if S matches /Filename.?\.o$/.
-static bool isCrtBeginEnd(StringRef s, StringRef filename) {
-  if (!s.endswith(".o"))
-    return false;
-  s = s.drop_back(2);
-  if (s.endswith(filename))
-    return true;
-  return !s.empty() && s.drop_back().endswith(filename);
+// Returns true if S is in one of the many forms the compiler driver may pass
+// crtbegin files.
+//
+// Gcc uses any of crtbegin[<empty>|S|T].o.
+// Clang uses Gcc's plus clang_rt.crtbegin[<empty>|S|T][-<arch>|<empty>].o.
+
+static bool isCrtbegin(StringRef s) {
+  static std::regex re(R"((clang_rt\.)?crtbegin[ST]?(-.*)?\.o)");
+  s = sys::path::filename(s);
+  return std::regex_match(s.begin(), s.end(), re);
 }
 
-static bool isCrtbegin(StringRef s) { return isCrtBeginEnd(s, "crtbegin"); }
-static bool isCrtend(StringRef s) { return isCrtBeginEnd(s, "crtend"); }
+static bool isCrtend(StringRef s) {
+  static std::regex re(R"((clang_rt\.)?crtend[ST]?(-.*)?\.o)");
+  s = sys::path::filename(s);
+  return std::regex_match(s.begin(), s.end(), re);
+}
 
 // .ctors and .dtors are sorted by this priority from highest to lowest.
 //

diff  --git a/lld/test/ELF/ctors_dtors_priority.s b/lld/test/ELF/ctors_dtors_priority.s
index 15fde3f63b3c..f68657fa3509 100644
--- a/lld/test/ELF/ctors_dtors_priority.s
+++ b/lld/test/ELF/ctors_dtors_priority.s
@@ -3,15 +3,27 @@
 // Test .ctors* and .dtors* are sorted by priority.
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
+// RUN: mkdir -p %t
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
-// RUN:   %p/Inputs/ctors_dtors_priority1.s -o %t-crtbegin.o
+// RUN:   %p/Inputs/ctors_dtors_priority1.s -o %t/crtbegin.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
 // RUN:   %p/Inputs/ctors_dtors_priority2.s -o %t2
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
-// RUN:   %p/Inputs/ctors_dtors_priority3.s -o %t-crtend.o
-// RUN: ld.lld %t1 %t2 %t-crtend.o %t-crtbegin.o -o %t.exe
+// RUN:   %p/Inputs/ctors_dtors_priority3.s -o %t/crtend.o
+// RUN: ld.lld %t1 %t2 %t/crtend.o %t/crtbegin.o -o %t.exe
 // RUN: llvm-objdump -s %t.exe | FileCheck %s
 
+// RUN: cp %t/crtbegin.o %t/clang_rt.crtbegin.o
+// RUN: cp %t/crtend.o %t/clang_rt.crtend.o
+// RUN: ld.lld %t1 %t2 %t/clang_rt.crtend.o %t/clang_rt.crtbegin.o -o %t.clang_rt.exe
+// RUN: llvm-objdump -s %t.clang_rt.exe | FileCheck %s
+
+// RUN: cp %t/crtbegin.o %t/clang_rt.crtbegin-x86_64.o
+// RUN: cp %t/crtend.o %t/clang_rt.crtend-x86_64.o
+// RUN: ld.lld %t1 %t2 %t/clang_rt.crtend-x86_64.o %t/clang_rt.crtbegin-x86_64.o -o %t.clang_rt-arch.exe
+// RUN: llvm-objdump -s %t.clang_rt-arch.exe | FileCheck %s
+
+	
 .globl _start
 _start:
   nop


        


More information about the llvm-commits mailing list