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.
This commit is contained in:
Sterling Augustine 2019-10-25 11:04:56 -07:00
parent 197b7b24c3
commit 118ceea5c3
2 changed files with 31 additions and 13 deletions

View File

@ -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.
//

View File

@ -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