aboutsummaryrefslogtreecommitdiffstats
path: root/tools/obj2yaml
diff options
context:
space:
mode:
authorStephen Hines <srhines@google.com>2014-05-29 02:49:00 -0700
committerStephen Hines <srhines@google.com>2014-05-29 02:49:00 -0700
commitdce4a407a24b04eebc6a376f8e62b41aaa7b071f (patch)
treedcebc53f2b182f145a2e659393bf9a0472cedf23 /tools/obj2yaml
parent220b921aed042f9e520c26cffd8282a94c66c3d5 (diff)
downloadexternal_llvm-dce4a407a24b04eebc6a376f8e62b41aaa7b071f.zip
external_llvm-dce4a407a24b04eebc6a376f8e62b41aaa7b071f.tar.gz
external_llvm-dce4a407a24b04eebc6a376f8e62b41aaa7b071f.tar.bz2
Update LLVM for 3.5 rebase (r209712).
Change-Id: I149556c940fb7dc92d075273c87ff584f400941f
Diffstat (limited to 'tools/obj2yaml')
-rw-r--r--tools/obj2yaml/CMakeLists.txt2
-rw-r--r--tools/obj2yaml/Error.cpp54
-rw-r--r--tools/obj2yaml/Error.h42
-rw-r--r--tools/obj2yaml/coff2yaml.cpp5
-rw-r--r--tools/obj2yaml/elf2yaml.cpp290
-rw-r--r--tools/obj2yaml/obj2yaml.cpp45
-rw-r--r--tools/obj2yaml/obj2yaml.h7
7 files changed, 420 insertions, 25 deletions
diff --git a/tools/obj2yaml/CMakeLists.txt b/tools/obj2yaml/CMakeLists.txt
index 9c10c04..f167ed5 100644
--- a/tools/obj2yaml/CMakeLists.txt
+++ b/tools/obj2yaml/CMakeLists.txt
@@ -4,5 +4,5 @@ set(LLVM_LINK_COMPONENTS
)
add_llvm_utility(obj2yaml
- obj2yaml.cpp coff2yaml.cpp
+ obj2yaml.cpp coff2yaml.cpp elf2yaml.cpp Error.cpp
)
diff --git a/tools/obj2yaml/Error.cpp b/tools/obj2yaml/Error.cpp
new file mode 100644
index 0000000..7be468d
--- /dev/null
+++ b/tools/obj2yaml/Error.cpp
@@ -0,0 +1,54 @@
+//===- Error.cpp - system_error extensions for obj2yaml ---------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Error.h"
+#include "llvm/Support/ErrorHandling.h"
+
+using namespace llvm;
+
+namespace {
+class _obj2yaml_error_category : public error_category {
+public:
+ const char *name() const override;
+ std::string message(int ev) const override;
+ error_condition default_error_condition(int ev) const override;
+};
+} // namespace
+
+const char *_obj2yaml_error_category::name() const { return "obj2yaml"; }
+
+std::string _obj2yaml_error_category::message(int ev) const {
+ switch (ev) {
+ case obj2yaml_error::success:
+ return "Success";
+ case obj2yaml_error::file_not_found:
+ return "No such file.";
+ case obj2yaml_error::unrecognized_file_format:
+ return "Unrecognized file type.";
+ case obj2yaml_error::unsupported_obj_file_format:
+ return "Unsupported object file format.";
+ default:
+ llvm_unreachable("An enumerator of obj2yaml_error does not have a message "
+ "defined.");
+ }
+}
+
+error_condition
+_obj2yaml_error_category::default_error_condition(int ev) const {
+ if (ev == obj2yaml_error::success)
+ return errc::success;
+ return errc::invalid_argument;
+}
+
+namespace llvm {
+const error_category &obj2yaml_category() {
+ static _obj2yaml_error_category o;
+ return o;
+}
+} // namespace llvm
diff --git a/tools/obj2yaml/Error.h b/tools/obj2yaml/Error.h
new file mode 100644
index 0000000..a326664
--- /dev/null
+++ b/tools/obj2yaml/Error.h
@@ -0,0 +1,42 @@
+//===- Error.h - system_error extensions for obj2yaml -----------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_ERROR_H
+#define LLVM_TOOLS_ERROR_H
+
+#include "llvm/Support/system_error.h"
+
+namespace llvm {
+
+const error_category &obj2yaml_category();
+
+struct obj2yaml_error {
+ enum _ {
+ success = 0,
+ file_not_found,
+ unrecognized_file_format,
+ unsupported_obj_file_format
+ };
+ _ v_;
+
+ obj2yaml_error(_ v) : v_(v) {}
+ explicit obj2yaml_error(int v) : v_(_(v)) {}
+ operator int() const {return v_;}
+};
+
+inline error_code make_error_code(obj2yaml_error e) {
+ return error_code(static_cast<int>(e), obj2yaml_category());
+}
+
+template <> struct is_error_code_enum<obj2yaml_error> : std::true_type { };
+template <> struct is_error_code_enum<obj2yaml_error::_> : std::true_type { };
+
+} // namespace llvm
+
+#endif
diff --git a/tools/obj2yaml/coff2yaml.cpp b/tools/obj2yaml/coff2yaml.cpp
index ef70922..42b09d3 100644
--- a/tools/obj2yaml/coff2yaml.cpp
+++ b/tools/obj2yaml/coff2yaml.cpp
@@ -210,10 +210,7 @@ COFFYAML::Object &COFFDumper::getYAMLObj() {
return YAMLObj;
}
-error_code coff2yaml(raw_ostream &Out, MemoryBuffer *Buff) {
- error_code ec;
- object::COFFObjectFile Obj(Buff, ec);
- check(ec);
+error_code coff2yaml(raw_ostream &Out, const object::COFFObjectFile &Obj) {
COFFDumper Dumper(Obj);
yaml::Output Yout(Out);
diff --git a/tools/obj2yaml/elf2yaml.cpp b/tools/obj2yaml/elf2yaml.cpp
new file mode 100644
index 0000000..7642921
--- /dev/null
+++ b/tools/obj2yaml/elf2yaml.cpp
@@ -0,0 +1,290 @@
+//===------ utils/elf2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Error.h"
+#include "obj2yaml.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/ELFYAML.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/YAMLTraits.h"
+
+using namespace llvm;
+
+namespace {
+
+template <class ELFT>
+class ELFDumper {
+ typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
+ typedef typename object::ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
+
+ const object::ELFFile<ELFT> &Obj;
+
+ error_code dumpSymbol(Elf_Sym_Iter Sym, ELFYAML::Symbol &S);
+ error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
+ template <class RelT>
+ error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel,
+ ELFYAML::Relocation &R);
+
+ ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr);
+ ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr);
+ ErrorOr<ELFYAML::RawContentSection *>
+ dumpContentSection(const Elf_Shdr *Shdr);
+
+public:
+ ELFDumper(const object::ELFFile<ELFT> &O);
+ ErrorOr<ELFYAML::Object *> dump();
+};
+
+}
+
+template <class ELFT>
+ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O)
+ : Obj(O) {}
+
+template <class ELFT>
+ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
+ auto Y = make_unique<ELFYAML::Object>();
+
+ // Dump header
+ Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass());
+ Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding());
+ Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI];
+ Y->Header.Type = Obj.getHeader()->e_type;
+ Y->Header.Machine = Obj.getHeader()->e_machine;
+ Y->Header.Flags = Obj.getHeader()->e_flags;
+ Y->Header.Entry = Obj.getHeader()->e_entry;
+
+ // Dump sections
+ for (const Elf_Shdr &Sec : Obj.sections()) {
+ switch (Sec.sh_type) {
+ case ELF::SHT_NULL:
+ case ELF::SHT_SYMTAB:
+ case ELF::SHT_DYNSYM:
+ case ELF::SHT_STRTAB:
+ // Do not dump these sections.
+ break;
+ case ELF::SHT_RELA: {
+ ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
+ if (error_code EC = S.getError())
+ return EC;
+ Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
+ break;
+ }
+ case ELF::SHT_REL: {
+ ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
+ if (error_code EC = S.getError())
+ return EC;
+ Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
+ break;
+ }
+ default: {
+ ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
+ if (error_code EC = S.getError())
+ return EC;
+ Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
+ }
+ }
+ }
+
+ // Dump symbols
+ bool IsFirstSym = true;
+ for (auto SI = Obj.begin_symbols(), SE = Obj.end_symbols(); SI != SE; ++SI) {
+ if (IsFirstSym) {
+ IsFirstSym = false;
+ continue;
+ }
+
+ ELFYAML::Symbol S;
+ if (error_code EC = ELFDumper<ELFT>::dumpSymbol(SI, S))
+ return EC;
+
+ switch (SI->getBinding())
+ {
+ case ELF::STB_LOCAL:
+ Y->Symbols.Local.push_back(S);
+ break;
+ case ELF::STB_GLOBAL:
+ Y->Symbols.Global.push_back(S);
+ break;
+ case ELF::STB_WEAK:
+ Y->Symbols.Weak.push_back(S);
+ break;
+ default:
+ llvm_unreachable("Unknown ELF symbol binding");
+ }
+ }
+
+ return Y.release();
+}
+
+template <class ELFT>
+error_code ELFDumper<ELFT>::dumpSymbol(Elf_Sym_Iter Sym, ELFYAML::Symbol &S) {
+ S.Type = Sym->getType();
+ S.Value = Sym->st_value;
+ S.Size = Sym->st_size;
+
+ ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym);
+ if (error_code EC = NameOrErr.getError())
+ return EC;
+ S.Name = NameOrErr.get();
+
+ const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
+ if (!Shdr)
+ return obj2yaml_error::success;
+
+ NameOrErr = Obj.getSectionName(Shdr);
+ if (error_code EC = NameOrErr.getError())
+ return EC;
+ S.Section = NameOrErr.get();
+
+ return obj2yaml_error::success;
+}
+
+template <class ELFT>
+template <class RelT>
+error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
+ const RelT *Rel,
+ ELFYAML::Relocation &R) {
+ R.Type = Rel->getType(Obj.isMips64EL());
+ R.Offset = Rel->r_offset;
+ R.Addend = 0;
+
+ auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
+ if (!NamePair.first)
+ return obj2yaml_error::success;
+
+ ErrorOr<StringRef> NameOrErr =
+ Obj.getSymbolName(NamePair.first, NamePair.second);
+ if (error_code EC = NameOrErr.getError())
+ return EC;
+ R.Symbol = NameOrErr.get();
+
+ return obj2yaml_error::success;
+}
+
+template <class ELFT>
+error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
+ ELFYAML::Section &S) {
+ S.Type = Shdr->sh_type;
+ S.Flags = Shdr->sh_flags;
+ S.Address = Shdr->sh_addr;
+ S.AddressAlign = Shdr->sh_addralign;
+
+ ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
+ if (error_code EC = NameOrErr.getError())
+ return EC;
+ S.Name = NameOrErr.get();
+
+ if (Shdr->sh_link != ELF::SHN_UNDEF) {
+ if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
+ NameOrErr = Obj.getSectionName(LinkSection);
+ if (error_code EC = NameOrErr.getError())
+ return EC;
+ S.Link = NameOrErr.get();
+ }
+ }
+ if (Shdr->sh_info != ELF::SHN_UNDEF) {
+ if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
+ NameOrErr = Obj.getSectionName(InfoSection);
+ if (error_code EC = NameOrErr.getError())
+ return EC;
+ S.Info = NameOrErr.get();
+ }
+ }
+ return obj2yaml_error::success;
+}
+
+template <class ELFT>
+ErrorOr<ELFYAML::RelocationSection *>
+ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
+ assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
+ auto S = make_unique<ELFYAML::RelocationSection>();
+
+ if (error_code EC = dumpCommonSection(Shdr, *S))
+ return EC;
+
+ for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE;
+ ++RI) {
+ ELFYAML::Relocation R;
+ if (error_code EC = dumpRelocation(Shdr, &*RI, R))
+ return EC;
+ S->Relocations.push_back(R);
+ }
+
+ return S.release();
+}
+
+template <class ELFT>
+ErrorOr<ELFYAML::RelocationSection *>
+ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
+ assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
+ auto S = make_unique<ELFYAML::RelocationSection>();
+
+ if (error_code EC = dumpCommonSection(Shdr, *S))
+ return EC;
+
+ for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE;
+ ++RI) {
+ ELFYAML::Relocation R;
+ if (error_code EC = dumpRelocation(Shdr, &*RI, R))
+ return EC;
+ R.Addend = RI->r_addend;
+ S->Relocations.push_back(R);
+ }
+
+ return S.release();
+}
+
+template <class ELFT>
+ErrorOr<ELFYAML::RawContentSection *>
+ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
+ auto S = make_unique<ELFYAML::RawContentSection>();
+
+ if (error_code EC = dumpCommonSection(Shdr, *S))
+ return EC;
+
+ ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
+ if (error_code EC = ContentOrErr.getError())
+ return EC;
+ S->Content = object::yaml::BinaryRef(ContentOrErr.get());
+ S->Size = S->Content.binary_size();
+
+ return S.release();
+}
+
+template <class ELFT>
+static error_code elf2yaml(raw_ostream &Out, const object::ELFFile<ELFT> &Obj) {
+ ELFDumper<ELFT> Dumper(Obj);
+ ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
+ if (error_code EC = YAMLOrErr.getError())
+ return EC;
+
+ std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
+ yaml::Output Yout(Out);
+ Yout << *YAML;
+
+ return object::object_error::success;
+}
+
+error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
+ if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
+ return elf2yaml(Out, *ELFObj->getELFFile());
+
+ if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
+ return elf2yaml(Out, *ELFObj->getELFFile());
+
+ if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
+ return elf2yaml(Out, *ELFObj->getELFFile());
+
+ if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
+ return elf2yaml(Out, *ELFObj->getELFFile());
+
+ return obj2yaml_error::unsupported_obj_file_format;
+}
diff --git a/tools/obj2yaml/obj2yaml.cpp b/tools/obj2yaml/obj2yaml.cpp
index 38779fe..7fe034d 100644
--- a/tools/obj2yaml/obj2yaml.cpp
+++ b/tools/obj2yaml/obj2yaml.cpp
@@ -7,6 +7,7 @@
//
//===----------------------------------------------------------------------===//
+#include "Error.h"
#include "obj2yaml.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/COFF.h"
@@ -16,16 +17,32 @@
#include "llvm/Support/Signals.h"
using namespace llvm;
+using namespace llvm::object;
-namespace {
-enum ObjectFileType {
- coff
-};
+static error_code dumpObject(const ObjectFile &Obj) {
+ if (Obj.isCOFF())
+ return coff2yaml(outs(), cast<COFFObjectFile>(Obj));
+ if (Obj.isELF())
+ return elf2yaml(outs(), Obj);
+
+ return obj2yaml_error::unsupported_obj_file_format;
}
-cl::opt<ObjectFileType> InputFormat(
- cl::desc("Choose input format"),
- cl::values(clEnumVal(coff, "process COFF object files"), clEnumValEnd));
+static error_code dumpInput(StringRef File) {
+ if (File != "-" && !sys::fs::exists(File))
+ return obj2yaml_error::file_not_found;
+
+ ErrorOr<Binary *> BinaryOrErr = createBinary(File);
+ if (error_code EC = BinaryOrErr.getError())
+ return EC;
+
+ std::unique_ptr<Binary> Binary(BinaryOrErr.get());
+ // TODO: If this is an archive, then burst it and dump each entry
+ if (ObjectFile *Obj = dyn_cast<ObjectFile>(Binary.get()))
+ return dumpObject(*Obj);
+
+ return obj2yaml_error::unrecognized_file_format;
+}
cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
cl::init("-"));
@@ -36,17 +53,9 @@ int main(int argc, char *argv[]) {
PrettyStackTraceProgram X(argc, argv);
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
- // Process the input file
- std::unique_ptr<MemoryBuffer> buf;
-
- // TODO: If this is an archive, then burst it and dump each entry
- if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, buf)) {
- errs() << "Error: '" << ec.message() << "' opening file '" << InputFilename
- << "'\n";
- } else {
- ec = coff2yaml(outs(), buf.release());
- if (ec)
- errs() << "Error: " << ec.message() << " dumping COFF file\n";
+ if (error_code EC = dumpInput(InputFilename)) {
+ errs() << "Error: '" << EC.message() << "'\n";
+ return 1;
}
return 0;
diff --git a/tools/obj2yaml/obj2yaml.h b/tools/obj2yaml/obj2yaml.h
index bde82e6..73c58fa 100644
--- a/tools/obj2yaml/obj2yaml.h
+++ b/tools/obj2yaml/obj2yaml.h
@@ -13,10 +13,13 @@
#ifndef LLVM_TOOLS_OBJ2YAML_H
#define LLVM_TOOLS_OBJ2YAML_H
-#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Object/COFF.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
-llvm::error_code coff2yaml(llvm::raw_ostream &Out, llvm::MemoryBuffer *TheObj);
+llvm::error_code coff2yaml(llvm::raw_ostream &Out,
+ const llvm::object::COFFObjectFile &Obj);
+llvm::error_code elf2yaml(llvm::raw_ostream &Out,
+ const llvm::object::ObjectFile &Obj);
#endif