Skip to content

Commit 2498281

Browse files
committed
Add bazel targets for libc/include/... tests.
These tests are compiled with `-DLIBC_FULL_BUILD`, since otherwise inclusion of system headers can redefined macros defined by LLVM libc. Also, set `alwayslink=True` for `//libc/test:LibcUnitTest`. This ensures that the `main` function provided always gets linked into a test target. While not strictly necessary, it makes it so tests like https://github.com/llvm/llvm-project/blob/45d8759cbed0f216786729718608a8be72a505c6/libc/test/include/signbit_test.c will give a duplicate symbol error if they incorrectly depend on `LibcUnitTest`.
1 parent 5886f0a commit 2498281

File tree

4 files changed

+451
-41
lines changed

4 files changed

+451
-41
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -780,14 +780,6 @@ void RewriteInstance::discoverFileObjects() {
780780

781781
// For local symbols we want to keep track of associated FILE symbol name for
782782
// disambiguation by combined name.
783-
StringRef FileSymbolName;
784-
bool SeenFileName = false;
785-
struct SymbolRefHash {
786-
size_t operator()(SymbolRef const &S) const {
787-
return std::hash<decltype(DataRefImpl::p)>{}(S.getRawDataRefImpl().p);
788-
}
789-
};
790-
std::unordered_map<SymbolRef, StringRef, SymbolRefHash> SymbolToFileName;
791783
for (const ELFSymbolRef &Symbol : InputFile->symbols()) {
792784
Expected<StringRef> NameOrError = Symbol.getName();
793785
if (NameOrError && NameOrError->starts_with("__asan_init")) {
@@ -806,21 +798,8 @@ void RewriteInstance::discoverFileObjects() {
806798
if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Undefined)
807799
continue;
808800

809-
if (cantFail(Symbol.getType()) == SymbolRef::ST_File) {
801+
if (cantFail(Symbol.getType()) == SymbolRef::ST_File)
810802
FileSymbols.emplace_back(Symbol);
811-
StringRef Name =
812-
cantFail(std::move(NameOrError), "cannot get symbol name for file");
813-
// Ignore Clang LTO artificial FILE symbol as it is not always generated,
814-
// and this uncertainty is causing havoc in function name matching.
815-
if (Name == "ld-temp.o")
816-
continue;
817-
FileSymbolName = Name;
818-
SeenFileName = true;
819-
continue;
820-
}
821-
if (!FileSymbolName.empty() &&
822-
!(cantFail(Symbol.getFlags()) & SymbolRef::SF_Global))
823-
SymbolToFileName[Symbol] = FileSymbolName;
824803
}
825804

826805
// Sort symbols in the file by value. Ignore symbols from non-allocatable
@@ -1028,14 +1007,14 @@ void RewriteInstance::discoverFileObjects() {
10281007
// The <id> field is used for disambiguation of local symbols since there
10291008
// could be identical function names coming from identical file names
10301009
// (e.g. from different directories).
1031-
std::string AltPrefix;
1032-
auto SFI = SymbolToFileName.find(Symbol);
1033-
if (SymbolType == SymbolRef::ST_Function && SFI != SymbolToFileName.end())
1034-
AltPrefix = Name + "/" + std::string(SFI->second);
1010+
auto SFI = llvm::upper_bound(FileSymbols, ELFSymbolRef(Symbol));
1011+
if (SymbolType == SymbolRef::ST_Function && SFI != FileSymbols.begin()) {
1012+
StringRef FileSymbolName = cantFail(SFI[-1].getName());
1013+
if (!FileSymbolName.empty())
1014+
AlternativeName = NR.uniquify(Name + "/" + FileSymbolName.str());
1015+
}
10351016

10361017
UniqueName = NR.uniquify(Name);
1037-
if (!AltPrefix.empty())
1038-
AlternativeName = NR.uniquify(AltPrefix);
10391018
}
10401019

10411020
uint64_t SymbolSize = ELFSymbolRef(Symbol).getSize();
@@ -1294,7 +1273,7 @@ void RewriteInstance::discoverFileObjects() {
12941273
FDE->getAddressRange());
12951274
}
12961275

1297-
BC->setHasSymbolsWithFileName(SeenFileName);
1276+
BC->setHasSymbolsWithFileName(FileSymbols.size());
12981277

12991278
// Now that all the functions were created - adjust their boundaries.
13001279
adjustFunctionBoundaries();

utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ libc_test_library(
7272
"//libc:llvm_libc_macros_stdfix_macros",
7373
"//llvm:Support",
7474
],
75+
# Force linking in this library's `main()` to surface
76+
# a duplicate symbol error if a test defines its own main.
77+
alwayslink = True,
7578
)
7679

7780
libc_test_library(

0 commit comments

Comments
 (0)