@@ -99,59 +99,61 @@ Error COFFWriter::finalizeCFGuardContents() {
9999 if (Obj.IsPE )
100100 return Error::success ();
101101
102+ auto IsSymIdxSection = [](StringRef Name) {
103+ return Name == " .gljmp$y" || Name == " .giats$y" || Name == " .gfids$y" ;
104+ };
105+
102106 DenseMap<size_t , size_t > SymIdMap;
107+ SmallDenseMap<ssize_t , coff_aux_section_definition *, 4 > SecIdMap;
103108 bool NeedUpdate = false ;
104- for (const auto &Sym : Obj.getSymbols ()) {
109+ for (auto &Sym : Obj.getMutableSymbols ()) {
105110 NeedUpdate |= Sym.OriginalRawIndex == Sym.RawIndex ;
106111 SymIdMap[Sym.OriginalRawIndex ] = Sym.RawIndex ;
112+
113+ // We collect only definition symbols of the sections to update checksum
114+ if (Sym.Sym .NumberOfAuxSymbols == 1 &&
115+ Sym.Sym .StorageClass == IMAGE_SYM_CLASS_STATIC && Sym.Sym .Value == 0 &&
116+ IsSymIdxSection (Sym.Name ))
117+ SecIdMap[Sym.TargetSectionId ] =
118+ reinterpret_cast <coff_aux_section_definition *>(
119+ Sym.AuxData [0 ].Opaque );
107120 }
108121
109122 if (!NeedUpdate)
110123 return Error::success ();
111124
112- for (auto &Sym : Obj.getMutableSymbols ()) {
113- if (Sym.Name != " .gljmp$y" && Sym.Name != " .giats$y" &&
114- Sym.Name != " .gfids$y" )
125+ for (auto &Sec : Obj.getMutableSections ()) {
126+ if (!IsSymIdxSection (Sec.Name ))
115127 continue ;
116128
117- auto Sec = find_if (Obj.getMutableSections (),
118- [&Sym](Section &S) { return S.Name == Sym.Name ; });
119-
120- if (Sec == Obj.getMutableSections ().end () ||
121- Sec->UniqueId != Sym.TargetSectionId )
122- return createStringError (object_error::invalid_symbol_index,
123- " symbol '%s' is missing its section" ,
124- Sym.Name .str ().c_str ());
125-
126- if (Sym.Sym .NumberOfAuxSymbols != 1 ||
127- Sym.Sym .StorageClass != IMAGE_SYM_CLASS_STATIC)
128- return createStringError (object_error::invalid_symbol_index,
129- " symbol '%s' has unexpected section format" ,
130- Sym.Name .str ().c_str ());
131-
132- ArrayRef<uint8_t > RawIds = Sec->getContents ();
129+ ArrayRef<uint8_t > RawIds = Sec.getContents ();
133130 // Nothing to do and also CheckSum will be -1 instead of 0 if we recalculate
134131 // it on empty input.
135132 if (RawIds.size () == 0 )
136133 continue ;
137134
135+ if (!SecIdMap.contains (Sec.UniqueId ))
136+ return createStringError (object_error::invalid_symbol_index,
137+ " section '%s' does not have the corresponding "
138+ " symbol or the symbol has unexpected format" ,
139+ Sec.Name .str ().c_str ());
140+
138141 // Create updated content
139142 ArrayRef<support::ulittle32_t > Ids (
140143 reinterpret_cast <const support::ulittle32_t *>(RawIds.data ()),
141144 RawIds.size () / 4 );
142145 std::vector<support::ulittle32_t > NewIds;
143- for (auto Id : Ids)
146+ for (auto Id : Ids) {
144147 NewIds.push_back (support::ulittle32_t (SymIdMap[Id]));
148+ }
145149 ArrayRef<uint8_t > NewRawIds (reinterpret_cast <uint8_t *>(NewIds.data ()),
146150 RawIds.size ());
147151 // Update check sum
148152 JamCRC JC (/* Init=*/ 0 );
149153 JC.update (NewRawIds);
150- coff_aux_section_definition *SD =
151- reinterpret_cast <coff_aux_section_definition *>(Sym.AuxData [0 ].Opaque );
152- SD->CheckSum = JC.getCRC ();
154+ SecIdMap[Sec.UniqueId ]->CheckSum = JC.getCRC ();
153155 // Set new content
154- Sec-> setOwnedContents (NewRawIds.vec ());
156+ Sec. setOwnedContents (NewRawIds.vec ());
155157 }
156158 return Error::success ();
157159}
0 commit comments