Skip to content

Commit 60c8135

Browse files
committed
[tools] [llvm-nm] Default to reading from stdin not a.out
Summary: This moves away from defaulting to a.out and uses stdin only if stdin has a file redirected to it. This has been discussed on the llvm-dev mailing list [[ https://lists.llvm.org/pipermail/llvm-dev/2019-July/133642.html | here ]]. Reviewers: jhenderson, rupprecht, MaskRay, chrisjackson Reviewed By: jhenderson, MaskRay Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D64290 llvm-svn: 365889
1 parent 0f7146d commit 60c8135

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

llvm/docs/CommandGuide/llvm-nm.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ DESCRIPTION
1313

1414
The :program:`llvm-nm` utility lists the names of symbols from LLVM bitcode
1515
files, object files, and archives. Each symbol is listed along with some simple
16-
information about its provenance. If no filename is specified, *a.out* is used
17-
as the input. If *-* is used as a filename, :program:`llvm-nm` will read a file
18-
from its standard input stream.
16+
information about its provenance. If no filename is specified, or *-* is used as
17+
a filename, :program:`llvm-nm` will read a file from its standard input stream.
1918

2019
:program:`llvm-nm`'s default output format is the traditional BSD :program:`nm`
2120
output format. Each such output record consists of an (optional) 8-digit

llvm/test/tools/llvm-nm/stdin.test

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Test llvm-nm when using stdin both explicitly (using '-' as a filename)
2+
## and implicitly (not specifying any filename).
3+
4+
# RUN: yaml2obj %s -o %t.o
5+
6+
## Pass an explicit filename to produce a baseline output. llvm-nm should
7+
## have the same behavior when opening a file itself and when reading that
8+
## file from its standard input stream.
9+
# RUN: llvm-nm %t.o > %t.base 2> %t.err
10+
11+
## Make sure there is no warning message about no file redirected to stdin.
12+
# RUN: FileCheck %s --input-file=%t.err --allow-empty --implicit-check-not={{.}}
13+
14+
# RUN: llvm-nm - < %t.o > %t.explicit 2> %t.err
15+
# RUN: FileCheck %s --input-file=%t.err --allow-empty --implicit-check-not={{.}}
16+
# RUN: cmp %t.base %t.explicit
17+
18+
# RUN: llvm-nm < %t.o > %t.implicit 2> %t.err
19+
# RUN: FileCheck %s --input-file=%t.err --allow-empty --implicit-check-not={{.}}
20+
# RUN: cmp %t.base %t.implicit
21+
22+
!ELF
23+
FileHeader:
24+
Class: ELFCLASS64
25+
Data: ELFDATA2LSB
26+
Type: ET_REL
27+
Machine: EM_X86_64
28+
Sections:
29+
- Name: .text
30+
Type: SHT_PROGBITS
31+
Symbols:
32+
- Name: symbol_a
33+
Section: .text

llvm/tools/llvm-nm/llvm-nm.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "llvm/Support/Format.h"
3535
#include "llvm/Support/InitLLVM.h"
3636
#include "llvm/Support/MemoryBuffer.h"
37+
#include "llvm/Support/Process.h"
3738
#include "llvm/Support/Program.h"
3839
#include "llvm/Support/Signals.h"
3940
#include "llvm/Support/TargetSelect.h"
@@ -1751,6 +1752,12 @@ static bool checkMachOAndArchFlags(SymbolicFile *O, std::string &Filename) {
17511752
}
17521753

17531754
static void dumpSymbolNamesFromFile(std::string &Filename) {
1755+
if (Filename == "-" && sys::Process::StandardInIsUserInput()) {
1756+
WithColor::warning(errs(), ToolName) << "can't read from terminal\n";
1757+
cl::PrintHelpMessage();
1758+
HadError = true;
1759+
return;
1760+
}
17541761
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
17551762
MemoryBuffer::getFileOrSTDIN(Filename);
17561763
if (error(BufferOrErr.getError(), Filename))
@@ -2082,7 +2089,7 @@ int main(int argc, char **argv) {
20822089
if (OutputFormat == sysv || SizeSort)
20832090
PrintSize = true;
20842091
if (InputFilenames.empty())
2085-
InputFilenames.push_back("a.out");
2092+
InputFilenames.push_back("-");
20862093
if (InputFilenames.size() > 1)
20872094
MultipleFiles = true;
20882095

0 commit comments

Comments
 (0)