@@ -1080,12 +1080,14 @@ static void maybeShuffle(Ctx &ctx,
10801080 }
10811081}
10821082
1083- // Builds section order for handling --symbol-ordering-file.
1083+ // Return section order within an InputSectionDescription.
1084+ // If both --symbol-ordering-file and call graph profile are present, the order
1085+ // file takes precedence, but the call graph profile is still used for symbols
1086+ // that don't appear in the order file.
10841087static DenseMap<const InputSectionBase *, int > buildSectionOrder (Ctx &ctx) {
10851088 DenseMap<const InputSectionBase *, int > sectionOrder;
1086- // Use the rarely used option --call-graph-ordering-file to sort sections.
10871089 if (!ctx.arg .callGraphProfile .empty ())
1088- return computeCallGraphProfileOrder (ctx);
1090+ sectionOrder = computeCallGraphProfileOrder (ctx);
10891091
10901092 if (ctx.arg .symbolOrderingFile .empty ())
10911093 return sectionOrder;
@@ -1099,7 +1101,7 @@ static DenseMap<const InputSectionBase *, int> buildSectionOrder(Ctx &ctx) {
10991101 // appear in the symbol ordering file have the lowest priority 0.
11001102 // All explicitly mentioned symbols have negative (higher) priorities.
11011103 DenseMap<CachedHashStringRef, SymbolOrderEntry> symbolOrder;
1102- int priority = -ctx.arg .symbolOrderingFile .size ();
1104+ int priority = -sectionOrder. size () - ctx.arg .symbolOrderingFile .size ();
11031105 for (StringRef s : ctx.arg .symbolOrderingFile )
11041106 symbolOrder.insert ({CachedHashStringRef (s), {priority++, false }});
11051107
@@ -1255,11 +1257,11 @@ static void sortSection(Ctx &ctx, OutputSection &osec,
12551257 }
12561258}
12571259
1258- // If no layout was provided by linker script, we want to apply default
1259- // sorting for special input sections. This also handles --symbol-ordering-file.
1260+ // Sort sections within each InputSectionDescription.
12601261template <class ELFT > void Writer<ELFT>::sortInputSections() {
1261- // Build the order once since it is expensive .
1262+ // Assign negative priorities .
12621263 DenseMap<const InputSectionBase *, int > order = buildSectionOrder (ctx);
1264+ // Assign non-negative priorities due to --shuffle-sections.
12631265 maybeShuffle (ctx, order);
12641266 for (SectionCommand *cmd : ctx.script ->sectionCommands )
12651267 if (auto *osd = dyn_cast<OutputDesc>(cmd))
0 commit comments