Skip to content

Commit dd1564e

Browse files
authored
[MachO] Report error when there are too many sections (#167418)
When there are more than 255 sections, MachO object writer allows creation of object files which are potentially malformed. Currently, there are assertions in object writer code that prevents this behavior. But for distributions where assertions are turned off this still results in creation of malformed object files. Turning assertions into explicit errors.
1 parent 2308d16 commit dd1564e

File tree

2 files changed

+1582
-3
lines changed

2 files changed

+1582
-3
lines changed

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "llvm/Support/Debug.h"
2929
#include "llvm/Support/ErrorHandling.h"
3030
#include "llvm/Support/MathExtras.h"
31+
#include "llvm/Support/SMLoc.h"
3132
#include "llvm/Support/raw_ostream.h"
3233
#include <algorithm>
3334
#include <cassert>
@@ -584,7 +585,12 @@ void MachObjectWriter::computeSymbolTable(
584585
unsigned Index = 1;
585586
for (MCSection &Sec : Asm)
586587
SectionIndexMap[&Sec] = Index++;
587-
assert(Index <= 256 && "Too many sections!");
588+
589+
// Section indices begin from 1 in MachO. Only sections 1-255 can be indexed
590+
// into section symbols. Referencing a section with index larger than 255 will
591+
// not set n_sect for these symbols.
592+
if (Index > 255)
593+
getContext().reportError(SMLoc(), "Too many sections!");
588594

589595
// Build the string table.
590596
for (const MCSymbol &Symbol : Asm.symbols()) {
@@ -621,7 +627,8 @@ void MachObjectWriter::computeSymbolTable(
621627
ExternalSymbolData.push_back(MSD);
622628
} else {
623629
MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
624-
assert(MSD.SectionIndex && "Invalid section index!");
630+
if (!MSD.SectionIndex)
631+
getContext().reportError(SMLoc(), "Invalid section index!");
625632
ExternalSymbolData.push_back(MSD);
626633
}
627634
}
@@ -645,7 +652,8 @@ void MachObjectWriter::computeSymbolTable(
645652
LocalSymbolData.push_back(MSD);
646653
} else {
647654
MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
648-
assert(MSD.SectionIndex && "Invalid section index!");
655+
if (!MSD.SectionIndex)
656+
getContext().reportError(SMLoc(), "Invalid section index!");
649657
LocalSymbolData.push_back(MSD);
650658
}
651659
}

0 commit comments

Comments
 (0)