diff --git a/llvm/lib/ObjectYAML/XCOFFEmitter.cpp b/llvm/lib/ObjectYAML/XCOFFEmitter.cpp index f3a9fb188f51d..5d7d6a1141ba0 100644 --- a/llvm/lib/ObjectYAML/XCOFFEmitter.cpp +++ b/llvm/lib/ObjectYAML/XCOFFEmitter.cpp @@ -145,14 +145,16 @@ bool XCOFFWriter::initSectionHeaders(uint64_t &CurrentOffset) { uint64_t CurrentEndTDataAddr = 0; for (uint16_t I = 0, E = InitSections.size(); I < E; ++I) { // Assign indices for sections. - if (InitSections[I].SectionName.size() && - !SectionIndexMap[InitSections[I].SectionName]) { - // The section index starts from 1. - SectionIndexMap[InitSections[I].SectionName] = I + 1; - if ((I + 1) > MaxSectionIndex) { - ErrHandler("exceeded the maximum permitted section index of " + - Twine(MaxSectionIndex)); - return false; + if (InitSections[I].SectionName.size()) { + int16_t &SectionIndex = SectionIndexMap[InitSections[I].SectionName]; + if (!SectionIndex) { + // The section index starts from 1. + SectionIndex = I + 1; + if ((I + 1) > MaxSectionIndex) { + ErrHandler("exceeded the maximum permitted section index of " + + Twine(MaxSectionIndex)); + return false; + } } } @@ -779,19 +781,19 @@ bool XCOFFWriter::writeSymbols() { W.write(YamlSym.Value); } if (YamlSym.SectionName) { - if (!SectionIndexMap.count(*YamlSym.SectionName)) { + auto It = SectionIndexMap.find(*YamlSym.SectionName); + if (It == SectionIndexMap.end()) { ErrHandler("the SectionName " + *YamlSym.SectionName + " specified in the symbol does not exist"); return false; } - if (YamlSym.SectionIndex && - SectionIndexMap[*YamlSym.SectionName] != *YamlSym.SectionIndex) { + if (YamlSym.SectionIndex && It->second != *YamlSym.SectionIndex) { ErrHandler("the SectionName " + *YamlSym.SectionName + " and the SectionIndex (" + Twine(*YamlSym.SectionIndex) + ") refer to different sections"); return false; } - W.write(SectionIndexMap[*YamlSym.SectionName]); + W.write(It->second); } else { W.write(YamlSym.SectionIndex.value_or(0)); }