1414#include " ObjDumper.h"
1515#include " llvm-readobj.h"
1616#include " llvm/Object/Archive.h"
17+ #include " llvm/Object/Decompressor.h"
1718#include " llvm/Object/ObjectFile.h"
1819#include " llvm/Support/Error.h"
1920#include " llvm/Support/FormatVariadic.h"
@@ -142,8 +143,23 @@ getSectionRefsByNameOrIndex(const object::ObjectFile &Obj,
142143 return Ret;
143144}
144145
146+ static void maybeDecompress (const object::ObjectFile &Obj,
147+ StringRef SectionName, StringRef &SectionContent,
148+ SmallString<0 > &Out) {
149+ Expected<object::Decompressor> Decompressor = object::Decompressor::create (
150+ SectionName, SectionContent, Obj.isLittleEndian (), Obj.is64Bit ());
151+ if (!Decompressor)
152+ reportWarning (Decompressor.takeError (), Obj.getFileName ());
153+ else if (auto Err = Decompressor->resizeAndDecompress (Out))
154+ reportWarning (std::move (Err), Obj.getFileName ());
155+ else
156+ SectionContent = Out;
157+ }
158+
145159void ObjDumper::printSectionsAsString (const object::ObjectFile &Obj,
146- ArrayRef<std::string> Sections) {
160+ ArrayRef<std::string> Sections,
161+ bool Decompress) {
162+ SmallString<0 > Out;
147163 bool First = true ;
148164 for (object::SectionRef Section :
149165 getSectionRefsByNameOrIndex (Obj, Sections)) {
@@ -156,12 +172,16 @@ void ObjDumper::printSectionsAsString(const object::ObjectFile &Obj,
156172
157173 StringRef SectionContent =
158174 unwrapOrError (Obj.getFileName (), Section.getContents ());
175+ if (Decompress && Section.isCompressed ())
176+ maybeDecompress (Obj, SectionName, SectionContent, Out);
159177 printAsStringList (SectionContent);
160178 }
161179}
162180
163181void ObjDumper::printSectionsAsHex (const object::ObjectFile &Obj,
164- ArrayRef<std::string> Sections) {
182+ ArrayRef<std::string> Sections,
183+ bool Decompress) {
184+ SmallString<0 > Out;
165185 bool First = true ;
166186 for (object::SectionRef Section :
167187 getSectionRefsByNameOrIndex (Obj, Sections)) {
@@ -174,6 +194,8 @@ void ObjDumper::printSectionsAsHex(const object::ObjectFile &Obj,
174194
175195 StringRef SectionContent =
176196 unwrapOrError (Obj.getFileName (), Section.getContents ());
197+ if (Decompress && Section.isCompressed ())
198+ maybeDecompress (Obj, SectionName, SectionContent, Out);
177199 const uint8_t *SecContent = SectionContent.bytes_begin ();
178200 const uint8_t *SecEnd = SecContent + SectionContent.size ();
179201
0 commit comments