[PATCH] D105071: [lld/mac] Make symbol table order deterministic

Nico Weber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 28 17:15:49 PDT 2021


thakis created this revision.
thakis added a reviewer: lld-macho.
Herald added a subscriber: mgrang.
Herald added a reviewer: int3.
Herald added a reviewer: gkm.
Herald added a project: lld-macho.
thakis requested review of this revision.

SymtabSection::emitStabs() writes the symbol table in the order
of externalSymbols, which has the order of symtab->getSymbols(),
which is just the order symbols are added to the symbol table.

In practice, symbols in the symbol files of input .o files are
sorted, but since that's not guaranteed we sort them in
ObjFile::parseSymbols().  To make sure several symbols with the same
address keep the order they're in the input file, we have to use
stable_sort().

In practice, std::sort() on already-sorted inputs won't change the order
of just adjacent elements, and while in theory std::sort() could use a
random pivot, in practice the code should be deterministic as it was
previously too.

But now lld/test/MachO/stabs.s passes with LLVM_ENABLE_EXPENSIVE_CHECKS=ON
(the last test that was failing with that set).

Fixes a regression from D99972 <https://reviews.llvm.org/D99972>.

While here, remove an empty section in stabs.s and move
.subsections_via_symbols to the end where it usually is (this part no
behavior change).


https://reviews.llvm.org/D105071

Files:
  lld/MachO/InputFiles.cpp
  lld/test/MachO/stabs.s


Index: lld/test/MachO/stabs.s
===================================================================
--- lld/test/MachO/stabs.s
+++ lld/test/MachO/stabs.s
@@ -191,14 +191,14 @@
   .long  Lset3
   .byte  0                       ## End Of Children Mark
 Ldebug_info_end0:
-.subsections_via_symbols
-.section  __DWARF,__debug_line,regular,debug
 
 .section OTHER,more_text,regular,pure_instructions
 .globl _fun
 _fun:
   ret
 
+.subsections_via_symbols
+
 #--- foo.s
 .text
 .globl  _foo
@@ -240,13 +240,13 @@
   .long  Lset3
   .byte  0                       ## End Of Children Mark
 Ldebug_info_end0:
-.subsections_via_symbols
-.section  __DWARF,__debug_line,regular,debug
 
 .section  __DWARF,__debug_aranges,regular,debug
 ltmp1:
   .byte 0
 
+.subsections_via_symbols
+
 #--- no-debug.s
 ## This file has no debug info.
 .text
Index: lld/MachO/InputFiles.cpp
===================================================================
--- lld/MachO/InputFiles.cpp
+++ lld/MachO/InputFiles.cpp
@@ -602,7 +602,7 @@
       continue;
 
     std::vector<uint32_t> &symbolIndices = symbolsBySection[i];
-    llvm::sort(symbolIndices, [&](uint32_t lhs, uint32_t rhs) {
+    llvm::stable_sort(symbolIndices, [&](uint32_t lhs, uint32_t rhs) {
       return nList[lhs].n_value < nList[rhs].n_value;
     });
     uint64_t sectionAddr = sectionHeaders[i].addr;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105071.355075.patch
Type: text/x-patch
Size: 1342 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210629/9907a08e/attachment.bin>


More information about the llvm-commits mailing list