diff options
Diffstat (limited to 'tools/obj2yaml')
| -rw-r--r-- | tools/obj2yaml/coff2yaml.cpp | 203 | ||||
| -rw-r--r-- | tools/obj2yaml/obj2yaml.cpp | 58 | ||||
| -rw-r--r-- | tools/obj2yaml/obj2yaml.h | 6 |
3 files changed, 130 insertions, 137 deletions
diff --git a/tools/obj2yaml/coff2yaml.cpp b/tools/obj2yaml/coff2yaml.cpp index f0241d931e60..5106a4a44b60 100644 --- a/tools/obj2yaml/coff2yaml.cpp +++ b/tools/obj2yaml/coff2yaml.cpp @@ -10,6 +10,7 @@ #include "obj2yaml.h" #include "llvm/Object/COFF.h" +using namespace llvm; template <typename One, typename Two> struct pod_pair { // I'd much rather use std::pair, but it's not a POD @@ -17,8 +18,8 @@ struct pod_pair { // I'd much rather use std::pair, but it's not a POD Two second; }; -#define STRING_PAIR(x) {llvm::COFF::x, #x} -static const pod_pair<llvm::COFF::MachineTypes, const char *> +#define STRING_PAIR(x) {COFF::x, #x} +static const pod_pair<COFF::MachineTypes, const char *> MachineTypePairs [] = { STRING_PAIR(IMAGE_FILE_MACHINE_UNKNOWN), STRING_PAIR(IMAGE_FILE_MACHINE_AM33), @@ -43,7 +44,7 @@ MachineTypePairs [] = { STRING_PAIR(IMAGE_FILE_MACHINE_WCEMIPSV2) }; -static const pod_pair<llvm::COFF::SectionCharacteristics, const char *> +static const pod_pair<COFF::SectionCharacteristics, const char *> SectionCharacteristicsPairs1 [] = { STRING_PAIR(IMAGE_SCN_TYPE_NO_PAD), STRING_PAIR(IMAGE_SCN_CNT_CODE), @@ -60,7 +61,7 @@ SectionCharacteristicsPairs1 [] = { STRING_PAIR(IMAGE_SCN_MEM_PRELOAD) }; -static const pod_pair<llvm::COFF::SectionCharacteristics, const char *> +static const pod_pair<COFF::SectionCharacteristics, const char *> SectionCharacteristicsPairsAlignment [] = { STRING_PAIR(IMAGE_SCN_ALIGN_1BYTES), STRING_PAIR(IMAGE_SCN_ALIGN_2BYTES), @@ -78,7 +79,7 @@ SectionCharacteristicsPairsAlignment [] = { STRING_PAIR(IMAGE_SCN_ALIGN_8192BYTES) }; -static const pod_pair<llvm::COFF::SectionCharacteristics, const char *> +static const pod_pair<COFF::SectionCharacteristics, const char *> SectionCharacteristicsPairs2 [] = { STRING_PAIR(IMAGE_SCN_LNK_NRELOC_OVFL), STRING_PAIR(IMAGE_SCN_MEM_DISCARDABLE), @@ -89,8 +90,8 @@ SectionCharacteristicsPairs2 [] = { STRING_PAIR(IMAGE_SCN_MEM_READ), STRING_PAIR(IMAGE_SCN_MEM_WRITE) }; - -static const pod_pair<llvm::COFF::SymbolBaseType, const char *> + +static const pod_pair<COFF::SymbolBaseType, const char *> SymbolBaseTypePairs [] = { STRING_PAIR(IMAGE_SYM_TYPE_NULL), STRING_PAIR(IMAGE_SYM_TYPE_VOID), @@ -110,15 +111,15 @@ SymbolBaseTypePairs [] = { STRING_PAIR(IMAGE_SYM_TYPE_DWORD) }; -static const pod_pair<llvm::COFF::SymbolComplexType, const char *> +static const pod_pair<COFF::SymbolComplexType, const char *> SymbolComplexTypePairs [] = { STRING_PAIR(IMAGE_SYM_DTYPE_NULL), STRING_PAIR(IMAGE_SYM_DTYPE_POINTER), STRING_PAIR(IMAGE_SYM_DTYPE_FUNCTION), STRING_PAIR(IMAGE_SYM_DTYPE_ARRAY), }; - -static const pod_pair<llvm::COFF::SymbolStorageClass, const char *> + +static const pod_pair<COFF::SymbolStorageClass, const char *> SymbolStorageClassPairs [] = { STRING_PAIR(IMAGE_SYM_CLASS_END_OF_FUNCTION), STRING_PAIR(IMAGE_SYM_CLASS_NULL), @@ -149,7 +150,7 @@ SymbolStorageClassPairs [] = { STRING_PAIR(IMAGE_SYM_CLASS_CLR_TOKEN), }; -static const pod_pair<llvm::COFF::RelocationTypeX86, const char *> +static const pod_pair<COFF::RelocationTypeX86, const char *> RelocationTypeX86Pairs [] = { STRING_PAIR(IMAGE_REL_I386_ABSOLUTE), STRING_PAIR(IMAGE_REL_I386_DIR16), @@ -181,7 +182,7 @@ RelocationTypeX86Pairs [] = { STRING_PAIR(IMAGE_REL_AMD64_SSPAN32) }; -static const pod_pair<llvm::COFF::RelocationTypesARM, const char *> +static const pod_pair<COFF::RelocationTypesARM, const char *> RelocationTypesARMPairs [] = { STRING_PAIR(IMAGE_REL_ARM_ABSOLUTE), STRING_PAIR(IMAGE_REL_ARM_ADDR32), @@ -201,13 +202,8 @@ RelocationTypesARMPairs [] = { }; #undef STRING_PAIR - -static const char endl = '\n'; - -namespace yaml { // COFF-specific yaml-writing specific routines - -static llvm::raw_ostream &writeName(llvm::raw_ostream &Out, - const char *Name, std::size_t NameSize) { +static raw_ostream &writeName(raw_ostream &Out, + const char *Name, std::size_t NameSize) { for (std::size_t i = 0; i < NameSize; ++i) { if (!Name[i]) break; Out << Name[i]; @@ -217,20 +213,19 @@ static llvm::raw_ostream &writeName(llvm::raw_ostream &Out, // Given an array of pod_pair<enum, const char *>, write all enums that match template <typename T, std::size_t N> -static llvm::raw_ostream &writeBitMask(llvm::raw_ostream &Out, - const pod_pair<T, const char *> (&Arr)[N], unsigned long Val) { +static raw_ostream &writeBitMask(raw_ostream &Out, + const pod_pair<T, const char *> (&Arr)[N], + unsigned long Val) { for (std::size_t i = 0; i < N; ++i) if (Val & Arr[i].first) Out << Arr[i].second << ", "; return Out; } -} // end of yaml namespace - // Given an array of pod_pair<enum, const char *>, look up a value template <typename T, std::size_t N> -const char *nameLookup(const pod_pair<T, const char *> (&Arr)[N], - unsigned long Val, const char *NotFound = NULL) { +const char *nameLookup(const pod_pair<T, const char *> (&Arr)[N], + unsigned long Val, const char *NotFound = NULL) { T n = static_cast<T>(Val); for (std::size_t i = 0; i < N; ++i) if (n == Arr[i].first) @@ -238,124 +233,122 @@ const char *nameLookup(const pod_pair<T, const char *> (&Arr)[N], return NotFound; } - -static llvm::raw_ostream &yamlCOFFHeader( - const llvm::object::coff_file_header *Header,llvm::raw_ostream &Out) { - - Out << "header: !Header" << endl; +static void yamlCOFFHeader(const object::coff_file_header *Header, + raw_ostream &Out) { + Out << "header: !Header\n"; Out << " Machine: "; Out << nameLookup(MachineTypePairs, Header->Machine, "# Unknown_MachineTypes") << " # ("; - return yaml::writeHexNumber(Out, Header->Machine) << ")" << endl << endl; + objyaml::writeHexNumber(Out, Header->Machine) << ")\n\n"; } -static llvm::raw_ostream &yamlCOFFSections(llvm::object::COFFObjectFile &Obj, - std::size_t NumSections, llvm::raw_ostream &Out) { - llvm::error_code ec; - Out << "sections:" << endl; - for (llvm::object::section_iterator iter = Obj.begin_sections(); - iter != Obj.end_sections(); iter.increment(ec)) { - const llvm::object::coff_section *sect = Obj.getCOFFSection(iter); - - Out << " - !Section" << endl; +static void yamlCOFFSections(object::COFFObjectFile &Obj, + std::size_t NumSections, raw_ostream &Out) { + error_code ec; + Out << "sections:\n"; + for (object::section_iterator iter = Obj.begin_sections(); + iter != Obj.end_sections(); iter.increment(ec)) { + const object::coff_section *sect = Obj.getCOFFSection(iter); + + Out << " - !Section\n"; Out << " Name: "; - yaml::writeName(Out, sect->Name, sizeof(sect->Name)) << endl; + writeName(Out, sect->Name, sizeof(sect->Name)) << '\n'; Out << " Characteristics: ["; - yaml::writeBitMask(Out, SectionCharacteristicsPairs1, sect->Characteristics); - Out << nameLookup(SectionCharacteristicsPairsAlignment, - sect->Characteristics & 0x00F00000, "# Unrecognized_IMAGE_SCN_ALIGN") + writeBitMask(Out, SectionCharacteristicsPairs1, sect->Characteristics); + Out << nameLookup(SectionCharacteristicsPairsAlignment, + sect->Characteristics & 0x00F00000, "# Unrecognized_IMAGE_SCN_ALIGN") << ", "; - yaml::writeBitMask(Out, SectionCharacteristicsPairs2, sect->Characteristics); + writeBitMask(Out, SectionCharacteristicsPairs2, sect->Characteristics); Out << "] # "; - yaml::writeHexNumber(Out, sect->Characteristics) << endl; + objyaml::writeHexNumber(Out, sect->Characteristics) << '\n'; - llvm::ArrayRef<uint8_t> sectionData; - Obj.getSectionContents(sect, sectionData); + ArrayRef<uint8_t> sectionData; + Obj.getSectionContents(sect, sectionData); Out << " SectionData: "; - yaml::writeHexStream(Out, sectionData) << endl; + objyaml::writeHexStream(Out, sectionData) << '\n'; if (iter->begin_relocations() != iter->end_relocations()) Out << " Relocations:\n"; - for (llvm::object::relocation_iterator rIter = iter->begin_relocations(); + for (object::relocation_iterator rIter = iter->begin_relocations(); rIter != iter->end_relocations(); rIter.increment(ec)) { - const llvm::object::coff_relocation *reloc = Obj.getCOFFRelocation(rIter); + const object::coff_relocation *reloc = Obj.getCOFFRelocation(rIter); - Out << " - !Relocation" << endl; + Out << " - !Relocation\n"; Out << " VirtualAddress: " ; - yaml::writeHexNumber(Out, reloc->VirtualAddress) << endl; - Out << " SymbolTableIndex: " << reloc->SymbolTableIndex << endl; - Out << " Type: " - << nameLookup(RelocationTypeX86Pairs, reloc->Type) << endl; + objyaml::writeHexNumber(Out, reloc->VirtualAddress) << '\n'; + Out << " SymbolTableIndex: " << reloc->SymbolTableIndex << '\n'; + Out << " Type: " + << nameLookup(RelocationTypeX86Pairs, reloc->Type) << '\n'; // TODO: Use the correct reloc type for the machine. - Out << endl; + Out << '\n'; } - } - return Out; + } } -static llvm::raw_ostream& yamlCOFFSymbols(llvm::object::COFFObjectFile &Obj, - std::size_t NumSymbols, llvm::raw_ostream &Out) { - llvm::error_code ec; - Out << "symbols:" << endl; - for (llvm::object::symbol_iterator iter = Obj.begin_symbols(); - iter != Obj.end_symbols(); iter.increment(ec)) { +static void yamlCOFFSymbols(object::COFFObjectFile &Obj, std::size_t NumSymbols, + raw_ostream &Out) { + error_code ec; + Out << "symbols:\n"; + for (object::symbol_iterator iter = Obj.begin_symbols(); + iter != Obj.end_symbols(); iter.increment(ec)) { // Gather all the info that we need - llvm::StringRef str; - const llvm::object::coff_symbol *symbol = Obj.getCOFFSymbol(iter); + StringRef str; + const object::coff_symbol *symbol = Obj.getCOFFSymbol(iter); Obj.getSymbolName(symbol, str); std::size_t simpleType = symbol->getBaseType(); std::size_t complexType = symbol->getComplexType(); std::size_t storageClass = symbol->StorageClass; - - Out << " - !Symbol" << endl; - Out << " Name: " << str << endl; - Out << " Value: " << symbol->Value << endl; - Out << " SectionNumber: " << symbol->SectionNumber << endl; + Out << " - !Symbol\n"; + Out << " Name: " << str << '\n'; - Out << " SimpleType: " - << nameLookup(SymbolBaseTypePairs, simpleType, - "# Unknown_SymbolBaseType") - << " # (" << simpleType << ")" << endl; - - Out << " ComplexType: " - << nameLookup(SymbolComplexTypePairs, complexType, - "# Unknown_SymbolComplexType") - << " # (" << complexType << ")" << endl; - - Out << " StorageClass: " + Out << " Value: " << symbol->Value << '\n'; + Out << " SectionNumber: " << symbol->SectionNumber << '\n'; + + Out << " SimpleType: " + << nameLookup(SymbolBaseTypePairs, simpleType, + "# Unknown_SymbolBaseType") + << " # (" << simpleType << ")\n"; + + Out << " ComplexType: " + << nameLookup(SymbolComplexTypePairs, complexType, + "# Unknown_SymbolComplexType") + << " # (" << complexType << ")\n"; + + Out << " StorageClass: " << nameLookup(SymbolStorageClassPairs, storageClass, - "# Unknown_StorageClass") - << " # (" << (int) storageClass << ")" << endl; + "# Unknown_StorageClass") + << " # (" << (int) storageClass << ")\n"; if (symbol->NumberOfAuxSymbols > 0) { - llvm::ArrayRef<uint8_t> aux = Obj.getSymbolAuxData(symbol); - Out << " NumberOfAuxSymbols: " - << (int) symbol->NumberOfAuxSymbols << endl; + ArrayRef<uint8_t> aux = Obj.getSymbolAuxData(symbol); + Out << " NumberOfAuxSymbols: " + << (int) symbol->NumberOfAuxSymbols << '\n'; Out << " AuxillaryData: "; - yaml::writeHexStream(Out, aux); + objyaml::writeHexStream(Out, aux); } - - Out << endl; - } - return Out; + Out << '\n'; + } } -llvm::error_code coff2yaml(llvm::raw_ostream &Out, llvm::MemoryBuffer *TheObj) { - llvm::error_code ec; - llvm::object::COFFObjectFile obj(TheObj, ec); - if (!ec) { - const llvm::object::coff_file_header *hd; - ec = obj.getHeader(hd); - if (!ec) { - yamlCOFFHeader(hd, Out); - yamlCOFFSections(obj, hd->NumberOfSections, Out); - yamlCOFFSymbols(obj, hd->NumberOfSymbols, Out); - } - } +error_code coff2yaml(raw_ostream &Out, MemoryBuffer *TheObj) { + error_code ec; + object::COFFObjectFile obj(TheObj, ec); + if (ec) + return ec; + + const object::coff_file_header *hd; + ec = obj.getHeader(hd); + if (ec) + return ec; + + yamlCOFFHeader(hd, Out); + yamlCOFFSections(obj, hd->NumberOfSections, Out); + yamlCOFFSymbols(obj, hd->NumberOfSymbols, Out); + return ec; } diff --git a/tools/obj2yaml/obj2yaml.cpp b/tools/obj2yaml/obj2yaml.cpp index bdc461a94715..821c9ac7d3a1 100644 --- a/tools/obj2yaml/obj2yaml.cpp +++ b/tools/obj2yaml/obj2yaml.cpp @@ -16,20 +16,19 @@ #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Signals.h" -const char endl = '\n'; +using namespace llvm; -namespace yaml { // generic yaml-writing specific routines +namespace objyaml { // generic yaml-writing specific routines unsigned char printable(unsigned char Ch) { return Ch >= ' ' && Ch <= '~' ? Ch : '.'; } - -llvm::raw_ostream &writeHexStream(llvm::raw_ostream &Out, - const llvm::ArrayRef<uint8_t> arr) { + +raw_ostream &writeHexStream(raw_ostream &Out, const ArrayRef<uint8_t> arr) { const char *hex = "0123456789ABCDEF"; Out << " !hex \""; - typedef llvm::ArrayRef<uint8_t>::const_iterator iter_t; + typedef ArrayRef<uint8_t>::const_iterator iter_t; const iter_t end = arr.end(); for (iter_t iter = arr.begin(); iter != end; ++iter) Out << hex[(*iter >> 4) & 0x0F] << hex[(*iter & 0x0F)]; @@ -37,49 +36,50 @@ llvm::raw_ostream &writeHexStream(llvm::raw_ostream &Out, Out << "\" # |"; for (iter_t iter = arr.begin(); iter != end; ++iter) Out << printable(*iter); - Out << "|" << endl; + Out << "|\n"; return Out; - } +} -llvm::raw_ostream &writeHexNumber(llvm::raw_ostream &Out, unsigned long long N) { +raw_ostream &writeHexNumber(raw_ostream &Out, unsigned long long N) { if (N >= 10) Out << "0x"; Out.write_hex(N); return Out; } -} +} // end namespace yaml - -using namespace llvm; -enum ObjectFileType { coff }; +namespace { +enum ObjectFileType { + coff +}; +} cl::opt<ObjectFileType> InputFormat( - cl::desc("Choose input format"), - cl::values( - clEnumVal(coff, "process COFF object files"), - clEnumValEnd)); - -cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); + cl::desc("Choose input format"), + cl::values(clEnumVal(coff, "process COFF object files"), clEnumValEnd)); + +cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"), + cl::init("-")); -int main(int argc, char * argv[]) { +int main(int argc, char *argv[]) { cl::ParseCommandLineOptions(argc, argv); sys::PrintStackTraceOnErrorSignal(); PrettyStackTraceProgram X(argc, argv); - llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. -// Process the input file + // Process the input file OwningPtr<MemoryBuffer> buf; -// TODO: If this is an archive, then burst it and dump each entry - if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, buf)) - llvm::errs() << "Error: '" << ec.message() << "' opening file '" - << InputFilename << "'" << endl; - else { - ec = coff2yaml(llvm::outs(), buf.take()); + // 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.take()); if (ec) - llvm::errs() << "Error: " << ec.message() << " dumping COFF file" << endl; + errs() << "Error: " << ec.message() << " dumping COFF file\n"; } return 0; diff --git a/tools/obj2yaml/obj2yaml.h b/tools/obj2yaml/obj2yaml.h index 0bc376a6db82..7d52a2d177b0 100644 --- a/tools/obj2yaml/obj2yaml.h +++ b/tools/obj2yaml/obj2yaml.h @@ -10,15 +10,15 @@ // source file, implement it. //===----------------------------------------------------------------------===// -#ifndef LLVM_UTILS_OBJ2YAML_H -#define LLVM_UTILS_OBJ2YAML_H +#ifndef LLVM_TOOLS_OBJ2YAML_H +#define LLVM_TOOLS_OBJ2YAML_H #include "llvm/ADT/ArrayRef.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/system_error.h" -namespace yaml { // routines for writing YAML +namespace objyaml { // routines for writing YAML // Write a hex stream: // <Prefix> !hex: "<hex digits>" #|<ASCII chars>\n llvm::raw_ostream &writeHexStream |
