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

View File

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

View File

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

View File

@ -34,8 +34,8 @@ Sections:
- Weight: 89 - Weight: 89
- Weight: 98 - Weight: 98
EntSize: [[ENTSIZE=<none>]] EntSize: [[ENTSIZE=<none>]]
- Name: .rela.llvm.call-graph-profile - Name: .rel.llvm.call-graph-profile
Type: SHT_RELA Type: SHT_REL
Info: .llvm.call-graph-profile Info: .llvm.call-graph-profile
Relocations: Relocations:
- Symbol: foo - Symbol: foo
@ -98,8 +98,8 @@ Sections:
- Weight: 10 - Weight: 10
- Weight: 20 - Weight: 20
- Weight: 20 - Weight: 20
- Name: .rela.llvm.call-graph-profile - Name: .rel.llvm.call-graph-profile
Type: SHT_RELA Type: SHT_REL
Info: .llvm.call-graph-profile Info: .llvm.call-graph-profile
Relocations: Relocations:
- Symbol: 1 - Symbol: 1
@ -186,8 +186,8 @@ Sections:
- Weight: 89 - Weight: 89
- Weight: 98 - Weight: 98
EntSize: [[ENTSIZE=<none>]] EntSize: [[ENTSIZE=<none>]]
- Name: .rela.llvm.call-graph-profile - Name: .rel.llvm.call-graph-profile
Type: SHT_RELA Type: SHT_REL
Info: .llvm.call-graph-profile Info: .llvm.call-graph-profile
Relocations: Relocations:
- Symbol: foo - 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 --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 # 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: CGProfile [
# LLVM-RELOC-WRONG-SIZE-NEXT: CGProfileEntry { # LLVM-RELOC-WRONG-SIZE-NEXT: CGProfileEntry {
# LLVM-RELOC-WRONG-SIZE-NEXT: Weight: 89 # LLVM-RELOC-WRONG-SIZE-NEXT: Weight: 89
@ -236,8 +236,8 @@ Sections:
- Weight: 89 - Weight: 89
- Weight: 98 - Weight: 98
EntSize: [[ENTSIZE=<none>]] EntSize: [[ENTSIZE=<none>]]
- Name: .rela.llvm.call-graph-profile - Name: .rel.llvm.call-graph-profile
Type: SHT_RELA Type: SHT_REL
Info: .llvm.call-graph-profile Info: .llvm.call-graph-profile
Relocations: Relocations:
- Symbol: foo - Symbol: foo
@ -251,7 +251,7 @@ Sections:
- Offset: 0x3 - Offset: 0x3
Symbol: foo Symbol: foo
Type: R_X86_64_NONE Type: R_X86_64_NONE
EntSize: 32 EntSize: 24
Symbols: Symbols:
- Name: foo - Name: foo
- Name: bar - Name: bar

View File

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

View File

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