1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00

[MC][ELF] Change SHT_LLVM_CALL_GRAPH_PROFILE relocations from SHT_RELA to SHT_REL

... even on targets preferring RELA. The section is only consumed by ld.lld
which can handle REL.

Follow-up to D104080 as I explained in the review. There are two advantages:

* The D104080 code only handles RELA, so arm/i386/mips32 etc may warn for -fprofile-use=/-fprofile-sample-use= usage.
* Decrease object file size for RELA targets

While here, change the relocation to relocate weights, instead of 0,1,2,3,..
I failed to catch the issue during review.
This commit is contained in:
Fangrui Song 2021-06-24 21:35:48 -07:00
parent 8d9d580ae9
commit ba4b6a66c6
6 changed files with 44 additions and 44 deletions

View File

@ -142,7 +142,7 @@ struct ELFWriter {
// TargetObjectWriter wrappers.
bool is64Bit() const;
bool hasRelocationAddend() const;
bool usesRela(const MCSectionELF &Sec) const;
uint64_t align(unsigned Alignment);
@ -392,8 +392,9 @@ bool ELFWriter::is64Bit() const {
return OWriter.TargetObjectWriter->is64Bit();
}
bool ELFWriter::hasRelocationAddend() const {
return OWriter.hasRelocationAddend();
bool ELFWriter::usesRela(const MCSectionELF &Sec) const {
return OWriter.hasRelocationAddend() &&
Sec.getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
}
// Emit the ELF header.
@ -785,11 +786,12 @@ MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
return nullptr;
const StringRef SectionName = Sec.getName();
std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
bool Rela = usesRela(Sec);
std::string RelaSectionName = Rela ? ".rela" : ".rel";
RelaSectionName += SectionName;
unsigned EntrySize;
if (hasRelocationAddend())
if (Rela)
EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
else
EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
@ -799,8 +801,8 @@ MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
Flags = ELF::SHF_GROUP;
MCSectionELF *RelaSection = Ctx.createELFRelSection(
RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
Flags, EntrySize, Sec.getGroup(), &Sec);
RelaSectionName, Rela ? ELF::SHT_RELA : ELF::SHT_REL, Flags, EntrySize,
Sec.getGroup(), &Sec);
RelaSection->setAlignment(is64Bit() ? Align(8) : Align(4));
return RelaSection;
}
@ -925,6 +927,7 @@ void ELFWriter::writeRelocations(const MCAssembler &Asm,
// Sort the relocation entries. MIPS needs this.
OWriter.TargetObjectWriter->sortRelocs(Asm, Relocs);
const bool Rela = usesRela(Sec);
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
const ELFRelocationEntry &Entry = Relocs[e - i - 1];
unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
@ -943,7 +946,7 @@ void ELFWriter::writeRelocations(const MCAssembler &Asm,
ERE64.setSymbolAndType(Index, Entry.Type);
write(ERE64.r_info);
}
if (hasRelocationAddend())
if (Rela)
write(Entry.Addend);
} else {
write(uint32_t(Entry.Offset));
@ -952,7 +955,7 @@ void ELFWriter::writeRelocations(const MCAssembler &Asm,
ERE32.setSymbolAndType(Index, Entry.Type);
write(ERE32.r_info);
if (hasRelocationAddend())
if (Rela)
write(uint32_t(Entry.Addend));
if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {

View File

@ -513,9 +513,10 @@ void MCELFStreamer::finalizeCGProfile() {
SwitchSection(CGProfile);
uint64_t Offset = 0;
for (MCAssembler::CGProfileEntry &E : Asm.CGProfile) {
finalizeCGProfileEntry(E.From, Offset++);
finalizeCGProfileEntry(E.To, Offset++);
finalizeCGProfileEntry(E.From, Offset);
finalizeCGProfileEntry(E.To, Offset);
emitIntValue(E.Count, sizeof(uint64_t));
Offset += sizeof(uint64_t);
}
PopSection();
}

View File

@ -31,30 +31,26 @@ late3:
# CHECK-NEXT: 0010: 14000000 00000000 2A000000 00000000
# CHECK-NEXT: )
# CHECK: Name: .rela.llvm.call-graph-profile (28)
# CHECK-NEXT: Type: SHT_RELA (0x4)
# CHECK: Name: .rel.llvm.call-graph-profile (28)
# CHECK-NEXT: Type: SHT_REL (0x9)
# CHECK-NEXT: Flags [ (0x0)
# CHECK-NEXT: ]
# CHECK-NEXT: Address: 0x0
# CHECK-NEXT: Offset: 0x140
# CHECK-NEXT: Size: 192
# CHECK-NEXT: Size: 128
# CHECK-NEXT: Link: 7
# CHECK-NEXT: Info: 5
# CHECK-NEXT: AddressAlignment: 8
# CHECK-NEXT: EntrySize: 24
# CHECK-NEXT: EntrySize: 16
# CHECK-NEXT: SectionData (
# CHECK-NEXT: 0000: 00000000 00000000 00000000 02000000
# CHECK-NEXT: 0010: 00000000 00000000 01000000 00000000
# CHECK-NEXT: 0020: 00000000 05000000 00000000 00000000
# CHECK-NEXT: 0030: 02000000 00000000 00000000 07000000
# CHECK-NEXT: 0040: 00000000 00000000 03000000 00000000
# CHECK-NEXT: 0050: 00000000 02000000 00000000 00000000
# CHECK-NEXT: 0060: 04000000 00000000 00000000 06000000
# CHECK-NEXT: 0070: 00000000 00000000 05000000 00000000
# CHECK-NEXT: 0080: 00000000 03000000 00000000 00000000
# CHECK-NEXT: 0090: 06000000 00000000 00000000 01000000
# CHECK-NEXT: 00A0: 00000000 00000000 07000000 00000000
# CHECK-NEXT: 00B0: 00000000 05000000 00000000 00000000
# CHECK-NEXT: 0010: 00000000 00000000 00000000 05000000
# CHECK-NEXT: 0020: 08000000 00000000 00000000 07000000
# CHECK-NEXT: 0030: 08000000 00000000 00000000 02000000
# CHECK-NEXT: 0040: 10000000 00000000 00000000 06000000
# CHECK-NEXT: 0050: 10000000 00000000 00000000 03000000
# CHECK-NEXT: 0060: 18000000 00000000 00000000 01000000
# CHECK-NEXT: 0070: 18000000 00000000 00000000 05000000
# CHECK-NEXT: )
# CHECK: Symbols [

View File

@ -34,8 +34,8 @@ Sections:
- Weight: 89
- Weight: 98
EntSize: [[ENTSIZE=<none>]]
- Name: .rela.llvm.call-graph-profile
Type: SHT_RELA
- Name: .rel.llvm.call-graph-profile
Type: SHT_REL
Info: .llvm.call-graph-profile
Relocations:
- Symbol: foo
@ -98,8 +98,8 @@ Sections:
- Weight: 10
- Weight: 20
- Weight: 20
- Name: .rela.llvm.call-graph-profile
Type: SHT_RELA
- Name: .rel.llvm.call-graph-profile
Type: SHT_REL
Info: .llvm.call-graph-profile
Relocations:
- Symbol: 1
@ -186,8 +186,8 @@ Sections:
- Weight: 89
- Weight: 98
EntSize: [[ENTSIZE=<none>]]
- Name: .rela.llvm.call-graph-profile
Type: SHT_RELA
- Name: .rel.llvm.call-graph-profile
Type: SHT_REL
Info: .llvm.call-graph-profile
Relocations:
- Symbol: foo
@ -213,7 +213,7 @@ Symbols:
# RUN: llvm-readobj %t6.o --cg-profile 2>&1 | FileCheck %s -DFILE=%t6.o --check-prefix=LLVM-RELOC-WRONG-SIZE
# RUN: llvm-readobj %t6.o --elf-cg-profile 2>&1 | FileCheck %s -DFILE=%t6.o --check-prefix=LLVM-RELOC-WRONG-SIZE
# LLVM-RELOC-WRONG-SIZE: warning: '[[FILE]]': unable to load relocations for SHT_LLVM_CALL_GRAPH_PROFILE section: section [index 2] has invalid sh_entsize: expected 24, but got 32
# LLVM-RELOC-WRONG-SIZE: warning: '[[FILE]]': unable to load relocations for SHT_LLVM_CALL_GRAPH_PROFILE section: section [index 2] has invalid sh_entsize: expected 16, but got 24
# LLVM-RELOC-WRONG-SIZE-NEXT: CGProfile [
# LLVM-RELOC-WRONG-SIZE-NEXT: CGProfileEntry {
# LLVM-RELOC-WRONG-SIZE-NEXT: Weight: 89
@ -236,8 +236,8 @@ Sections:
- Weight: 89
- Weight: 98
EntSize: [[ENTSIZE=<none>]]
- Name: .rela.llvm.call-graph-profile
Type: SHT_RELA
- Name: .rel.llvm.call-graph-profile
Type: SHT_REL
Info: .llvm.call-graph-profile
Relocations:
- Symbol: foo
@ -251,7 +251,7 @@ Sections:
- Offset: 0x3
Symbol: foo
Type: R_X86_64_NONE
EntSize: 32
EntSize: 24
Symbols:
- Name: foo
- Name: bar

View File

@ -199,8 +199,8 @@ Sections:
Link: .symtab
EntSize: 8
Content: "2000000000000000"
- Name: .rela.llvm.call-graph-profile
Type: SHT_RELA
- Name: .rel.llvm.call-graph-profile
Type: SHT_REL
Info: .llvm.call-graph-profile
Relocations:
- Symbol: 1

View File

@ -6723,20 +6723,20 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printCGProfile() {
return;
}
Elf_Rela_Range CGProfileRela;
Elf_Rel_Range CGProfileRel;
bool UseReloc = (CGRelSection != nullptr);
if (UseReloc) {
Expected<Elf_Rela_Range> CGProfileRelaOrError =
this->Obj.relas(*CGRelSection);
Expected<Elf_Rel_Range> CGProfileRelaOrError =
this->Obj.rels(*CGRelSection);
if (!CGProfileRelaOrError) {
this->reportUniqueWarning("unable to load relocations for "
"SHT_LLVM_CALL_GRAPH_PROFILE section: " +
toString(CGProfileRelaOrError.takeError()));
UseReloc = false;
} else
CGProfileRela = *CGProfileRelaOrError;
CGProfileRel = *CGProfileRelaOrError;
if (UseReloc && CGProfileRela.size() != (CGProfileOrErr->size() * 2)) {
if (UseReloc && CGProfileRel.size() != (CGProfileOrErr->size() * 2)) {
this->reportUniqueWarning(
"number of from/to pairs does not match number of frequencies");
UseReloc = false;
@ -6746,7 +6746,7 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printCGProfile() {
"relocation section for a call graph section doesn't exist");
auto GetIndex = [&](uint32_t Index) {
const Elf_Rel_Impl<ELFT, true> &Rel = CGProfileRela[Index];
const Elf_Rel_Impl<ELFT, false> &Rel = CGProfileRel[Index];
return Rel.getSymbol(this->Obj.isMips64EL());
};