Skip to content

Commit 15b20a1

Browse files
authored
[NFC][llvm-objdump] Add ostream param to control console prints (#139931)
1 parent 59c6d70 commit 15b20a1

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,17 +1375,17 @@ static uint64_t dumpARMELFData(uint64_t SectionAddr, uint64_t Index,
13751375
}
13761376

13771377
static void dumpELFData(uint64_t SectionAddr, uint64_t Index, uint64_t End,
1378-
ArrayRef<uint8_t> Bytes) {
1378+
ArrayRef<uint8_t> Bytes, raw_ostream &OS) {
13791379
// print out data up to 8 bytes at a time in hex and ascii
13801380
uint8_t AsciiData[9] = {'\0'};
13811381
uint8_t Byte;
13821382
int NumBytes = 0;
13831383

13841384
for (; Index < End; ++Index) {
13851385
if (NumBytes == 0)
1386-
outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1386+
OS << format("%8" PRIx64 ":", SectionAddr + Index);
13871387
Byte = Bytes.slice(Index)[0];
1388-
outs() << format(" %02x", Byte);
1388+
OS << format(" %02x", Byte);
13891389
AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.';
13901390

13911391
uint8_t IndentOffset = 0;
@@ -1400,9 +1400,9 @@ static void dumpELFData(uint64_t SectionAddr, uint64_t Index, uint64_t End,
14001400
}
14011401
if (NumBytes == 8) {
14021402
AsciiData[8] = '\0';
1403-
outs() << std::string(IndentOffset, ' ') << " ";
1404-
outs() << reinterpret_cast<char *>(AsciiData);
1405-
outs() << '\n';
1403+
OS << std::string(IndentOffset, ' ') << " ";
1404+
OS << reinterpret_cast<char *>(AsciiData);
1405+
OS << '\n';
14061406
NumBytes = 0;
14071407
}
14081408
}
@@ -1666,7 +1666,7 @@ static void
16661666
disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
16671667
DisassemblerTarget &PrimaryTarget,
16681668
std::optional<DisassemblerTarget> &SecondaryTarget,
1669-
SourcePrinter &SP, bool InlineRelocs) {
1669+
SourcePrinter &SP, bool InlineRelocs, raw_ostream &OS) {
16701670
DisassemblerTarget *DT = &PrimaryTarget;
16711671
bool PrimaryIsThumb = false;
16721672
SmallVector<std::pair<uint64_t, uint64_t>, 0> CHPECodeMap;
@@ -2089,10 +2089,10 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
20892089

20902090
if (!PrintedSection) {
20912091
PrintedSection = true;
2092-
outs() << "\nDisassembly of section ";
2092+
OS << "\nDisassembly of section ";
20932093
if (!SegmentName.empty())
2094-
outs() << SegmentName << ",";
2095-
outs() << SectionName << ":\n";
2094+
OS << SegmentName << ",";
2095+
OS << SectionName << ":\n";
20962096
}
20972097

20982098
bool PrintedLabel = false;
@@ -2104,22 +2104,22 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
21042104
const StringRef SymbolName = SymNamesHere[i];
21052105

21062106
if (!PrintedLabel) {
2107-
outs() << '\n';
2107+
OS << '\n';
21082108
PrintedLabel = true;
21092109
}
21102110
if (LeadingAddr)
2111-
outs() << format(Is64Bits ? "%016" PRIx64 " " : "%08" PRIx64 " ",
2112-
SectionAddr + Start + VMAAdjustment);
2111+
OS << format(Is64Bits ? "%016" PRIx64 " " : "%08" PRIx64 " ",
2112+
SectionAddr + Start + VMAAdjustment);
21132113
if (Obj.isXCOFF() && SymbolDescription) {
2114-
outs() << getXCOFFSymbolDescription(Symbol, SymbolName) << ":\n";
2114+
OS << getXCOFFSymbolDescription(Symbol, SymbolName) << ":\n";
21152115
} else
2116-
outs() << '<' << SymbolName << ">:\n";
2116+
OS << '<' << SymbolName << ">:\n";
21172117
}
21182118

21192119
// Don't print raw contents of a virtual section. A virtual section
21202120
// doesn't have any contents in the file.
21212121
if (Section.isVirtual()) {
2122-
outs() << "...\n";
2122+
OS << "...\n";
21232123
continue;
21242124
}
21252125

@@ -2156,17 +2156,17 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
21562156
do {
21572157
StringRef Line;
21582158
std::tie(Line, ErrMsg) = ErrMsg.split('\n');
2159-
outs() << DT->Context->getAsmInfo()->getCommentString()
2160-
<< " error decoding " << SymNamesHere[SHI] << ": " << Line
2161-
<< '\n';
2159+
OS << DT->Context->getAsmInfo()->getCommentString()
2160+
<< " error decoding " << SymNamesHere[SHI] << ": " << Line
2161+
<< '\n';
21622162
} while (!ErrMsg.empty());
21632163

21642164
if (Size) {
2165-
outs() << DT->Context->getAsmInfo()->getCommentString()
2166-
<< " decoding failed region as bytes\n";
2165+
OS << DT->Context->getAsmInfo()->getCommentString()
2166+
<< " decoding failed region as bytes\n";
21672167
for (uint64_t I = 0; I < Size; ++I)
2168-
outs() << "\t.byte\t " << format_hex(Bytes[I], 1, /*Upper=*/true)
2169-
<< '\n';
2168+
OS << "\t.byte\t " << format_hex(Bytes[I], 1, /*Upper=*/true)
2169+
<< '\n';
21702170
}
21712171
}
21722172

