31
31
#include " llvm/Support/TimeProfiler.h"
32
32
#include " llvm/Support/xxhash.h"
33
33
#include < climits>
34
+ #include < map>
34
35
35
36
#define DEBUG_TYPE " lld"
36
37
@@ -1368,6 +1369,58 @@ sortISDBySectionOrder(InputSectionDescription *isd,
1368
1369
isd->sections .push_back (isec);
1369
1370
}
1370
1371
1372
+ // Sort Xtensa literal sections in OutputSection. For each literal section we try
1373
+ // to find by name text(code) section, which uses these literals. The literal
1374
+ // section should always be placed before code section.
1375
+ // Also we try to place literal section just before code section.
1376
+ static void sortSectionXtensa (OutputSection &osec) {
1377
+ for (SectionCommand *b : osec.commands ) {
1378
+ if (auto *isd = dyn_cast<InputSectionDescription>(b)) {
1379
+ std::map<std::string, int > orderedNames;
1380
+ SmallVector<std::pair<InputSection *, int >, 0 > orderedSections;
1381
+ int sidx = 0 ;
1382
+
1383
+ for (InputSection *isec : isd->sections ) {
1384
+ if (orderedNames.count (isec->name .str ())) {
1385
+ orderedSections.push_back ({isec, orderedNames[isec->name .str ()]});
1386
+ continue ;
1387
+ }
1388
+ // Check if current section contains literals
1389
+ if (isec->name .contains (" .literal" )) {
1390
+ std::string literalName = isec->name .str ();
1391
+ std::size_t pos = literalName.find (" .literal" );
1392
+ std::string textName;
1393
+ // Reconstructing text(code) section name by literal section name
1394
+ if (pos == 0 ) {
1395
+ textName = " .text" ;
1396
+ } else {
1397
+ textName = literalName.substr (0 , pos);
1398
+ }
1399
+ textName += literalName.substr (pos + 8 , std::string::npos);
1400
+ if (orderedNames.count (textName)) {
1401
+ int textIdx = orderedNames[textName];
1402
+ int literalIdx = textIdx - 1 ;
1403
+ orderedSections.push_back ({isec, literalIdx});
1404
+ orderedNames[isec->name .str ()] = literalIdx;
1405
+ } else {
1406
+ orderedSections.push_back ({isec, sidx});
1407
+ orderedNames[isec->name .str ()] = sidx;
1408
+ }
1409
+ } else {
1410
+ orderedSections.push_back ({isec, sidx});
1411
+ orderedNames[isec->name .str ()] = sidx;
1412
+ }
1413
+ sidx += 2 ;
1414
+ }
1415
+
1416
+ llvm::sort (orderedSections, llvm::less_second ());
1417
+ isd->sections .clear ();
1418
+ for (std::pair<InputSection *, int > p : orderedSections)
1419
+ isd->sections .push_back (p.first );
1420
+ }
1421
+ }
1422
+ }
1423
+
1371
1424
static void sortSection (OutputSection &osec,
1372
1425
const DenseMap<const InputSectionBase *, int > &order) {
1373
1426
StringRef name = osec.name ;
@@ -1394,7 +1447,7 @@ static void sortSection(OutputSection &osec,
1394
1447
sortISDBySectionOrder (isd, order, osec.flags & SHF_EXECINSTR);
1395
1448
1396
1449
if (config->emachine == EM_XTENSA) {
1397
- osec. sort ([](InputSectionBase *s) { return s-> name . contains ( " .literal " ) ? 0 : 1 ; } );
1450
+ sortSectionXtensa (osec );
1398
1451
}
1399
1452
1400
1453
if (script->hasSectionsCommand )
0 commit comments