Index: llvm/docs/CommandGuide/llvm-objdump.rst =================================================================== --- llvm/docs/CommandGuide/llvm-objdump.rst +++ llvm/docs/CommandGuide/llvm-objdump.rst @@ -11,10 +11,9 @@ DESCRIPTION ----------- The :program:`llvm-objdump` utility prints the contents of object files and -final linked images named on the command line. If no file name is specified, -:program:`llvm-objdump` will attempt to read from *a.out*. If *-* is used as a -file name, :program:`llvm-objdump` will process a file on its standard input -stream. +final linked images named on the command line. If no filename is specified, +or *-* is used as a filename, :program:`llvm-objdump` will read a file from +its standard input stream. COMMANDS -------- Index: llvm/test/tools/llvm-objdump/stdin.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objdump/stdin.test @@ -0,0 +1,45 @@ +## Test llvm-objdump when using stdin both explicitly (using '-' as a filename) +## and implicitly (not specifying any filename). + +# RUN: yaml2obj %s > a.out + +## This will fail because llvm-lit redirects to stdin by default, it will be +## read but is not a valid object file, test that it fails because of this +## and does not use a.out as the default file. +# RUN: not llvm-objdump -h 2>&1 | FileCheck %s +# CHECK-NOT: error: 'a.out': No such file or directory + +## This is a hack to get the base output to use the filename '' which +## is used as the filename in the llvm-objdumps output when reading a file +## from standard input. +# RUN: cp a.out \ + +## Pass an explicit filename to produce a baseline output. llvm-objdump should +## have the same behavior when opening a file itself and when reading that +## file from its standard input stream. +# RUN: llvm-objdump -h \ > %t.base 2> %t.err +# RUN: FileCheck %s --input-file=%t.err --allow-empty --implicit-check-not={{.}} + +# RUN: llvm-objdump -h - < a.out > %t.explicit 2> %t.err +# RUN: FileCheck %s --input-file=%t.err --allow-empty --implicit-check-not={{.}} +# UN: cmp %t.base %t.explicit + +# RUN: llvm-objdump -h < a.out > %t.implicit 2> %t.err +# RUN: FileCheck %s --input-file=%t.err --allow-empty --implicit-check-not={{.}} +# RUN: cmp %t.base %t.implicit + +# RUN: rm -f a.out \ + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [SHF_ALLOC, SHF_EXECINSTR] + Address: 0x400 + AddressAlign: 0x10 + Content: 'c3' Index: llvm/tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- llvm/tools/llvm-objdump/llvm-objdump.cpp +++ llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -55,6 +55,7 @@ #include "llvm/Support/Host.h" #include "llvm/Support/InitLLVM.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Process.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/StringSaver.h" #include "llvm/Support/TargetRegistry.h" @@ -2107,6 +2108,12 @@ /// Open file and figure out how to dump it. static void dumpInput(StringRef file) { + if (file == "-" && sys::Process::StandardInIsUserInput()) { + WithColor::warning(errs(), ToolName) << "can't read from terminal\n"; + cl::PrintHelpMessage(); + exit(1); + } + // If we are using the Mach-O specific object file parser, then let it parse // the file and process the command line options. So the -arch flags can // be used to select specific slices, etc. @@ -2151,9 +2158,9 @@ ToolName = argv[0]; - // Defaults to a.out if no filenames specified. + // Defaults to stdin if no filenames are specified. if (InputFilenames.empty()) - InputFilenames.push_back("a.out"); + InputFilenames.push_back("-"); if (AllHeaders) ArchiveHeaders = FileHeaders = PrivateHeaders = Relocations =