[llvm] [AArch64, ELF] Allow implicit $d/$x at section beginning (PR #99718)

Peter Smith via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 16 12:18:06 PDT 2024


================
@@ -299,6 +311,53 @@ void AArch64TargetELFStreamer::finish() {
   AArch64ELFStreamer &S = getStreamer();
   MCContext &Ctx = S.getContext();
   auto &Asm = S.getAssembler();
+
+  // If ImplicitMapSyms is specified, ensure that text sections end with
+  // the A64 state while non-text sections end with the data state. When
+  // sections are combined by the linker, the subsequent section will start with
+  // the right state. The ending mapping symbol is added right after the last
+  // symbol relative to the section. When a dumb linker combines (.text.0; .word
+  // 0) and (.text.1; .word 0), the ending $x of .text.0 precedes the $d of
+  // .text.1, even if they have the same address.
+  if (S.ImplicitMapSyms) {
+    auto &Syms = Asm.getSymbols();
+    const size_t NumSyms = Syms.size();
+    DenseMap<MCSection *, MCSymbol *> EndMappingSym;
+    for (MCSection &Sec : Asm) {
+      S.switchSection(&Sec);
+      if (S.LastEMS == (Sec.isText() ? AArch64ELFStreamer::EMS_Data
+                                     : AArch64ELFStreamer::EMS_A64))
+        EndMappingSym.try_emplace(
+            &Sec, S.emitMappingSymbol(Sec.isText() ? "$x" : "$d"));
+    }
+    if (Syms.size() != NumSyms) {
+      SmallVector<const MCSymbol *, 0> NewSyms;
+      DenseMap<MCSection *, size_t> Cnt;
+      Syms.truncate(NumSyms);
+      for (const MCSymbol *Sym : Syms)
+        if (Sym->isInSection())
----------------
smithp35 wrote:

It looks like we have a count of symbols for every section. For a large object with lots of sections (coming from say LTO with -ffunction-sections) then if we expect trailing symbols to be rare, then it could be better to test if the section has a trailing mapping symbol early, and only retain counts for the  sections that we are adding to.

May not be worth it for the extra complexity though.

https://github.com/llvm/llvm-project/pull/99718


More information about the llvm-commits mailing list