@@ -215,6 +215,7 @@ class Writer {
215
215
void appendImportThunks ();
216
216
void locateImportTables ();
217
217
void createExportTable ();
218
+ void mergeSection (const std::map<StringRef, StringRef>::value_type &p);
218
219
void mergeSections ();
219
220
void sortECChunks ();
220
221
void appendECImportTables ();
@@ -1566,6 +1567,30 @@ void Writer::createSymbolAndStringTable() {
1566
1567
fileSize = alignTo (fileOff, ctx.config .fileAlign );
1567
1568
}
1568
1569
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
+
1569
1594
void Writer::mergeSections () {
1570
1595
llvm::TimeTraceScope timeScope (" Merge sections" );
1571
1596
if (!pdataSec->chunks .empty ()) {
@@ -1594,28 +1619,16 @@ void Writer::mergeSections() {
1594
1619
}
1595
1620
1596
1621
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);
1618
1624
}
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);
1619
1632
}
1620
1633
1621
1634
// EC targets may have chunks of various architectures mixed together at this
0 commit comments