Skip to content

Commit 38b12d4

Browse files
committed
MCAssembler: Postpone errors in layout iteration
.org and .fill errors reported in a layout iteration might go away in the final layout. Related to llvm#100283
1 parent 9ff30d4 commit 38b12d4

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class MCAssembler {
7171

7272
SmallVector<const MCSymbol *, 0> Symbols;
7373

74+
mutable SmallVector<std::pair<SMLoc, std::string>, 0> PendingErrors;
75+
7476
MCDwarfLineTableParams LTParams;
7577

7678
/// The set of function symbols for which a .thumb_func directive has
@@ -230,6 +232,10 @@ class MCAssembler {
230232
uint64_t FSize) const;
231233

232234
void reportError(SMLoc L, const Twine &Msg) const;
235+
// Record pending errors during layout iteration, as they may go away once the
236+
// layout is finalized.
237+
void recordError(SMLoc L, const Twine &Msg) const;
238+
void flushPendingErrors() const;
233239

234240
void dump() const;
235241
};

llvm/lib/MC/MCAssembler.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,12 @@ uint64_t MCAssembler::computeFragmentSize(const MCFragment &F) const {
217217
auto &FF = cast<MCFillFragment>(F);
218218
int64_t NumValues = 0;
219219
if (!FF.getNumValues().evaluateKnownAbsolute(NumValues, *this)) {
220-
reportError(FF.getLoc(), "expected assembly-time absolute expression");
220+
recordError(FF.getLoc(), "expected assembly-time absolute expression");
221221
return 0;
222222
}
223223
int64_t Size = NumValues * FF.getValueSize();
224224
if (Size < 0) {
225-
reportError(FF.getLoc(), "invalid number of bytes");
225+
recordError(FF.getLoc(), "invalid number of bytes");
226226
return 0;
227227
}
228228
return Size;
@@ -266,7 +266,7 @@ uint64_t MCAssembler::computeFragmentSize(const MCFragment &F) const {
266266
const MCOrgFragment &OF = cast<MCOrgFragment>(F);
267267
MCValue Value;
268268
if (!OF.getOffset().evaluateAsValue(Value, *this)) {
269-
reportError(OF.getLoc(), "expected assembly-time absolute expression");
269+
recordError(OF.getLoc(), "expected assembly-time absolute expression");
270270
return 0;
271271
}
272272

@@ -275,14 +275,14 @@ uint64_t MCAssembler::computeFragmentSize(const MCFragment &F) const {
275275
if (const auto *SA = Value.getAddSym()) {
276276
uint64_t Val;
277277
if (!getSymbolOffset(*SA, Val)) {
278-
reportError(OF.getLoc(), "expected absolute expression");
278+
recordError(OF.getLoc(), "expected absolute expression");
279279
return 0;
280280
}
281281
TargetLocation += Val;
282282
}
283283
int64_t Size = TargetLocation - FragmentOffset;
284284
if (Size < 0 || Size >= 0x40000000) {
285-
reportError(OF.getLoc(), "invalid .org offset '" + Twine(TargetLocation) +
285+
recordError(OF.getLoc(), "invalid .org offset '" + Twine(TargetLocation) +
286286
"' (at offset '" + Twine(FragmentOffset) +
287287
"')");
288288
return 0;
@@ -825,6 +825,7 @@ void MCAssembler::writeSectionData(raw_ostream &OS,
825825
for (const MCFragment &F : *Sec)
826826
writeFragment(OS, *this, F);
827827

828+
flushPendingErrors();
828829
assert(getContext().hadError() ||
829830
OS.tell() - Start == getSectionAddressSize(*Sec));
830831
}
@@ -878,6 +879,8 @@ void MCAssembler::layout() {
878879
// Finalize the layout, including fragment lowering.
879880
getBackend().finishLayout(*this);
880881

882+
flushPendingErrors();
883+
881884
DEBUG_WITH_TYPE("mc-dump", {
882885
errs() << "assembler backend - final-layout\n--\n";
883886
dump(); });
@@ -968,6 +971,7 @@ void MCAssembler::Finish() {
968971
stats::ObjectBytes += getWriter().writeObject();
969972

970973
HasLayout = false;
974+
assert(PendingErrors.empty());
971975
}
972976

973977
bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
@@ -1222,6 +1226,7 @@ bool MCAssembler::relaxFragment(MCFragment &F) {
12221226

12231227
bool MCAssembler::layoutOnce() {
12241228
++stats::RelaxationSteps;
1229+
PendingErrors.clear();
12251230

12261231
bool Changed = false;
12271232
for (MCSection &Sec : *this)
@@ -1235,6 +1240,16 @@ void MCAssembler::reportError(SMLoc L, const Twine &Msg) const {
12351240
getContext().reportError(L, Msg);
12361241
}
12371242

1243+
void MCAssembler::recordError(SMLoc Loc, const Twine &Msg) const {
1244+
PendingErrors.emplace_back(Loc, Msg.str());
1245+
}
1246+
1247+
void MCAssembler::flushPendingErrors() const {
1248+
for (auto &Err : PendingErrors)
1249+
reportError(Err.first, Err.second);
1250+
PendingErrors.clear();
1251+
}
1252+
12381253
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
12391254
LLVM_DUMP_METHOD void MCAssembler::dump() const{
12401255
raw_ostream &OS = errs();

0 commit comments

Comments
 (0)