@@ -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 ();
@@ -1566,6 +1567,30 @@ void Writer::createSymbolAndStringTable() {
15661567 fileSize = alignTo (fileOff, ctx.config .fileAlign );
15671568}
15681569
1570+ void Writer::mergeSection (const std::map<StringRef, StringRef>::value_type &p) {
1571+ StringRef toName = p.second ;
1572+ if (p.first == toName)
1573+ return ;
1574+ StringSet<> names;
1575+ while (true ) {
1576+ if (!names.insert (toName).second )
1577+ Fatal (ctx) << " /merge: cycle found for section '" << p.first << " '" ;
1578+ auto i = ctx.config .merge .find (toName);
1579+ if (i == ctx.config .merge .end ())
1580+ break ;
1581+ toName = i->second ;
1582+ }
1583+ OutputSection *from = findSection (p.first );
1584+ OutputSection *to = findSection (toName);
1585+ if (!from)
1586+ return ;
1587+ if (!to) {
1588+ from->name = toName;
1589+ return ;
1590+ }
1591+ to->merge (from);
1592+ }
1593+
15691594void Writer::mergeSections () {
15701595 llvm::TimeTraceScope timeScope (" Merge sections" );
15711596 if (!pdataSec->chunks .empty ()) {
@@ -1594,28 +1619,16 @@ void Writer::mergeSections() {
15941619 }
15951620
15961621 for (auto &p : ctx.config .merge ) {
1597- StringRef toName = p.second ;
1598- if (p.first == toName)
1599- continue ;
1600- StringSet<> names;
1601- while (true ) {
1602- if (!names.insert (toName).second )
1603- Fatal (ctx) << " /merge: cycle found for section '" << p.first << " '" ;
1604- auto i = ctx.config .merge .find (toName);
1605- if (i == ctx.config .merge .end ())
1606- break ;
1607- toName = i->second ;
1608- }
1609- OutputSection *from = findSection (p.first );
1610- OutputSection *to = findSection (toName);
1611- if (!from)
1612- continue ;
1613- if (!to) {
1614- from->name = toName;
1615- continue ;
1616- }
1617- to->merge (from);
1622+ if (p.first != " .bss" )
1623+ mergeSection (p);
16181624 }
1625+
1626+ // Because .bss contains all zeros, it should be merged at the end of
1627+ // whatever section it is being merged into (usually .data) so that the image
1628+ // need not actually contain all of the zeros.
1629+ auto it = ctx.config .merge .find (" .bss" );
1630+ if (it != ctx.config .merge .end ())
1631+ mergeSection (*it);
16191632}
16201633
16211634// EC targets may have chunks of various architectures mixed together at this
0 commit comments