mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-10-18 10:32:48 +02:00
llvm-strings: trivialise logic until we support more options
Until we have handling for ignoring unloaded sections, simplify the logic to the point of triviality. This fixes the scanning of archives, particularly when embedded in archives. llvm-svn: 286727
This commit is contained in:
parent
e9f11fdb64
commit
b4ba74c4c4
13
test/tools/llvm-strings/nested-archives.test
Normal file
13
test/tools/llvm-strings/nested-archives.test
Normal file
@ -0,0 +1,13 @@
|
||||
RUN: echo -n abcd > %T/abcd
|
||||
RUN: rm -f %T/inner.ar
|
||||
RUN: llvm-ar crs %T/inner.a %T/abcd
|
||||
RUN: rm -f %T/outer.ar
|
||||
RUN: llvm-ar crs %T/outer.a %T/inner.a
|
||||
RUN: llvm-strings %T/outer.a | FileCheck %s
|
||||
|
||||
CHECK: !<arch>
|
||||
CHECK: inner.a/ 0 0 0 644 72 `
|
||||
CHECK: !<arch>
|
||||
CHECK: abcd/ 0 0 0 644 4 `
|
||||
CHECK: abcd
|
||||
|
@ -12,10 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/IR/LLVMContext.h"
|
||||
#include "llvm/Object/Archive.h"
|
||||
#include "llvm/Object/Binary.h"
|
||||
#include "llvm/Object/ObjectFile.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/Error.h"
|
||||
#include "llvm/Support/MemoryBuffer.h"
|
||||
@ -32,7 +29,7 @@ static cl::list<std::string> InputFileNames(cl::Positional,
|
||||
cl::desc("<input object files>"),
|
||||
cl::ZeroOrMore);
|
||||
|
||||
static void dump(raw_ostream &OS, StringRef Contents) {
|
||||
static void strings(raw_ostream &OS, StringRef Contents) {
|
||||
const char *P = nullptr, *E = nullptr, *S = nullptr;
|
||||
for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
|
||||
if (std::isgraph(*P) || std::isblank(*P)) {
|
||||
@ -48,64 +45,6 @@ static void dump(raw_ostream &OS, StringRef Contents) {
|
||||
OS << StringRef(S, E - S) << '\n';
|
||||
}
|
||||
|
||||
namespace {
|
||||
class Strings {
|
||||
LLVMContext Context;
|
||||
raw_ostream &OS;
|
||||
|
||||
void dump(const ObjectFile *O) {
|
||||
for (const auto &S : O->sections()) {
|
||||
StringRef Contents;
|
||||
if (!S.getContents(Contents))
|
||||
::dump(OS, Contents);
|
||||
}
|
||||
}
|
||||
|
||||
void dump(const Archive *A) {
|
||||
Error E = Error::success();
|
||||
for (auto &Element : A->children(E)) {
|
||||
if (Expected<std::unique_ptr<Binary>> Child =
|
||||
Element.getAsBinary(&Context)) {
|
||||
dump(dyn_cast<ObjectFile>(&**Child));
|
||||
} else {
|
||||
if (auto E = isNotObjectErrorInvalidFileType(Child.takeError())) {
|
||||
errs() << A->getFileName();
|
||||
if (Expected<StringRef> Name = Element.getName())
|
||||
errs() << '(' << *Name << ')';
|
||||
logAllUnhandledErrors(std::move(E), errs(), "");
|
||||
errs() << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
(void)static_cast<bool>(E);
|
||||
}
|
||||
|
||||
public:
|
||||
Strings(raw_ostream &S) : OS(S) {}
|
||||
|
||||
void scan(StringRef File) {
|
||||
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
|
||||
MemoryBuffer::getFileOrSTDIN(File);
|
||||
if (std::error_code EC = Buffer.getError()) {
|
||||
errs() << File << ": " << EC.message() << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
if (Expected<std::unique_ptr<Binary>> B =
|
||||
createBinary(Buffer.get()->getMemBufferRef(), &Context)) {
|
||||
if (auto *A = dyn_cast<Archive>(&**B))
|
||||
return dump(A);
|
||||
if (auto *O = dyn_cast<ObjectFile>(&**B))
|
||||
return dump(O);
|
||||
::dump(OS, Buffer.get()->getMemBufferRef().getBuffer());
|
||||
} else {
|
||||
consumeError(B.takeError());
|
||||
::dump(OS, Buffer.get()->getMemBufferRef().getBuffer());
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
||||
PrettyStackTraceProgram X(argc, argv);
|
||||
@ -115,9 +54,14 @@ int main(int argc, char **argv) {
|
||||
if (InputFileNames.empty())
|
||||
InputFileNames.push_back("-");
|
||||
|
||||
Strings S(llvm::outs());
|
||||
std::for_each(InputFileNames.begin(), InputFileNames.end(),
|
||||
[&S](StringRef F) { S.scan(F); });
|
||||
for (const auto &File : InputFileNames) {
|
||||
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
|
||||
MemoryBuffer::getFileOrSTDIN(File);
|
||||
if (std::error_code EC = Buffer.getError())
|
||||
errs() << File << ": " << EC.message() << '\n';
|
||||
else
|
||||
strings(llvm::outs(), Buffer.get()->getMemBufferRef().getBuffer());
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user