1 //===-- WindowsResourceDumper.cpp - Windows Resource printer --------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the Windows resource (.res) dumper for llvm-readobj.
11 //===----------------------------------------------------------------------===//
13 #include "WindowsResourceDumper.h"
14 #include "llvm/Object/WindowsResource.h"
15 #include "llvm/Support/ConvertUTF.h"
16 #include "llvm/Support/ScopedPrinter.h"
20 namespace WindowsRes
{
22 std::string
stripUTF16(const ArrayRef
<UTF16
> &UTF16Str
) {
24 Result
.reserve(UTF16Str
.size());
26 for (UTF16 Ch
: UTF16Str
) {
27 // UTF16Str will have swapped byte order in case of big-endian machines.
28 // Swap it back in such a case.
29 uint16_t ChValue
= support::endian::byte_swap(Ch
, llvm::endianness::little
);
38 Error
Dumper::printData() {
39 auto EntryPtrOrErr
= WinRes
->getHeadEntry();
41 return EntryPtrOrErr
.takeError();
42 auto EntryPtr
= *EntryPtrOrErr
;
48 if (auto Err
= EntryPtr
.moveNext(IsEnd
))
51 return Error::success();
54 void Dumper::printEntry(const ResourceEntryRef
&Ref
) {
55 if (Ref
.checkTypeString()) {
56 auto NarrowStr
= stripUTF16(Ref
.getTypeString());
57 SW
.printString("Resource type (string)", NarrowStr
);
59 SmallString
<20> IDStr
;
60 raw_svector_ostream
OS(IDStr
);
61 printResourceTypeName(Ref
.getTypeID(), OS
);
62 SW
.printString("Resource type (int)", IDStr
);
65 if (Ref
.checkNameString()) {
66 auto NarrowStr
= stripUTF16(Ref
.getNameString());
67 SW
.printString("Resource name (string)", NarrowStr
);
69 SW
.printNumber("Resource name (int)", Ref
.getNameID());
71 SW
.printNumber("Data version", Ref
.getDataVersion());
72 SW
.printHex("Memory flags", Ref
.getMemoryFlags());
73 SW
.printNumber("Language ID", Ref
.getLanguage());
74 SW
.printNumber("Version (major)", Ref
.getMajorVersion());
75 SW
.printNumber("Version (minor)", Ref
.getMinorVersion());
76 SW
.printNumber("Characteristics", Ref
.getCharacteristics());
77 SW
.printNumber("Data size", (uint64_t)Ref
.getData().size());
78 SW
.printBinary("Data:", Ref
.getData());
79 SW
.startLine() << "\n";
82 } // namespace WindowsRes