diff --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp index a7b0f08c8d95..c43022a6b9d3 100644 --- a/lld/ELF/MarkLive.cpp +++ b/lld/ELF/MarkLive.cpp @@ -202,6 +202,9 @@ void MarkLive::resolveReloc(InputSectionBase &sec, // LSDAs and personality functions if we found that they were unused. template void MarkLive::scanEhFrameSection(EhInputSection &eh) { + if (TrackWhyLive) + whyLive.try_emplace(&eh, + LiveReason{std::nullopt, "exception handling frame"}); ArrayRef rels = eh.rels; for (const EhSectionPiece &cie : eh.cies) if (cie.firstRelocation != unsigned(-1)) @@ -443,9 +446,13 @@ void MarkLive::run() { for (Symbol *sym : ctx.symtab->getSymbols()) handleSym(sym); + // Handle local symbols, skipping the symbol at index 0 and section + // symbols, which usually have empty names and technically not live. Note: + // a live section may lack an associated section symbol, making them + // unreliable liveness indicators. for (ELFFileBase *file : ctx.objectFiles) for (Symbol *sym : file->getSymbols()) - if (sym->isLocal()) + if (sym->isLocal() && sym->isDefined() && !sym->isSection()) handleSym(sym); } } diff --git a/lld/test/ELF/why-live.test b/lld/test/ELF/why-live.test index d8f8cc7b6db6..a281f9942985 100644 --- a/lld/test/ELF/why-live.test +++ b/lld/test/ELF/why-live.test @@ -4,6 +4,7 @@ # RUN: llvm-mc -n -filetype=obj -triple=x86_64 -o shared.o shared.s # RUN: ld.lld -shared shared.o -o a.so # RUN: llvm-mc -n -filetype=obj -triple=x86_64 -o a.o a.s +# RUN: llvm-mc -n -filetype=obj -triple=x86_64 -o b.o b.s #--- shared.s .globl test_shared @@ -159,3 +160,46 @@ test_local: # MULTIPLE-DAG: live symbol: a.o:(test_section_offset) # MULTIPLE-DAG: live symbol: a.o:(test_section_offset_within_symbol) # MULTIPLE-NOT: live symbol + +#--- b.s +## --why-live='*' skips symbol at index 0 and section symbols. +# RUN: ld.lld b.o --threads=1 --gc-sections --why-live="*" | FileCheck %s --check-prefix=STAR --match-full-lines +# STAR-NOT: {{.}} +# STAR: live symbol: b.o:(_start) (entry point) +# STAR-NEXT: live symbol: b.o:(b.s) (no section) +# STAR-NEXT: live symbol: b.o:(str1) +# STAR-NEXT: >>> referenced by: b.o:(.alloc) +# STAR-NEXT: >>> referenced by: b.o:(.text) +# STAR-NEXT: >>> contained live symbol: b.o:(_start) (entry point) +# STAR-NEXT: live symbol: b.o:(note1) +# STAR-NEXT: >>> in live section: b.o:(.note.1) (reserved) +# STAR-NEXT: live symbol: b.o:(__FRAME_END__) +# STAR-NEXT: >>> in live section: b.o:(.eh_frame) (exception handling frame) +# STAR-NOT: {{.}} + +## STT_FILE symbol +.file "b.s" + +.text +.globl _start +_start: + call .alloc + +.section .alloc,"a" + lea str1(%rip), %rax + +.section .nonalloc + .long .nonalloc + +.section .note.1,"a",@note +note1: + +## GCC crtendS.o has such a local symbol relative to an explicit .eh_frame +## section. +.section .eh_frame,"a" +__FRAME_END__: + .long 0 + +.section .rodata.str1.1,"aMS",@progbits,1 +str1: + .asciz "str1"