Skip to content

Commit 4f2a60c

Browse files
MaskRayAlexisPerry
authored andcommitted
[MC,COFF] Register .llvm.call-graph-profile in finalizeImpl
All sections should have been created before MCAssembler::layout so that every section has an ordinal. Registering the section in WinCOFFObjectWriter::executePostLayoutBinding is a hack.
1 parent 8b54f47 commit 4f2a60c

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

llvm/include/llvm/MC/MCWinCOFFStreamer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class MCWinCOFFStreamer : public MCObjectStreamer {
7777
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
7878

7979
void finalizeCGProfileEntry(const MCSymbolRefExpr *&S);
80-
void finalizeCGProfile();
8180

8281
private:
8382
void Error(const Twine &Msg) const;

llvm/lib/MC/MCWinCOFFStreamer.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "llvm/MC/MCObjectFileInfo.h"
2626
#include "llvm/MC/MCObjectStreamer.h"
2727
#include "llvm/MC/MCObjectWriter.h"
28-
#include "llvm/MC/MCSection.h"
28+
#include "llvm/MC/MCSectionCOFF.h"
2929
#include "llvm/MC/MCSymbolCOFF.h"
3030
#include "llvm/Support/Casting.h"
3131
#include "llvm/Support/ErrorHandling.h"
@@ -353,15 +353,21 @@ void MCWinCOFFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) {
353353
cast<MCSymbolCOFF>(S)->setExternal(true);
354354
}
355355

356-
void MCWinCOFFStreamer::finalizeCGProfile() {
357-
for (MCAssembler::CGProfileEntry &E : getAssembler().CGProfile) {
358-
finalizeCGProfileEntry(E.From);
359-
finalizeCGProfileEntry(E.To);
360-
}
361-
}
362-
363356
void MCWinCOFFStreamer::finishImpl() {
364-
finalizeCGProfile();
357+
MCAssembler &Asm = getAssembler();
358+
if (Asm.getWriter().getEmitAddrsigSection()) {
359+
// Register the section.
360+
switchSection(Asm.getContext().getCOFFSection(".llvm_addrsig",
361+
COFF::IMAGE_SCN_LNK_REMOVE));
362+
}
363+
if (!Asm.CGProfile.empty()) {
364+
for (MCAssembler::CGProfileEntry &E : Asm.CGProfile) {
365+
finalizeCGProfileEntry(E.From);
366+
finalizeCGProfileEntry(E.To);
367+
}
368+
switchSection(Asm.getContext().getCOFFSection(".llvm.call-graph-profile",
369+
COFF::IMAGE_SCN_LNK_REMOVE));
370+
}
365371

366372
MCObjectStreamer::finishImpl();
367373
}

llvm/lib/MC/WinCOFFObjectWriter.cpp

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@ class WinCOFFWriter {
151151
bool UseOffsetLabels = false;
152152

153153
public:
154-
MCSectionCOFF *AddrsigSection = nullptr;
155-
MCSectionCOFF *CGProfileSection = nullptr;
156-
157154
enum DwoMode {
158155
AllSections,
159156
NonDwoOnly,
@@ -1096,10 +1093,10 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm,
10961093
}
10971094

10981095
// Create the contents of the .llvm_addrsig section.
1099-
if (Mode != DwoOnly && OWriter.EmitAddrsigSection) {
1100-
auto *Frag = Asm.getContext().allocFragment<MCDataFragment>();
1101-
Frag->setParent(AddrsigSection);
1102-
AddrsigSection->addFragment(*Frag);
1096+
if (Mode != DwoOnly && OWriter.getEmitAddrsigSection()) {
1097+
auto *Sec = Asm.getContext().getCOFFSection(
1098+
".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE);
1099+
auto *Frag = cast<MCDataFragment>(Sec->curFragList()->Head);
11031100
raw_svector_ostream OS(Frag->getContents());
11041101
for (const MCSymbol *S : OWriter.AddrsigSyms) {
11051102
if (!S->isRegistered())
@@ -1118,10 +1115,10 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm,
11181115
}
11191116

11201117
// Create the contents of the .llvm.call-graph-profile section.
1121-
if (Mode != DwoOnly && CGProfileSection) {
1122-
auto *Frag = Asm.getContext().allocFragment<MCDataFragment>();
1123-
Frag->setParent(CGProfileSection);
1124-
CGProfileSection->addFragment(*Frag);
1118+
if (Mode != DwoOnly && !Asm.CGProfile.empty()) {
1119+
auto *Sec = Asm.getContext().getCOFFSection(
1120+
".llvm.call-graph-profile", COFF::IMAGE_SCN_LNK_REMOVE);
1121+
auto *Frag = cast<MCDataFragment>(Sec->curFragList()->Head);
11251122
raw_svector_ostream OS(Frag->getContents());
11261123
for (const MCAssembler::CGProfileEntry &CGPE : Asm.CGProfile) {
11271124
uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
@@ -1209,18 +1206,6 @@ bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
12091206

12101207
void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
12111208
const MCAsmLayout &Layout) {
1212-
if (EmitAddrsigSection) {
1213-
ObjWriter->AddrsigSection = Asm.getContext().getCOFFSection(
1214-
".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE);
1215-
Asm.registerSection(*ObjWriter->AddrsigSection);
1216-
}
1217-
1218-
if (!Asm.CGProfile.empty()) {
1219-
ObjWriter->CGProfileSection = Asm.getContext().getCOFFSection(
1220-
".llvm.call-graph-profile", COFF::IMAGE_SCN_LNK_REMOVE);
1221-
Asm.registerSection(*ObjWriter->CGProfileSection);
1222-
}
1223-
12241209
ObjWriter->executePostLayoutBinding(Asm, Layout);
12251210
if (DwoWriter)
12261211
DwoWriter->executePostLayoutBinding(Asm, Layout);

0 commit comments

Comments
 (0)