[llvm-branch-commits] [lld] c7dbaec - [lld-macho] Add isCodeSection()

Jez Ng via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Dec 1 15:10:21 PST 2020


Author: Jez Ng
Date: 2020-12-01T15:05:21-08:00
New Revision: c7dbaec396ef98b8bc6acb7631d2919449986add

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

LOG: [lld-macho] Add isCodeSection()

This is the same logic that ld64 uses to determine which sections
contain functions. This was added so that we could determine which
STABS entries should be N_FUN.

Reviewed By: clayborg

Differential Revision: https://reviews.llvm.org/D92430

Added: 
    

Modified: 
    lld/MachO/InputSection.cpp
    lld/MachO/InputSection.h
    lld/MachO/SyntheticSections.cpp
    lld/test/MachO/stabs.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index e164630159e2..80c263301f41 100644
--- a/lld/MachO/InputSection.cpp
+++ b/lld/MachO/InputSection.cpp
@@ -58,6 +58,23 @@ void InputSection::writeTo(uint8_t *buf) {
   }
 }
 
+bool macho::isCodeSection(InputSection *isec) {
+  uint32_t type = isec->flags & MachO::SECTION_TYPE;
+  if (type != S_REGULAR && type != S_COALESCED)
+    return false;
+
+  uint32_t attr = isec->flags & MachO::SECTION_ATTRIBUTES_USR;
+  if (attr == S_ATTR_PURE_INSTRUCTIONS)
+    return true;
+
+  if (isec->segname == segment_names::text)
+    return StringSwitch<bool>(isec->name)
+        .Cases("__textcoal_nt", "__StaticInit", true)
+        .Default(false);
+
+  return false;
+}
+
 std::string lld::toString(const InputSection *isec) {
   return (toString(isec->file) + ":(" + isec->name + ")").str();
 }

diff  --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h
index 4ef8d84bc8a0..f405fd6cf6d3 100644
--- a/lld/MachO/InputSection.h
+++ b/lld/MachO/InputSection.h
@@ -76,6 +76,8 @@ class InputSection {
   std::vector<Reloc> relocs;
 };
 
+bool isCodeSection(InputSection *);
+
 extern std::vector<InputSection *> inputSections;
 
 } // namespace macho

diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 6ba067bbc9e5..808d3594bfad 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -668,9 +668,7 @@ void SymtabSection::emitStabs() {
     symStab.strx = stringTableSection.addString(defined->getName());
     symStab.value = defined->getVA();
 
-    // XXX is it right to assume that all symbols in __text are function
-    // symbols?
-    if (isec->name == "__text") {
+    if (isCodeSection(isec)) {
       symStab.type = MachO::N_FUN;
       stabs.emplace_back(std::move(symStab));
       emitEndFunStab(defined);

diff  --git a/lld/test/MachO/stabs.s b/lld/test/MachO/stabs.s
index a3f1e2906d2b..445783269d11 100644
--- a/lld/test/MachO/stabs.s
+++ b/lld/test/MachO/stabs.s
@@ -36,12 +36,15 @@
 # CHECK-NEXT:  [[#DATA_ID:]]      __data
 # CHECK-NEXT:  [[#MORE_DATA_ID:]] more_data
 # CHECK-NEXT:  [[#COMM_ID:]]      __common
+# CHECK-NEXT:  [[#MORE_TEXT_ID:]] more_text
 
 # CHECK:       0000000000000000 - 00                     0000    SO /tmp/test.cpp
 # CHECK-NEXT:  0000000000000010 - 03                     0001   OSO [[DIR]]/test.o
 # CHECK-NEXT:  [[#%x, STATIC:]] - 0[[#MORE_DATA_ID + 1]] 0000 STSYM _static_var
 # CHECK-NEXT:  [[#%x, MAIN:]]   - 0[[#TEXT_ID + 1]]      0000   FUN _main
 # CHECK-NEXT:  0000000000000006 - 00                     0000   FUN
+# CHECK-NEXT:  [[#%x, FUN:]]    - 0[[#MORE_TEXT_ID + 1]] 0000   FUN _fun
+# CHECK-NEXT:  0000000000000001 - 00                     0000   FUN
 # CHECK-NEXT:  [[#%x, GLOB:]]   - 0[[#DATA_ID + 1]]      0000  GSYM _global_var
 # CHECK-NEXT:  [[#%x, ZERO:]]   - 0[[#COMM_ID + 1]]      0000  GSYM _zero
 # CHECK-NEXT:  0000000000000000 - 01                     0000    SO
@@ -53,6 +56,7 @@
 # CHECK-NEXT:  [[#STATIC]]      s _static_var
 # CHECK-NEXT:  [[#MAIN]]        T _main
 # CHECK-NEXT:  {{[0-9af]+}}     A _abs
+# CHECK-NEXT:  [[#FUN]]         S _fun
 # CHECK-NEXT:  [[#GLOB]]        D _global_var
 # CHECK-NEXT:  [[#ZERO]]        S _zero
 # CHECK-NEXT:  [[#FOO]]         T _foo
@@ -121,6 +125,11 @@ Ldebug_info_end0:
 .subsections_via_symbols
 .section  __DWARF,__debug_line,regular,debug
 
+.section OTHER,more_text,regular,pure_instructions
+.globl _fun
+_fun:
+  ret
+
 #--- foo.s
 .text
 .globl  _foo


        


More information about the llvm-branch-commits mailing list