[PATCH] D92430: [lld-macho] Add isCodeSection()

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 1 14:08:44 PST 2020


int3 created this revision.
int3 added reviewers: lld-macho, clayborg.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
int3 requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92430

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


Index: lld/test/MachO/stabs.s
===================================================================
--- lld/test/MachO/stabs.s
+++ 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 @@
 .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
Index: lld/MachO/SyntheticSections.cpp
===================================================================
--- lld/MachO/SyntheticSections.cpp
+++ lld/MachO/SyntheticSections.cpp
@@ -668,9 +668,7 @@
     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);
Index: lld/MachO/InputSection.h
===================================================================
--- lld/MachO/InputSection.h
+++ lld/MachO/InputSection.h
@@ -76,6 +76,8 @@
   std::vector<Reloc> relocs;
 };
 
+bool isCodeSection(InputSection *);
+
 extern std::vector<InputSection *> inputSections;
 
 } // namespace macho
Index: lld/MachO/InputSection.cpp
===================================================================
--- lld/MachO/InputSection.cpp
+++ lld/MachO/InputSection.cpp
@@ -58,6 +58,23 @@
   }
 }
 
+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();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92430.308767.patch
Type: text/x-patch
Size: 3350 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201201/62a76fe2/attachment.bin>


More information about the llvm-commits mailing list