Skip to content

Commit 4e096e9

Browse files
merge with main, and resolve code review comments
2 parents f07d34d + 5399782 commit 4e096e9

File tree

837 files changed

+49048
-8290
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

837 files changed

+49048
-8290
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,17 @@ class BinaryContext {
14351435
bool PrintRelocations = false,
14361436
StringRef Endl = "\n") const;
14371437

1438+
/// Print data when embedded in the instruction stream keeping the format
1439+
/// similar to printInstruction().
1440+
void printData(raw_ostream &OS, ArrayRef<uint8_t> Data,
1441+
uint64_t Offset) const;
1442+
1443+
/// Extract data from the binary corresponding to [Address, Address + Size)
1444+
/// range. Return an empty ArrayRef if the address range does not belong to
1445+
/// any section in the binary, crosses a section boundary, or falls into a
1446+
/// virtual section.
1447+
ArrayRef<uint8_t> extractData(uint64_t Address, uint64_t Size) const;
1448+
14381449
/// Print a range of instructions.
14391450
template <typename Itr>
14401451
uint64_t

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,11 @@ class BinaryFunction {
20602060
return Islands ? Islands->getAlignment() : 1;
20612061
}
20622062

2063+
/// If there is a constant island in the range [StartOffset, EndOffset),
2064+
/// return its address.
2065+
std::optional<uint64_t> getIslandInRange(uint64_t StartOffset,
2066+
uint64_t EndOffset) const;
2067+
20632068
uint64_t
20642069
estimateConstantIslandSize(const BinaryFunction *OnBehalfOf = nullptr) const {
20652070
if (!Islands)

bolt/lib/Core/BinaryContext.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1759,7 +1759,11 @@ void BinaryContext::preprocessDebugInfo() {
17591759
dwarf::toString(CU->getUnitDIE().find(dwarf::DW_AT_name), nullptr);
17601760
if (std::optional<uint64_t> DWOID = CU->getDWOId()) {
17611761
auto Iter = DWOCUs.find(*DWOID);
1762-
assert(Iter != DWOCUs.end() && "DWO CU was not found.");
1762+
if (Iter == DWOCUs.end()) {
1763+
this->errs() << "BOLT-ERROR: DWO CU was not found for " << Name
1764+
<< '\n';
1765+
exit(1);
1766+
}
17631767
Name = dwarf::toString(
17641768
Iter->second->getUnitDIE().find(dwarf::DW_AT_name), nullptr);
17651769
}
@@ -1942,6 +1946,43 @@ static void printDebugInfo(raw_ostream &OS, const MCInst &Instruction,
19421946
OS << " discriminator:" << Row.Discriminator;
19431947
}
19441948

1949+
ArrayRef<uint8_t> BinaryContext::extractData(uint64_t Address,
1950+
uint64_t Size) const {
1951+
ArrayRef<uint8_t> Res;
1952+
1953+
const ErrorOr<const BinarySection &> Section = getSectionForAddress(Address);
1954+
if (!Section || Section->isVirtual())
1955+
return Res;
1956+
1957+
if (!Section->containsRange(Address, Size))
1958+
return Res;
1959+
1960+
auto *Bytes =
1961+
reinterpret_cast<const uint8_t *>(Section->getContents().data());
1962+
return ArrayRef<uint8_t>(Bytes + Address - Section->getAddress(), Size);
1963+
}
1964+
1965+
void BinaryContext::printData(raw_ostream &OS, ArrayRef<uint8_t> Data,
1966+
uint64_t Offset) const {
1967+
DataExtractor DE(Data, AsmInfo->isLittleEndian(),
1968+
AsmInfo->getCodePointerSize());
1969+
uint64_t DataOffset = 0;
1970+
while (DataOffset + 4 <= Data.size()) {
1971+
OS << format(" %08" PRIx64 ": \t.word\t0x", Offset + DataOffset);
1972+
const auto Word = DE.getUnsigned(&DataOffset, 4);
1973+
OS << Twine::utohexstr(Word) << '\n';
1974+
}
1975+
if (DataOffset + 2 <= Data.size()) {
1976+
OS << format(" %08" PRIx64 ": \t.short\t0x", Offset + DataOffset);
1977+
const auto Short = DE.getUnsigned(&DataOffset, 2);
1978+
OS << Twine::utohexstr(Short) << '\n';
1979+
}
1980+
if (DataOffset + 1 == Data.size()) {
1981+
OS << format(" %08" PRIx64 ": \t.byte\t0x%x\n", Offset + DataOffset,
1982+
Data[DataOffset]);
1983+
}
1984+
}
1985+
19451986
void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
19461987
uint64_t Offset,
19471988
const BinaryFunction *Function,

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,27 @@ void BinaryFunction::print(raw_ostream &OS, std::string Annotation) {
491491
// Offset of the instruction in function.
492492
uint64_t Offset = 0;
493493

494+
auto printConstantIslandInRange = [&](uint64_t Start, uint64_t End) {
495+
assert(Start <= End && "Invalid range");
496+
std::optional<uint64_t> IslandOffset = getIslandInRange(Start, End);
497+
498+
if (!IslandOffset)
499+
return;
500+
501+
const size_t IslandSize = getSizeOfDataInCodeAt(*IslandOffset);
502+
BC.printData(OS, BC.extractData(getAddress() + *IslandOffset, IslandSize),
503+
*IslandOffset);
504+
};
505+
494506
if (BasicBlocks.empty() && !Instructions.empty()) {
495507
// Print before CFG was built.
508+
uint64_t PrevOffset = 0;
496509
for (const std::pair<const uint32_t, MCInst> &II : Instructions) {
497510
Offset = II.first;
498511

512+
// Print any constant islands inbeetween the instructions.
513+
printConstantIslandInRange(PrevOffset, Offset);
514+
499515
// Print label if exists at this offset.
500516
auto LI = Labels.find(Offset);
501517
if (LI != Labels.end()) {
@@ -506,7 +522,12 @@ void BinaryFunction::print(raw_ostream &OS, std::string Annotation) {
506522
}
507523

508524
BC.printInstruction(OS, II.second, Offset, this);
525+
526+
PrevOffset = Offset;
509527
}
528+
529+
// Print any data at the end of the function.
530+
printConstantIslandInRange(PrevOffset, getMaxSize());
510531
}
511532

512533
StringRef SplitPointMsg = "";
@@ -1048,6 +1069,19 @@ size_t BinaryFunction::getSizeOfDataInCodeAt(uint64_t Offset) const {
10481069
return getSize() - Offset;
10491070
}
10501071

1072+
std::optional<uint64_t>
1073+
BinaryFunction::getIslandInRange(uint64_t StartOffset,
1074+
uint64_t EndOffset) const {
1075+
if (!Islands)
1076+
return std::nullopt;
1077+
1078+
auto Iter = llvm::lower_bound(Islands->DataOffsets, StartOffset);
1079+
if (Iter != Islands->DataOffsets.end() && *Iter < EndOffset)
1080+
return *Iter;
1081+
1082+
return std::nullopt;
1083+
}
1084+
10511085
bool BinaryFunction::isZeroPaddingAt(uint64_t Offset) const {
10521086
ArrayRef<uint8_t> FunctionData = *getData();
10531087
uint64_t EndOfCode = getSize();

bolt/test/AArch64/data-in-code.s

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Check that llvm-bolt prints data embedded in code.
2+
3+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
4+
# RUN: %clang %cflags -fno-PIC -no-pie %t.o -o %t.exe -nostdlib \
5+
# RUN: -fuse-ld=lld -Wl,-q
6+
7+
## Check disassembly of BOLT input.
8+
# RUN: llvm-objdump %t.exe -d | FileCheck %s
9+
10+
# RUN: llvm-bolt %t.exe -o %t.bolt --print-disasm | FileCheck %s
11+
12+
.text
13+
.balign 4
14+
15+
.global _start
16+
.type _start, %function
17+
_start:
18+
mov x0, #0x0
19+
.word 0x4f82e010
20+
ret
21+
.byte 0x0, 0xff, 0x42
22+
# CHECK-LABEL: _start
23+
# CHECK: mov x0, #0x0
24+
# CHECK-NEXT: .word 0x4f82e010
25+
# CHECK-NEXT: ret
26+
# CHECK-NEXT: .short 0xff00
27+
# CHECK-NEXT: .byte 0x42
28+
.size _start, .-_start
29+
30+
## Force relocation mode.
31+
.reloc 0, R_AARCH64_NONE

clang-tools-extra/clang-doc/HTMLGenerator.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,18 +494,31 @@ genReferencesBlock(const std::vector<Reference> &References,
494494
static std::unique_ptr<TagNode>
495495
writeFileDefinition(const Location &L,
496496
std::optional<StringRef> RepositoryUrl = std::nullopt) {
497-
if (!L.IsFileInRootDir || !RepositoryUrl)
497+
if (!L.IsFileInRootDir && !RepositoryUrl)
498498
return std::make_unique<TagNode>(
499499
HTMLTag::TAG_P, "Defined at line " + std::to_string(L.LineNumber) +
500500
" of file " + L.Filename);
501501
SmallString<128> FileURL(*RepositoryUrl);
502-
llvm::sys::path::append(FileURL, llvm::sys::path::Style::posix, L.Filename);
502+
llvm::sys::path::append(
503+
FileURL, llvm::sys::path::Style::posix,
504+
// If we're on Windows, the file name will be in the wrong format, and
505+
// append won't convert the full path being appended to the correct
506+
// format, so we need to do that here.
507+
llvm::sys::path::convert_to_slash(
508+
L.Filename,
509+
// The style here is the current style of the path, not the one we're
510+
// targeting. If the string is already in the posix style, it will do
511+
// nothing.
512+
llvm::sys::path::Style::windows));
503513
auto Node = std::make_unique<TagNode>(HTMLTag::TAG_P);
504514
Node->Children.emplace_back(std::make_unique<TextNode>("Defined at line "));
505515
auto LocNumberNode =
506516
std::make_unique<TagNode>(HTMLTag::TAG_A, std::to_string(L.LineNumber));
507517
// The links to a specific line in the source code use the github /
508518
// googlesource notation so it won't work for all hosting pages.
519+
// FIXME: we probably should have a configuration setting for line number
520+
// rendering in the HTML. For example, GitHub uses #L22, while googlesource
521+
// uses #22 for line numbers.
509522
LocNumberNode->Attributes.emplace_back(
510523
"href", (FileURL + "#" + std::to_string(L.LineNumber)).str());
511524
Node->Children.emplace_back(std::move(LocNumberNode));

clang-tools-extra/test/clang-doc/Inputs/basic-project/src/Circle.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ double Circle::area() const {
88

99
double Circle::perimeter() const {
1010
return 3.141 * radius_;
11-
}
11+
}
12+

0 commit comments

Comments
 (0)