[lld] b9095f5 - [lld][MachO] Fix function starts section

Alexander Shaposhnikov via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 11 17:53:08 PDT 2021


Author: Alexander Shaposhnikov
Date: 2021-06-11T17:47:28-07:00
New Revision: b9095f5e1a7f6f3ee1d741a6931a318da050bed8

URL: https://github.com/llvm/llvm-project/commit/b9095f5e1a7f6f3ee1d741a6931a318da050bed8
DIFF: https://github.com/llvm/llvm-project/commit/b9095f5e1a7f6f3ee1d741a6931a318da050bed8.diff

LOG: [lld][MachO] Fix function starts section

Sort the addresses stored in FunctionStarts section.
Previously we were encoding potentially large numbers (due to unsigned overflow).

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D103662

Added: 
    

Modified: 
    lld/MachO/SyntheticSections.cpp
    lld/MachO/SyntheticSections.h
    lld/test/MachO/function-starts.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 235389c10601..cec0f950fe7e 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -588,21 +588,25 @@ FunctionStartsSection::FunctionStartsSection()
 
 void FunctionStartsSection::finalizeContents() {
   raw_svector_ostream os{contents};
-  uint64_t addr = in.header->addr;
+  std::vector<uint64_t> addrs;
   for (const Symbol *sym : symtab->getSymbols()) {
     if (const auto *defined = dyn_cast<Defined>(sym)) {
       if (!defined->isec || !isCodeSection(defined->isec) || !defined->isLive())
         continue;
       // TODO: Add support for thumbs, in that case
       // the lowest bit of nextAddr needs to be set to 1.
-      uint64_t nextAddr = defined->getVA();
-      uint64_t delta = nextAddr - addr;
-      if (delta == 0)
-        continue;
-      encodeULEB128(delta, os);
-      addr = nextAddr;
+      addrs.push_back(defined->getVA());
     }
   }
+  llvm::sort(addrs);
+  uint64_t addr = in.header->addr;
+  for (uint64_t nextAddr : addrs) {
+    uint64_t delta = nextAddr - addr;
+    if (delta == 0)
+      continue;
+    encodeULEB128(delta, os);
+    addr = nextAddr;
+  }
   os << '\0';
 }
 

diff  --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index ca78ce804db3..bde3b3bbe1ac 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -381,6 +381,7 @@ class ExportSection : public LinkEditSection {
   size_t size = 0;
 };
 
+// Stores ULEB128 delta encoded addresses of functions.
 class FunctionStartsSection : public LinkEditSection {
 public:
   FunctionStartsSection();

diff  --git a/lld/test/MachO/function-starts.s b/lld/test/MachO/function-starts.s
index d5168c630b61..7026ca457dd4 100644
--- a/lld/test/MachO/function-starts.s
+++ b/lld/test/MachO/function-starts.s
@@ -13,9 +13,9 @@
 # BASIC-NEXT:  [[#%x,F1:]] g F __TEXT,__text _f1
 # BASIC-NEXT:  [[#%x,F2:]] g F __TEXT,__text _f2
 # BASIC-LABEL: basic:
-# BASIC:       [[#MAIN]]
 # BASIC:       [[#F1]]
 # BASIC:       [[#F2]]
+# BASIC:       [[#MAIN]]
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/alias.s -o %t/alias.o
 # RUN: %lld %t/alias.o -o %t/alias
@@ -28,8 +28,8 @@
 # ALIAS-NEXT:  [[#%x,MAIN:]] g F __TEXT,__text _main
 # ALIAS-NEXT:  [[#%x,F1:]] g F __TEXT,__text _f1
 # ALIAS-LABEL: alias:
-# ALIAS:       [[#MAIN]]
 # ALIAS:       [[#F1]]
+# ALIAS:       [[#MAIN]]
 
 # RUN: %lld %t/basic.o -no_function_starts -o %t/basic-no-function-starts
 # RUN: llvm-objdump --macho --function-starts %t/basic-no-function-starts | FileCheck %s --check-prefix=NO-FUNCTION-STARTS


        


More information about the llvm-commits mailing list