Skip to content

Commit 16e1920

Browse files
committed
LLVM and SPIRV-LLVM-Translator pulldown (WW11)
LLVM: llvm/llvm-project@bb50d47a40f66 SPIRV-LLVM-Translator: KhronosGroup/SPIRV-LLVM-Translator@1538cf5
2 parents 7a8fa1a + 4cc2737 commit 16e1920

File tree

3,979 files changed

+214323
-196246
lines changed

Some content is hidden

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

3,979 files changed

+214323
-196246
lines changed

.github/workflows/llvm-bugs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ jobs:
4848
4949
subject: `[Bug ${issue.data.number}] ${issue.data.title}`,
5050
template: "new-github-issue",
51+
'o:tracking-clicks': 'no',
5152
'h:X-Mailgun-Variables': JSON.stringify(payload)
5253
};
5354

.mailmap

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Combinations of both are possible too, see
1717
# https://git-scm.com/docs/gitmailmap for format details.
1818
#
19-
# You can commit changes for your own names and email addresses without review.
19+
# You can commit changes for your own names and email addresses without review.
2020
# If you want to add entries for other people, please have them review the
2121
# addition.
2222
#

bolt/include/bolt/Core/BinaryFunction.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1299,10 +1299,11 @@ class BinaryFunction {
12991299
case ELF::R_X86_64_32:
13001300
case ELF::R_X86_64_32S:
13011301
case ELF::R_X86_64_64:
1302+
case ELF::R_X86_64_PC8:
1303+
case ELF::R_X86_64_PC32:
1304+
case ELF::R_X86_64_PC64:
13021305
Relocations[Offset] = Relocation{Offset, Symbol, RelType, Addend, Value};
13031306
return;
1304-
case ELF::R_X86_64_PC32:
1305-
case ELF::R_X86_64_PC8:
13061307
case ELF::R_X86_64_PLT32:
13071308
case ELF::R_X86_64_GOTPCRELX:
13081309
case ELF::R_X86_64_REX_GOTPCRELX:

bolt/include/bolt/Core/DebugData.h

+20
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ class DebugInfoBinaryPatcher : public SimpleBinaryPatcher {
490490
PatchBaseClass,
491491
PatchValue32,
492492
PatchValue64to32,
493+
PatchValue32GenericSize,
493494
PatchValue64,
494495
PatchValueVariable,
495496
ReferencePatchValue,
@@ -536,6 +537,22 @@ class DebugInfoBinaryPatcher : public SimpleBinaryPatcher {
536537
uint32_t Value;
537538
};
538539

540+
/// Patch for 4 byte entry, where original entry size is not 4 bytes or 8
541+
/// bytes.
542+
struct DebugPatch32GenericSize : public Patch {
543+
DebugPatch32GenericSize(uint32_t O, uint32_t V, uint32_t OVS)
544+
: Patch(O, DebugPatchKind::PatchValue32GenericSize) {
545+
Value = V;
546+
OldValueSize = OVS;
547+
}
548+
549+
static bool classof(const Patch *Writer) {
550+
return Writer->getKind() == DebugPatchKind::PatchValue32GenericSize;
551+
}
552+
uint32_t Value;
553+
uint32_t OldValueSize;
554+
};
555+
539556
struct DebugPatch64 : public Patch {
540557
DebugPatch64(uint32_t O, uint64_t V)
541558
: Patch(O, DebugPatchKind::PatchValue64) {
@@ -693,6 +710,9 @@ class DebugInfoBinaryPatcher : public SimpleBinaryPatcher {
693710
case DebugPatchKind::PatchValue64to32:
694711
delete reinterpret_cast<DebugPatch64to32 *>(P);
695712
break;
713+
case DebugPatchKind::PatchValue32GenericSize:
714+
delete reinterpret_cast<DebugPatch32GenericSize *>(P);
715+
break;
696716
case DebugPatchKind::PatchValue64:
697717
delete reinterpret_cast<DebugPatch64 *>(P);
698718
break;

bolt/include/bolt/Core/MCPlusBuilder.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,7 @@ class MCPlusBuilder {
424424

425425
/// Return a register number that is guaranteed to not match with
426426
/// any real register on the underlying architecture.
427-
virtual MCPhysReg getNoRegister() const {
428-
llvm_unreachable("not implemented");
429-
}
427+
MCPhysReg getNoRegister() const { return MCRegister::NoRegister; }
430428

431429
/// Return a register corresponding to a function integer argument \p ArgNo
432430
/// if the argument is passed in a register. Or return the result of
@@ -1327,6 +1325,16 @@ class MCPlusBuilder {
13271325
return IndirectBranchType::UNKNOWN;
13281326
}
13291327

1328+
/// Analyze branch \p Instruction in PLT section and try to determine
1329+
/// associated got entry address.
1330+
virtual uint64_t analyzePLTEntry(MCInst &Instruction,
1331+
InstructionIterator Begin,
1332+
InstructionIterator End,
1333+
uint64_t BeginPC) const {
1334+
llvm_unreachable("not implemented");
1335+
return 0;
1336+
}
1337+
13301338
virtual bool analyzeVirtualMethodCall(InstructionIterator Begin,
13311339
InstructionIterator End,
13321340
std::vector<MCInst *> &MethodFetchInsns,

bolt/include/bolt/Core/Relocation.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct Relocation {
4949
/// Used to validate relocation correctness.
5050
uint64_t Value;
5151

52-
/// Return size of the given relocation \p Type.
52+
/// Return size in bytes of the given relocation \p Type.
5353
static size_t getSizeForType(uint64_t Type);
5454

5555
/// Return size of this relocation.

bolt/include/bolt/Rewrite/RewriteInstance.h

+17-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class RewriteInstance {
5656
Error setProfile(StringRef Filename);
5757

5858
/// Run all the necessary steps to read, optimize and rewrite the binary.
59-
void run();
59+
Error run();
6060

6161
/// Diff this instance against another one. Non-const since we may run passes
6262
/// to fold identical functions.
@@ -218,7 +218,7 @@ class RewriteInstance {
218218

219219
/// Detect addresses and offsets available in the binary for allocating
220220
/// new sections.
221-
void discoverStorage();
221+
Error discoverStorage();
222222

223223
/// Adjust function sizes and set proper maximum size values after the whole
224224
/// symbol table has been processed.
@@ -246,6 +246,19 @@ class RewriteInstance {
246246
/// Disassemble and create function entries for PLT.
247247
void disassemblePLT();
248248

249+
/// Auxiliary function to create .plt BinaryFunction on \p EntryAddres
250+
/// with the \p EntrySize size. \p TargetAddress is the .got entry
251+
/// associated address.
252+
void createPLTBinaryFunction(uint64_t TargetAddress, uint64_t EntryAddress,
253+
uint64_t EntrySize);
254+
255+
/// Disassemble aarch64-specific .plt \p Section auxiliary function
256+
void disassemblePLTSectionAArch64(BinarySection &Section);
257+
258+
/// Disassemble X86-specific .plt \p Section auxiliary function. \p EntrySize
259+
/// is the expected .plt \p Section entry function size.
260+
void disassemblePLTSectionX86(BinarySection &Section, uint64_t EntrySize);
261+
249262
/// ELF-specific part. TODO: refactor into new class.
250263
#define ELF_FUNCTION(FUNC) \
251264
template <typename ELFT> void FUNC(object::ELFObjectFile<ELFT> *Obj); \
@@ -473,7 +486,7 @@ class RewriteInstance {
473486
/// multiple variants generated by different linkers.
474487
struct PLTSectionInfo {
475488
const char *Name;
476-
uint64_t EntrySize;
489+
uint64_t EntrySize{0};
477490
};
478491

479492
/// Different types of X86-64 PLT sections.
@@ -485,10 +498,7 @@ class RewriteInstance {
485498
};
486499

487500
/// AArch64 PLT sections.
488-
const PLTSectionInfo AArch64_PLTSections[2] = {
489-
{ ".plt", 16 },
490-
{ nullptr, 0 }
491-
};
501+
const PLTSectionInfo AArch64_PLTSections[2] = {{".plt"}, {nullptr}};
492502

493503
/// Return PLT information for a section with \p SectionName or nullptr
494504
/// if the section is not PLT.

bolt/lib/Core/BinaryFunction.cpp

+71-47
Original file line numberDiff line numberDiff line change
@@ -1265,51 +1265,6 @@ bool BinaryFunction::disassemble() {
12651265
}
12661266
}
12671267

1268-
// Check if there's a relocation associated with this instruction.
1269-
bool UsedReloc = false;
1270-
for (auto Itr = Relocations.lower_bound(Offset),
1271-
ItrE = Relocations.lower_bound(Offset + Size);
1272-
Itr != ItrE; ++Itr) {
1273-
const Relocation &Relocation = Itr->second;
1274-
1275-
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: replacing immediate 0x"
1276-
<< Twine::utohexstr(Relocation.Value)
1277-
<< " with relocation"
1278-
" against "
1279-
<< Relocation.Symbol << "+" << Relocation.Addend
1280-
<< " in function " << *this
1281-
<< " for instruction at offset 0x"
1282-
<< Twine::utohexstr(Offset) << '\n');
1283-
1284-
// Process reference to the primary symbol.
1285-
if (!Relocation.isPCRelative())
1286-
BC.handleAddressRef(Relocation.Value - Relocation.Addend, *this,
1287-
/*IsPCRel*/ false);
1288-
1289-
int64_t Value = Relocation.Value;
1290-
const bool Result = BC.MIB->replaceImmWithSymbolRef(
1291-
Instruction, Relocation.Symbol, Relocation.Addend, Ctx.get(), Value,
1292-
Relocation.Type);
1293-
(void)Result;
1294-
assert(Result && "cannot replace immediate with relocation");
1295-
1296-
// For aarch, if we replaced an immediate with a symbol from a
1297-
// relocation, we mark it so we do not try to further process a
1298-
// pc-relative operand. All we need is the symbol.
1299-
if (BC.isAArch64())
1300-
UsedReloc = true;
1301-
1302-
// Make sure we replaced the correct immediate (instruction
1303-
// can have multiple immediate operands).
1304-
if (BC.isX86()) {
1305-
assert(truncateToSize(static_cast<uint64_t>(Value),
1306-
Relocation::getSizeForType(Relocation.Type)) ==
1307-
truncateToSize(Relocation.Value, Relocation::getSizeForType(
1308-
Relocation.Type)) &&
1309-
"immediate value mismatch in function");
1310-
}
1311-
}
1312-
13131268
if (MIB->isBranch(Instruction) || MIB->isCall(Instruction)) {
13141269
uint64_t TargetAddress = 0;
13151270
if (MIB->evaluateBranch(Instruction, AbsoluteInstrAddr, Size,
@@ -1394,8 +1349,75 @@ bool BinaryFunction::disassemble() {
13941349
if (BC.isAArch64())
13951350
handleAArch64IndirectCall(Instruction, Offset);
13961351
}
1397-
} else if (MIB->hasPCRelOperand(Instruction) && !UsedReloc) {
1398-
handlePCRelOperand(Instruction, AbsoluteInstrAddr, Size);
1352+
} else {
1353+
// Check if there's a relocation associated with this instruction.
1354+
bool UsedReloc = false;
1355+
for (auto Itr = Relocations.lower_bound(Offset),
1356+
ItrE = Relocations.lower_bound(Offset + Size);
1357+
Itr != ItrE; ++Itr) {
1358+
const Relocation &Relocation = Itr->second;
1359+
uint64_t SymbolValue = Relocation.Value - Relocation.Addend;
1360+
if (Relocation.isPCRelative())
1361+
SymbolValue += getAddress() + Relocation.Offset;
1362+
1363+
// Process reference to the symbol.
1364+
if (BC.isX86())
1365+
BC.handleAddressRef(SymbolValue, *this, Relocation.isPCRelative());
1366+
1367+
if (BC.isAArch64() || !Relocation.isPCRelative()) {
1368+
int64_t Value = Relocation.Value;
1369+
const bool Result = BC.MIB->replaceImmWithSymbolRef(
1370+
Instruction, Relocation.Symbol, Relocation.Addend, Ctx.get(),
1371+
Value, Relocation.Type);
1372+
(void)Result;
1373+
assert(Result && "cannot replace immediate with relocation");
1374+
1375+
if (BC.isX86()) {
1376+
// Make sure we replaced the correct immediate (instruction
1377+
// can have multiple immediate operands).
1378+
assert(
1379+
truncateToSize(static_cast<uint64_t>(Value),
1380+
Relocation::getSizeForType(Relocation.Type)) ==
1381+
truncateToSize(Relocation.Value, Relocation::getSizeForType(
1382+
Relocation.Type)) &&
1383+
"immediate value mismatch in function");
1384+
} else if (BC.isAArch64()) {
1385+
// For aarch, if we replaced an immediate with a symbol from a
1386+
// relocation, we mark it so we do not try to further process a
1387+
// pc-relative operand. All we need is the symbol.
1388+
UsedReloc = true;
1389+
}
1390+
} else {
1391+
// Check if the relocation matches memop's Disp.
1392+
uint64_t TargetAddress;
1393+
if (!BC.MIB->evaluateMemOperandTarget(Instruction, TargetAddress,
1394+
AbsoluteInstrAddr, Size)) {
1395+
errs() << "BOLT-ERROR: PC-relative operand can't be evaluated\n";
1396+
exit(1);
1397+
}
1398+
assert(TargetAddress == Relocation.Value + AbsoluteInstrAddr + Size &&
1399+
"Immediate value mismatch detected.");
1400+
1401+
const MCExpr *Expr = MCSymbolRefExpr::create(
1402+
Relocation.Symbol, MCSymbolRefExpr::VK_None, *BC.Ctx);
1403+
// Real addend for pc-relative targets is adjusted with a delta
1404+
// from relocation placement to the next instruction.
1405+
const uint64_t TargetAddend =
1406+
Relocation.Addend + Offset + Size - Relocation.Offset;
1407+
if (TargetAddend) {
1408+
const MCConstantExpr *Offset =
1409+
MCConstantExpr::create(TargetAddend, *BC.Ctx);
1410+
Expr = MCBinaryExpr::createAdd(Expr, Offset, *BC.Ctx);
1411+
}
1412+
BC.MIB->replaceMemOperandDisp(
1413+
Instruction, MCOperand::createExpr(BC.MIB->getTargetExprFor(
1414+
Instruction, Expr, *BC.Ctx, 0)));
1415+
UsedReloc = true;
1416+
}
1417+
}
1418+
1419+
if (MIB->hasPCRelOperand(Instruction) && !UsedReloc)
1420+
handlePCRelOperand(Instruction, AbsoluteInstrAddr, Size);
13991421
}
14001422

14011423
add_instruction:
@@ -1565,6 +1587,8 @@ bool BinaryFunction::scanExternalRefs() {
15651587
ItrE = Relocations.lower_bound(Offset + Size);
15661588
Itr != ItrE; ++Itr) {
15671589
Relocation &Relocation = Itr->second;
1590+
if (Relocation.isPCRelative() && BC.isX86())
1591+
continue;
15681592
if (ignoreReference(Relocation.Symbol))
15691593
continue;
15701594

bolt/lib/Core/DebugData.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,11 @@ void DebugInfoBinaryPatcher::addLE32Patch(uint64_t Offset, uint32_t NewValue,
486486
std::lock_guard<std::mutex> Lock(WriterMutex);
487487
if (OldValueSize == 4)
488488
DebugPatches.emplace_back(new DebugPatch32(Offset, NewValue));
489-
else
489+
else if (OldValueSize == 8)
490490
DebugPatches.emplace_back(new DebugPatch64to32(Offset, NewValue));
491+
else
492+
DebugPatches.emplace_back(
493+
new DebugPatch32GenericSize(Offset, NewValue, OldValueSize));
491494
}
492495

493496
void SimpleBinaryPatcher::addBinaryPatch(uint64_t Offset,
@@ -578,6 +581,12 @@ CUOffsetMap DebugInfoBinaryPatcher::computeNewOffsets(DWARFContext &DWCtx,
578581
PreviousChangeInSize -= 4;
579582
break;
580583
}
584+
case DebugPatchKind::PatchValue32GenericSize: {
585+
DebugPatch32GenericSize *DPVS =
586+
reinterpret_cast<DebugPatch32GenericSize *>(P);
587+
PreviousChangeInSize += 4 - DPVS->OldValueSize;
588+
break;
589+
}
581590
case DebugPatchKind::PatchValueVariable: {
582591
DebugPatchVariableSize *DPV =
583592
reinterpret_cast<DebugPatchVariableSize *>(P);
@@ -691,6 +700,14 @@ std::string DebugInfoBinaryPatcher::patchBinary(StringRef BinaryContents) {
691700
ByteSequence = encodeLE(4, P64to32->Value);
692701
break;
693702
}
703+
case DebugPatchKind::PatchValue32GenericSize: {
704+
DebugPatch32GenericSize *DPVS =
705+
reinterpret_cast<DebugPatch32GenericSize *>(P);
706+
Offset = DPVS->Offset;
707+
OldValueSize = DPVS->OldValueSize;
708+
ByteSequence = encodeLE(4, DPVS->Value);
709+
break;
710+
}
694711
case DebugPatchKind::PatchValueVariable: {
695712
DebugPatchVariableSize *PV =
696713
reinterpret_cast<DebugPatchVariableSize *>(P);

bolt/lib/Core/Relocation.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ uint64_t adjustValueAArch64(uint64_t Type, uint64_t Value, uint64_t PC) {
254254

255255
uint64_t extractValueX86(uint64_t Type, uint64_t Contents, uint64_t PC) {
256256
if (Type == ELF::R_X86_64_32S)
257-
return SignExtend64<32>(Contents & 0xffffffff);
257+
return SignExtend64<32>(Contents);
258+
if (Relocation::isPCRelative(Type))
259+
return SignExtend64(Contents, 8 * Relocation::getSizeForType(Type));
258260
return Contents;
259261
}
260262

@@ -442,6 +444,8 @@ bool isPCRelativeX86(uint64_t Type) {
442444
case ELF::R_X86_64_PC64:
443445
case ELF::R_X86_64_GOTPCREL:
444446
case ELF::R_X86_64_PLT32:
447+
case ELF::R_X86_64_GOTOFF64:
448+
case ELF::R_X86_64_GOTPC32:
445449
case ELF::R_X86_64_GOTTPOFF:
446450
case ELF::R_X86_64_GOTPCRELX:
447451
case ELF::R_X86_64_REX_GOTPCRELX:

0 commit comments

Comments
 (0)