[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
This commit is contained in:
Jez Ng 2020-12-01 14:45:13 -08:00
parent 78f6498cdc
commit c7dbaec396
4 changed files with 29 additions and 3 deletions

View File

@ -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();
}

View File

@ -76,6 +76,8 @@ public:
std::vector<Reloc> relocs;
};
bool isCodeSection(InputSection *);
extern std::vector<InputSection *> inputSections;
} // namespace macho

View File

@ -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);

View File

@ -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