@@ -2179,13 +2179,13 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
21792179
Start += Size;
21802180
break;
21812181
}
2182-
2182+
formatted_raw_ostream FOS(OS);
21832183
Index = Start;
21842184
if (SectionAddr < StartAddress)
21852185
Index = std::max<uint64_t>(Index, StartAddress - SectionAddr);
21862186

21872187
if (DisassembleAsELFData) {
2188-
dumpELFData(SectionAddr, Index, End, Bytes);
2188+
dumpELFData(SectionAddr, Index, End, Bytes, FOS);
21892189
Index = End;
21902190
continue;
21912191
}
@@ -2203,8 +2203,6 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
22032203
Symbols[SI - 1].XCOFFSymInfo.StorageMappingClass &&
22042204
(*Symbols[SI - 1].XCOFFSymInfo.StorageMappingClass == XCOFF::XMC_PR);
22052205

2206-
formatted_raw_ostream FOS(outs());
2207-
22082206
std::unordered_map<uint64_t, std::string> AllLabels;
22092207
std::unordered_map<uint64_t, std::vector<BBAddrMapLabel>> BBAddrMapLabels;
22102208
if (SymbolizeOperands) {
@@ -2553,7 +2551,8 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
25532551
reportWarning("failed to disassemble missing symbol " + Sym, FileName);
25542552
}
25552553

2556-
static void disassembleObject(ObjectFile *Obj, bool InlineRelocs) {
2554+
static void disassembleObject(ObjectFile *Obj, bool InlineRelocs,
2555+
raw_ostream &OS) {
25572556
// If information useful for showing the disassembly is missing, try to find a
25582557
// more complete binary and disassemble that instead.
25592558
OwningBinary<Binary> FetchedBinary;
@@ -2679,7 +2678,7 @@ static void disassembleObject(ObjectFile *Obj, bool InlineRelocs) {
26792678
"Unrecognized disassembler option: " + Opt);
26802679

26812680
disassembleObject(*Obj, *DbgObj, PrimaryTarget, SecondaryTarget, SP,
2682-
InlineRelocs);
2681+
InlineRelocs, OS);
26832682
}
26842683

26852684
void Dumper::printRelocations() {
@@ -3340,7 +3339,7 @@ static void dumpObject(ObjectFile *O, const Archive *A = nullptr,
33403339
if (SectionContents)
33413340
printSectionContents(O);
33423341
if (Disassemble)
3343-
disassembleObject(O, Relocations);
3342+
disassembleObject(O, Relocations, outs());
33443343
if (UnwindInfo)
33453344
printUnwindInfo(O);
33463345

0 commit comments

Comments
 (0)