Skip to content

Commit 118ceea

Browse files
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.
1 parent 197b7b2 commit 118ceea

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

lld/ELF/OutputSections.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/Support/MD5.h"
2121
#include "llvm/Support/MathExtras.h"
2222
#include "llvm/Support/SHA1.h"
23+
#include <regex>
2324

2425
using namespace llvm;
2526
using namespace llvm::dwarf;
@@ -384,18 +385,23 @@ void OutputSection::finalize() {
384385
flags |= SHF_INFO_LINK;
385386
}
386387

387-
// Returns true if S matches /Filename.?\.o$/.
388-
static bool isCrtBeginEnd(StringRef s, StringRef filename) {
389-
if (!s.endswith(".o"))
390-
return false;
391-
s = s.drop_back(2);
392-
if (s.endswith(filename))
393-
return true;
394-
return !s.empty() && s.drop_back().endswith(filename);
388+
// Returns true if S is in one of the many forms the compiler driver may pass
389+
// crtbegin files.
390+
//
391+
// Gcc uses any of crtbegin[<empty>|S|T].o.
392+
// Clang uses Gcc's plus clang_rt.crtbegin[<empty>|S|T][-<arch>|<empty>].o.
393+
394+
static bool isCrtbegin(StringRef s) {
395+
static std::regex re(R"((clang_rt\.)?crtbegin[ST]?(-.*)?\.o)");
396+
s = sys::path::filename(s);
397+
return std::regex_match(s.begin(), s.end(), re);
395398
}
396399

397-
static bool isCrtbegin(StringRef s) { return isCrtBeginEnd(s, "crtbegin"); }
398-
static bool isCrtend(StringRef s) { return isCrtBeginEnd(s, "crtend"); }
400+
static bool isCrtend(StringRef s) {
401+
static std::regex re(R"((clang_rt\.)?crtend[ST]?(-.*)?\.o)");
402+
s = sys::path::filename(s);
403+
return std::regex_match(s.begin(), s.end(), re);
404+
}
399405

400406
// .ctors and .dtors are sorted by this priority from highest to lowest.
401407
//

lld/test/ELF/ctors_dtors_priority.s

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@
33
// Test .ctors* and .dtors* are sorted by priority.
44

55
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
6+
// RUN: mkdir -p %t
67
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
7-
// RUN: %p/Inputs/ctors_dtors_priority1.s -o %t-crtbegin.o
8+
// RUN: %p/Inputs/ctors_dtors_priority1.s -o %t/crtbegin.o
89
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
910
// RUN: %p/Inputs/ctors_dtors_priority2.s -o %t2
1011
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
11-
// RUN: %p/Inputs/ctors_dtors_priority3.s -o %t-crtend.o
12-
// RUN: ld.lld %t1 %t2 %t-crtend.o %t-crtbegin.o -o %t.exe
12+
// RUN: %p/Inputs/ctors_dtors_priority3.s -o %t/crtend.o
13+
// RUN: ld.lld %t1 %t2 %t/crtend.o %t/crtbegin.o -o %t.exe
1314
// RUN: llvm-objdump -s %t.exe | FileCheck %s
1415

16+
// RUN: cp %t/crtbegin.o %t/clang_rt.crtbegin.o
17+
// RUN: cp %t/crtend.o %t/clang_rt.crtend.o
18+
// RUN: ld.lld %t1 %t2 %t/clang_rt.crtend.o %t/clang_rt.crtbegin.o -o %t.clang_rt.exe
19+
// RUN: llvm-objdump -s %t.clang_rt.exe | FileCheck %s
20+
21+
// RUN: cp %t/crtbegin.o %t/clang_rt.crtbegin-x86_64.o
22+
// RUN: cp %t/crtend.o %t/clang_rt.crtend-x86_64.o
23+
// 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
24+
// RUN: llvm-objdump -s %t.clang_rt-arch.exe | FileCheck %s
25+
26+
1527
.globl _start
1628
_start:
1729
nop

0 commit comments

Comments
 (0)