diff --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h --- a/lld/MachO/InputSection.h +++ b/lld/MachO/InputSection.h @@ -76,6 +76,8 @@ std::vector relocs; }; +bool isCodeSection(InputSection *); + extern std::vector inputSections; } // namespace macho diff --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp --- a/lld/MachO/InputSection.cpp +++ b/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(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/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp --- a/lld/MachO/SyntheticSections.cpp +++ b/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); diff --git a/lld/test/MachO/stabs.s b/lld/test/MachO/stabs.s --- 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 @@ .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