1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 11:13:28 +01:00

[llvm-nm] Print an explicit "no symbols" message when an object file has no symbols

Summary:
GNU nm (and other nm implementations, such as "go tool nm") prints an explicit "no symbols" message when an object file has no symbols. Currently llvm-nm just doesn't print anything. Adding an explicit "no symbols" message will allow llvm-nm to be used in place of nm: some scripts and build processes use `nm <file> | grep "no symbols"` as a test to see if a file has no symbols. It will also be more familiar to anyone used to nm.

That said, the format implemented here is slightly different, in that it doesn't print the tool name in the message (which IMHO is not useful to include).

Demo:
```
$ for nm in nm bin/llvm-nm ; do echo "nm implementation: $nm"; $nm /tmp/foo{1,2}.o; echo; done
nm implementation: nm

/tmp/foo1.o:
nm: /tmp/foo1.o: no symbols

/tmp/foo2.o:
0000000000000000 T foo2

nm implementation: bin/llvm-nm

/tmp/foo1.o:
no symbols

/tmp/foo2.o:
0000000000000000 T foo2
```

Reviewers: MaskRay

Reviewed By: MaskRay

Subscribers: llvm-commits

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

llvm-svn: 343742
This commit is contained in:
Jordan Rupprecht 2018-10-03 23:39:49 +00:00
parent b42ed71c63
commit d5effeb47c
6 changed files with 43 additions and 15 deletions

View File

@ -30,4 +30,5 @@ RUN: | FileCheck %s -check-prefix ERROR
ERROR: File format has no dynamic symbol table.
RUN: llvm-nm -D %p/Inputs/trivial-object-test.elf-i386 | count 0
RUN: llvm-nm -D %p/Inputs/trivial-object-test.elf-i386 | FileCheck %s -check-prefix=NO-SYMBOLS
NO-SYMBOLS: no symbols

View File

@ -3,7 +3,8 @@
; RUN: rm -f %t2.0
; RUN: llvm-lto2 run %t.bc -r %t.bc,foo,pl -o %t2 -thinlto-distributed-indexes
; RUN: llvm-readobj -h %t2.0 | FileCheck %s
; RUN: llvm-nm %t2.0 | count 0
; RUN: llvm-nm %t2.0 | FileCheck %s -check-prefix=NO-SYMBOLS
; NO-SYMBOLS: no symbols
; CHECK: Format: ELF64-x86-64

View File

@ -2,7 +2,9 @@
; RUN: llvm-as -o %t/bcsection.bc %s
; RUN: llvm-mc -I=%t -filetype=obj -triple=x86_64-unknown-unknown -o %t/bcsection.bco %p/Inputs/bcsection.s
; RUN: llvm-nm -no-llvm-bc %t/bcsection.bco | count 0
; RUN: llvm-nm -no-llvm-bc %t/bcsection.bco | FileCheck %s -check-prefix=NO-SYMBOLS
; NO-SYMBOLS: no symbols
; RUN: %gold -r -o %t/bcsection.o -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext %t/bcsection.bco
; RUN: llvm-nm -no-llvm-bc %t/bcsection.o | FileCheck %s

View File

@ -83,7 +83,8 @@
; RUN: --plugin-opt=obj-path=%t5.o \
; RUN: -shared %t.o %t2.o -o %t4
; RUN: llvm-readobj -h %t5.o | FileCheck %s --check-prefix=FORMAT
; RUN: llvm-nm %t5.o | count 0
; RUN: llvm-nm %t5.o | FileCheck %s -check-prefix=NO-SYMBOLS
; NO-SYMBOLS: no symbols
; NM: T f
; NM2: T {{f|g}}

View File

@ -0,0 +1,14 @@
# RUN: yaml2obj %s > %t.o
# RUN: llvm-nm %t.o | FileCheck %s
# RUN: llvm-nm --print-file-name %t.o | FileCheck %s --check-prefix=CHECK-PRINT-FILE-NAME
!ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_X86_64
# CHECK: {{^}}no symbols{{$}}
# CHECK-PRINT-FILE-NAME: nm-no-symbols.test{{.*}}.o: no symbols{{$}}

View File

@ -757,6 +757,24 @@ static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
}
}
auto writeFileName = [&]() {
if (!ArchitectureName.empty())
outs() << "(for architecture " << ArchitectureName << "):";
if (OutputFormat == posix && !ArchiveName.empty())
outs() << ArchiveName << "[" << CurrentFilename << "]: ";
else {
if (!ArchiveName.empty())
outs() << ArchiveName << ":";
outs() << CurrentFilename << ": ";
}
};
if (SymbolList.empty()) {
if (PrintFileName)
writeFileName();
outs() << "no symbols\n";
}
for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end();
I != E; ++I) {
uint32_t SymFlags;
@ -778,17 +796,8 @@ static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
(!Global && ExternalOnly) || (SizeSort && !PrintAddress) ||
(Weak && NoWeakSymbols))
continue;
if (PrintFileName) {
if (!ArchitectureName.empty())
outs() << "(for architecture " << ArchitectureName << "):";
if (OutputFormat == posix && !ArchiveName.empty())
outs() << ArchiveName << "[" << CurrentFilename << "]: ";
else {
if (!ArchiveName.empty())
outs() << ArchiveName << ":";
outs() << CurrentFilename << ": ";
}
}
if (PrintFileName)
writeFileName();
if ((JustSymbolName ||
(UndefinedOnly && MachO && OutputFormat != darwin)) &&
OutputFormat != posix) {