Skip to content

Commit 6c42d0d

Browse files
committed
[MC,CodeView] Postpone MCDataFragment creation to finish time
`StrTabFragment` created due to `.cv_stringtable` is not placed directly into a section. This requires a funky ~CodeViewContext and does not fit well with the future that moves MCFragment storage out-of-line.
1 parent 49ecd66 commit 6c42d0d

File tree

3 files changed

+16
-30
lines changed

3 files changed

+16
-30
lines changed

llvm/include/llvm/MC/MCCodeView.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ struct MCCVFunctionInfo {
144144
class CodeViewContext {
145145
public:
146146
CodeViewContext(MCContext *MCCtx) : MCCtx(MCCtx) {}
147-
~CodeViewContext();
148147

149148
CodeViewContext &operator=(const CodeViewContext &other) = delete;
150149
CodeViewContext(const CodeViewContext &other) = delete;
151150

151+
void finish();
152+
152153
bool isValidFileNumber(unsigned FileNumber) const;
153154
bool addFile(MCStreamer &OS, unsigned FileNumber, StringRef Filename,
154155
ArrayRef<uint8_t> ChecksumBytes, uint8_t ChecksumKind);
@@ -230,9 +231,7 @@ class CodeViewContext {
230231

231232
/// The fragment that ultimately holds our strings.
232233
MCDataFragment *StrTabFragment = nullptr;
233-
bool InsertedStrTabFragment = false;
234-
235-
MCDataFragment *getStringTableFragment();
234+
SmallVector<char, 0> StrTab = {'\0'};
236235

237236
/// Get a string table offset.
238237
unsigned getStringTableOffset(StringRef S);

llvm/lib/MC/MCCodeView.cpp

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
using namespace llvm;
2626
using namespace llvm::codeview;
2727

28-
CodeViewContext::~CodeViewContext() {
29-
// If someone inserted strings into the string table but never actually
30-
// emitted them somewhere, clean up the fragment.
31-
if (!InsertedStrTabFragment && StrTabFragment)
32-
StrTabFragment->destroy();
28+
void CodeViewContext::finish() {
29+
if (StrTabFragment)
30+
StrTabFragment->setContents(StrTab);
3331
}
3432

3533
/// This is a valid number for use with .cv_loc if we've already seen a .cv_file
@@ -133,26 +131,15 @@ void CodeViewContext::recordCVLoc(MCContext &Ctx, const MCSymbol *Label,
133131
Label, FunctionId, FileNo, Line, Column, PrologueEnd, IsStmt});
134132
}
135133

136-
MCDataFragment *CodeViewContext::getStringTableFragment() {
137-
if (!StrTabFragment) {
138-
StrTabFragment = MCCtx->allocFragment<MCDataFragment>();
139-
// Start a new string table out with a null byte.
140-
StrTabFragment->appendContents(1, '\0');
141-
}
142-
return StrTabFragment;
143-
}
144-
145134
std::pair<StringRef, unsigned> CodeViewContext::addToStringTable(StringRef S) {
146-
SmallVectorImpl<char> &Contents = getStringTableFragment()->getContents();
147135
auto Insertion =
148-
StringTable.insert(std::make_pair(S, unsigned(Contents.size())));
136+
StringTable.insert(std::make_pair(S, unsigned(StrTab.size())));
149137
// Return the string from the table, since it is stable.
150138
std::pair<StringRef, unsigned> Ret =
151139
std::make_pair(Insertion.first->first(), Insertion.first->second);
152140
if (Insertion.second) {
153141
// The string map key is always null terminated.
154-
StrTabFragment->appendContents(
155-
ArrayRef(Ret.first.begin(), Ret.first.end() + 1));
142+
StrTab.append(Ret.first.begin(), Ret.first.end() + 1);
156143
}
157144
return Ret;
158145
}
@@ -178,9 +165,9 @@ void CodeViewContext::emitStringTable(MCObjectStreamer &OS) {
178165
// Put the string table data fragment here, if we haven't already put it
179166
// somewhere else. If somebody wants two string tables in their .s file, one
180167
// will just be empty.
181-
if (!InsertedStrTabFragment) {
182-
OS.insert(getStringTableFragment());
183-
InsertedStrTabFragment = true;
168+
if (!StrTabFragment) {
169+
StrTabFragment = Ctx.allocFragment<MCDataFragment>();
170+
OS.insert(StrTabFragment);
184171
}
185172

186173
OS.emitValueToAlignment(Align(4), 0);
@@ -375,11 +362,9 @@ void CodeViewContext::emitLineTableForFunction(MCObjectStreamer &OS,
375362
return Loc.getFileNum() != CurFileNum;
376363
});
377364
unsigned EntryCount = FileSegEnd - I;
378-
OS.AddComment(
379-
"Segment for file '" +
380-
Twine(getStringTableFragment()
381-
->getContents()[Files[CurFileNum - 1].StringTableOffset]) +
382-
"' begins");
365+
OS.AddComment("Segment for file '" +
366+
Twine(StrTab[Files[CurFileNum - 1].StringTableOffset]) +
367+
"' begins");
383368
OS.emitCVFileChecksumOffsetDirective(CurFileNum);
384369
OS.emitInt32(EntryCount);
385370
uint32_t SegmentSize = 12;

llvm/lib/MC/MCWinCOFFStreamer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/MC/MCAsmBackend.h"
1919
#include "llvm/MC/MCAssembler.h"
2020
#include "llvm/MC/MCCodeEmitter.h"
21+
#include "llvm/MC/MCCodeView.h"
2122
#include "llvm/MC/MCContext.h"
2223
#include "llvm/MC/MCExpr.h"
2324
#include "llvm/MC/MCFixup.h"
@@ -370,6 +371,7 @@ void MCWinCOFFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) {
370371
}
371372

372373
void MCWinCOFFStreamer::finishImpl() {
374+
getContext().getCVContext().finish();
373375
MCAssembler &Asm = getAssembler();
374376
if (Asm.getWriter().getEmitAddrsigSection()) {
375377
// Register the section.

0 commit comments

Comments
 (0)