@@ -215,6 +215,7 @@ class Writer {
215215 void appendImportThunks ();
216216 void locateImportTables ();
217217 void createExportTable ();
218+ void mergeSection (const std::map<StringRef, StringRef>::value_type &p);
218219 void mergeSections ();
219220 void sortECChunks ();
220221 void appendECImportTables ();
@@ -1547,6 +1548,30 @@ void Writer::createSymbolAndStringTable() {
15471548 fileSize = alignTo (fileOff, ctx.config .fileAlign );
15481549}
15491550
1551+ void Writer::mergeSection (const std::map<StringRef, StringRef>::value_type &p) {
1552+ StringRef toName = p.second ;
1553+ if (p.first == toName)
1554+ return ;
1555+ StringSet<> names;
1556+ while (true ) {
1557+ if (!names.insert (toName).second )
1558+ Fatal (ctx) << " /merge: cycle found for section '" << p.first << " '" ;
1559+ auto i = ctx.config .merge .find (toName);
1560+ if (i == ctx.config .merge .end ())
1561+ break ;
1562+ toName = i->second ;
1563+ }
1564+ OutputSection *from = findSection (p.first );
1565+ OutputSection *to = findSection (toName);
1566+ if (!from)
1567+ return ;
1568+ if (!to) {
1569+ from->name = toName;
1570+ return ;
1571+ }
1572+ to->merge (from);
1573+ }
1574+
15501575void Writer::mergeSections () {
15511576 llvm::TimeTraceScope timeScope (" Merge sections" );
15521577 if (!pdataSec->chunks .empty ()) {
@@ -1575,28 +1600,16 @@ void Writer::mergeSections() {
15751600 }
15761601
15771602 for (auto &p : ctx.config .merge ) {
1578- StringRef toName = p.second ;
1579- if (p.first == toName)
1580- continue ;
1581- StringSet<> names;
1582- while (true ) {
1583- if (!names.insert (toName).second )
1584- Fatal (ctx) << " /merge: cycle found for section '" << p.first << " '" ;
1585- auto i = ctx.config .merge .find (toName);
1586- if (i == ctx.config .merge .end ())
1587- break ;
1588- toName = i->second ;
1589- }
1590- OutputSection *from = findSection (p.first );
1591- OutputSection *to = findSection (toName);
1592- if (!from)
1593- continue ;
1594- if (!to) {
1595- from->name = toName;
1596- continue ;
1597- }
1598- to->merge (from);
1603+ if (p.first != " .bss" )
1604+ mergeSection (p);
15991605 }
1606+
1607+ // Because .bss contains all zeros, it should be merged at the end of
1608+ // whatever section it is being merged into (usually .data) so that the image
1609+ // need not actually contain all of the zeros.
1610+ auto it = ctx.config .merge .find (" .bss" );
1611+ if (it != ctx.config .merge .end ())
1612+ mergeSection (*it);
16001613}
16011614
16021615// EC targets may have chunks of various architectures mixed together at this
0 commit comments