@@ -145,14 +145,16 @@ bool XCOFFWriter::initSectionHeaders(uint64_t &CurrentOffset) {
145145 uint64_t CurrentEndTDataAddr = 0 ;
146146 for (uint16_t I = 0 , E = InitSections.size (); I < E; ++I) {
147147 // Assign indices for sections.
148- if (InitSections[I].SectionName .size () &&
149- !SectionIndexMap[InitSections[I].SectionName ]) {
150- // The section index starts from 1.
151- SectionIndexMap[InitSections[I].SectionName ] = I + 1 ;
152- if ((I + 1 ) > MaxSectionIndex) {
153- ErrHandler (" exceeded the maximum permitted section index of " +
154- Twine (MaxSectionIndex));
155- return false ;
148+ if (InitSections[I].SectionName .size ()) {
149+ int16_t &SectionIndex = SectionIndexMap[InitSections[I].SectionName ];
150+ if (!SectionIndex) {
151+ // The section index starts from 1.
152+ SectionIndex = I + 1 ;
153+ if ((I + 1 ) > MaxSectionIndex) {
154+ ErrHandler (" exceeded the maximum permitted section index of " +
155+ Twine (MaxSectionIndex));
156+ return false ;
157+ }
156158 }
157159 }
158160
@@ -779,19 +781,19 @@ bool XCOFFWriter::writeSymbols() {
779781 W.write <uint32_t >(YamlSym.Value );
780782 }
781783 if (YamlSym.SectionName ) {
782- if (!SectionIndexMap.count (*YamlSym.SectionName )) {
784+ auto It = SectionIndexMap.find (*YamlSym.SectionName );
785+ if (It == SectionIndexMap.end ()) {
783786 ErrHandler (" the SectionName " + *YamlSym.SectionName +
784787 " specified in the symbol does not exist" );
785788 return false ;
786789 }
787- if (YamlSym.SectionIndex &&
788- SectionIndexMap[*YamlSym.SectionName ] != *YamlSym.SectionIndex ) {
790+ if (YamlSym.SectionIndex && It->second != *YamlSym.SectionIndex ) {
789791 ErrHandler (" the SectionName " + *YamlSym.SectionName +
790792 " and the SectionIndex (" + Twine (*YamlSym.SectionIndex ) +
791793 " ) refer to different sections" );
792794 return false ;
793795 }
794- W.write <int16_t >(SectionIndexMap[*YamlSym. SectionName ] );
796+ W.write <int16_t >(It-> second );
795797 } else {
796798 W.write <int16_t >(YamlSym.SectionIndex .value_or (0 ));
797799 }
0 commit comments