Skip to content

Commit b54337d

Browse files
committed
MC: Enhance mc-dump output
* Make pre-layout to -debug-only=mc-dump-pre. This output is not useful for most debugging needs. * Print fragment-associated symbols. Make it easier to locate relevant fragments. * Print the LinkerRelaxable flag.
1 parent 842f4f7 commit b54337d

File tree

6 files changed

+66
-16
lines changed

6 files changed

+66
-16
lines changed

llvm/include/llvm/MC/MCSection.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_MC_MCSECTION_H
1414
#define LLVM_MC_MCSECTION_H
1515

16+
#include "llvm/ADT/DenseMap.h"
1617
#include "llvm/ADT/SmallVector.h"
1718
#include "llvm/MC/MCFragment.h"
1819
#include "llvm/MC/SectionKind.h"
@@ -182,7 +183,8 @@ class LLVM_ABI MCSection {
182183
iterator begin() const { return iterator(CurFragList->Head); }
183184
iterator end() const { return {}; }
184185

185-
void dump() const;
186+
void dump(DenseMap<const MCFragment *, SmallVector<const MCSymbol *, 0>>
187+
*FragToSyms = nullptr) const;
186188

187189
virtual void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
188190
raw_ostream &OS,

llvm/lib/MC/MCAssembler.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,9 +812,10 @@ void MCAssembler::writeSectionData(raw_ostream &OS,
812812

813813
void MCAssembler::layout() {
814814
assert(getBackendPtr() && "Expected assembler backend");
815-
DEBUG_WITH_TYPE("mc-dump", {
816-
errs() << "assembler backend - pre-layout\n--\n";
817-
dump(); });
815+
DEBUG_WITH_TYPE("mc-dump-pre", {
816+
errs() << "assembler backend - pre-layout\n--\n";
817+
dump();
818+
});
818819

819820
// Assign section ordinals.
820821
unsigned SectionIndex = 0;
@@ -1269,11 +1270,19 @@ void MCAssembler::flushPendingErrors() const {
12691270
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
12701271
LLVM_DUMP_METHOD void MCAssembler::dump() const{
12711272
raw_ostream &OS = errs();
1273+
DenseMap<const MCFragment *, SmallVector<const MCSymbol *, 0>> FragToSyms;
1274+
// Scan symbols and build a map of fragments to their corresponding symbols.
1275+
// For variable symbols, we don't want to call their getFragment, which might
1276+
// modify `Fragment`.
1277+
for (const MCSymbol &Sym : symbols())
1278+
if (!Sym.isVariable())
1279+
if (auto *F = Sym.getFragment())
1280+
FragToSyms.try_emplace(F).first->second.push_back(&Sym);
12721281

12731282
OS << "Sections:[";
12741283
for (const MCSection &Sec : *this) {
12751284
OS << '\n';
1276-
Sec.dump();
1285+
Sec.dump(&FragToSyms);
12771286
}
12781287
OS << "\n]\n";
12791288
}

llvm/lib/MC/MCFragment.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ LLVM_DUMP_METHOD void MCFragment::dump() const {
112112
if (Fixups.empty())
113113
return;
114114
for (auto [I, F] : llvm::enumerate(Fixups)) {
115-
OS << "\n Fixup Offset:" << F.getOffset() << " Value:";
115+
OS << "\n Fixup @" << F.getOffset() << " Value:";
116116
F.getValue()->print(OS, nullptr);
117117
OS << " Kind:" << F.getKind();
118118
}
@@ -130,6 +130,8 @@ LLVM_DUMP_METHOD void MCFragment::dump() const {
130130
}
131131
case MCFragment::FT_Data: {
132132
const auto *F = cast<MCDataFragment>(this);
133+
if (F->isLinkerRelaxable())
134+
OS << " LinkerRelaxable";
133135
const SmallVectorImpl<char> &Contents = F->getContents();
134136
OS << " Size:" << Contents.size() << " [";
135137
for (unsigned i = 0, e = Contents.size(); i != e; ++i) {

llvm/lib/MC/MCSection.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,25 @@ void MCSection::setBundleLockState(BundleLockStateType NewState) {
6767
StringRef MCSection::getVirtualSectionKind() const { return "virtual"; }
6868

6969
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
70-
LLVM_DUMP_METHOD void MCSection::dump() const {
70+
LLVM_DUMP_METHOD void MCSection::dump(
71+
DenseMap<const MCFragment *, SmallVector<const MCSymbol *, 0>> *FragToSyms)
72+
const {
7173
raw_ostream &OS = errs();
7274

7375
OS << "MCSection Name:" << getName();
7476
for (auto &F : *this) {
7577
OS << '\n';
7678
F.dump();
79+
if (!FragToSyms)
80+
continue;
81+
auto It = FragToSyms->find(&F);
82+
if (It == FragToSyms->end())
83+
continue;
84+
for (auto *Sym : It->second) {
85+
OS << "\n Symbol @" << Sym->getOffset() << ' ' << Sym->getName();
86+
if (Sym->isTemporary())
87+
OS << " Temporary";
88+
}
7789
}
7890
}
7991
#endif

llvm/test/MC/ELF/mc-dump.s

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
# REQUIRES: asserts
2-
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t -debug-only=mc-dump 2>&1 | FileCheck %s --match-full-lines --strict-whitespace
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t -debug-only=mc-dump-pre,mc-dump 2>&1 | FileCheck %s --match-full-lines --strict-whitespace
33

44
#CHECK-LABEL:assembler backend - pre-layout
55
# CHECK:MCSection Name:.text
66
#CHECK-LABEL:assembler backend - final-layout
77
# CHECK:Sections:[
88
# CHECK-NEXT:MCSection Name:.text
99
# CHECK-NEXT:0 Data Size:0 []
10+
# CHECK-NEXT: Symbol @0 .text
1011
# CHECK-NEXT:0 Align Align:4 Value:0 ValueSize:1 MaxBytesToEmit:4 Nops
1112
# CHECK-NEXT:0 Data Size:0 []
13+
# CHECK-NEXT: Symbol @0 _start
1214
# CHECK-NEXT:0 Org Offset:3 Value:0
13-
# CHECK-NEXT:3 Relaxable Size:2 <MCInst #1996 <MCOperand Expr:.Ltmp1>>
14-
# CHECK-NEXT: Fixup Offset:1 Value:.Ltmp1-1 Kind:4006
15+
# CHECK-NEXT:3 Relaxable Size:2 <MCInst #1996 <MCOperand Expr:.Ltmp0>>
16+
# CHECK-NEXT: Fixup @1 Value:.Ltmp0-1 Kind:4006
1517
# CHECK-NEXT:5 Data Size:16 [48,8b,04,25,00,00,00,00,48,8b,04,25,00,00,00,00]
16-
# CHECK-NEXT: Fixup Offset:4 Value:f0@<variant 11> Kind:4021
17-
# CHECK-NEXT: Fixup Offset:12 Value:f1@<variant 11> Kind:4021
18+
# CHECK-NEXT: Fixup @4 Value:f0@<variant 11> Kind:4021
19+
# CHECK-NEXT: Fixup @12 Value:_start@<variant 11> Kind:4021
20+
# CHECK-NEXT: Symbol @16 .Ltmp0 Temporary
1821
# CHECK-NEXT:MCSection Name:.data
1922
# CHECK-NEXT:0 Data Size:0 []
23+
# CHECK-NEXT: Symbol @0 .data
2024
# CHECK-NEXT:0 Align Align:4 Value:0 ValueSize:1 MaxBytesToEmit:4
2125
# CHECK-NEXT:0 Data Size:4 [01,00,00,00]
2226
# CHECK-NEXT:4 Fill Value:0 ValueSize:1 NumValues:1
23-
# CHECK-NEXT:5 LEB Value:.Ltmp1-.Ltmp0 Signed:0
27+
# CHECK-NEXT:5 LEB Value:.Ltmp0-_start Signed:0
2428
# CHECK-NEXT:]
2529

26-
0:
30+
_start:
31+
var = _start
2732
.org 3
2833
jmp 1f
2934
movq f0@GOTPCREL, %rax
30-
movq f1@GOTPCREL, %rax
35+
movq _start@GOTPCREL, %rax
3136
1:
3237

3338
.data
3439
.p2align 2
3540
.long 1
3641
.space 1
37-
.uleb128 1b-0b
42+
.uleb128 1b-_start
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# REQUIRES: asserts
2+
# RUN: llvm-mc -filetype=obj --triple=riscv64 --mattr=+relax %s -debug-only=mc-dump -o /dev/null 2>&1 | FileCheck %s
3+
4+
# CHECK:Sections:[
5+
# CHECK-NEXT:MCSection Name:.text
6+
# CHECK-NEXT:0 Data Size:0 []
7+
# CHECK-NEXT: Symbol @0 .text
8+
# CHECK-NEXT:0 Align Align:4 Value:0 ValueSize:1 MaxBytesToEmit:4 Nops
9+
# CHECK-NEXT:0 Data LinkerRelaxable Size:8 [97,00,00,00,e7,80,00,00]
10+
# CHECK-NEXT: Fixup @0 Value:specifier(19,ext) Kind:4026
11+
# CHECK-NEXT: Symbol @0 $x
12+
# CHECK-NEXT:8 Align Align:8 Value:0 ValueSize:1 MaxBytesToEmit:8 Nops
13+
# CHECK-NEXT:12 Data Size:4 [13,05,30,00]
14+
# CHECK-NEXT:16 Align Align:8 Value:0 ValueSize:1 MaxBytesToEmit:8 Nops
15+
# CHECK-NEXT:]
16+
17+
call ext
18+
.p2align 3
19+
li x10, 3
20+
.p2align 3

0 commit comments

Comments
 (0)