Skip to content

Commit 34cfed2

Browse files
author
diggerlin
committed
[AIX][XCOFF] add symbol priority for the llvm-objdump -D -symbol-description
SUMMARY: when there are two symbol has the same address. llvm-objdump -D -symbol-description will select symbol based on the following rule: 1. using Label first if there is a Label symbol. 2. If there is not Label, using a symbol which has Storage Mapping class. 3. if more than one symbol has storage mapping class, put the TC0 has the low priority, for other storage mapping class , compare based on the value. Reviewers: James Henderson ,hubert.reinterpretcast, Differential Revision: https://reviews.llvm.org/D78387
1 parent 0384446 commit 34cfed2

File tree

5 files changed

+110
-4
lines changed

5 files changed

+110
-4
lines changed

llvm/include/llvm/MC/MCDisassembler/MCDisassembler.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ struct XCOFFSymbolInfo {
2626
XCOFFSymbolInfo(Optional<XCOFF::StorageMappingClass> Smc,
2727
Optional<uint32_t> Idx, bool Label)
2828
: StorageMappingClass(Smc), Index(Idx), IsLabel(Label) {}
29+
30+
bool operator<(const XCOFFSymbolInfo &SymInfo) const;
2931
};
3032

3133
struct SymbolInfoTy {
@@ -53,9 +55,10 @@ struct SymbolInfoTy {
5355
assert(P1.IsXCOFF == P2.IsXCOFF &&
5456
"P1.IsXCOFF should be equal to P2.IsXCOFF.");
5557
if (P1.IsXCOFF)
56-
return std::tie(P1.Addr, P1.Name) < std::tie(P2.Addr, P2.Name);
57-
else
58-
return std::tie(P1.Addr, P1.Name, P1.Type) <
58+
return std::tie(P1.Addr, P1.XCOFFSymInfo, P1.Name) <
59+
std::tie(P2.Addr, P2.XCOFFSymInfo, P2.Name);
60+
61+
return std::tie(P1.Addr, P1.Name, P1.Type) <
5962
std::tie(P2.Addr, P2.Name, P2.Type);
6063
}
6164
};

llvm/lib/MC/MCDisassembler/MCDisassembler.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,56 @@ void MCDisassembler::tryAddingPcLoadReferenceComment(int64_t Value,
4343
void MCDisassembler::setSymbolizer(std::unique_ptr<MCSymbolizer> Symzer) {
4444
Symbolizer = std::move(Symzer);
4545
}
46+
47+
#define SMC_PCASE(A, P) \
48+
case XCOFF::XMC_##A: \
49+
return P;
50+
51+
uint8_t getSMCPriority(XCOFF::StorageMappingClass SMC) {
52+
switch (SMC) {
53+
SMC_PCASE(PR, 1)
54+
SMC_PCASE(RO, 1)
55+
SMC_PCASE(DB, 1)
56+
SMC_PCASE(GL, 1)
57+
SMC_PCASE(XO, 1)
58+
SMC_PCASE(SV, 1)
59+
SMC_PCASE(SV64, 1)
60+
SMC_PCASE(SV3264, 1)
61+
SMC_PCASE(TI, 1)
62+
SMC_PCASE(TB, 1)
63+
SMC_PCASE(RW, 1)
64+
SMC_PCASE(TC0, 0)
65+
SMC_PCASE(TC, 1)
66+
SMC_PCASE(TD, 1)
67+
SMC_PCASE(DS, 1)
68+
SMC_PCASE(UA, 1)
69+
SMC_PCASE(BS, 1)
70+
SMC_PCASE(UC, 1)
71+
SMC_PCASE(TL, 1)
72+
SMC_PCASE(UL, 1)
73+
SMC_PCASE(TE, 1)
74+
#undef SMC_PCASE
75+
}
76+
return 0;
77+
}
78+
79+
/// The function is for symbol sorting when symbols have the same address.
80+
/// The symbols in the same section are sorted in ascending order.
81+
/// llvm-objdump -D will choose the highest priority symbol to display when
82+
/// there are symbols with the same address.
83+
bool XCOFFSymbolInfo::operator<(const XCOFFSymbolInfo &SymInfo) const {
84+
// Label symbols have higher priority than non-label symbols.
85+
if (IsLabel != SymInfo.IsLabel)
86+
return SymInfo.IsLabel;
87+
88+
// Symbols with a StorageMappingClass have higher priority than those without.
89+
if (StorageMappingClass.hasValue() != SymInfo.StorageMappingClass.hasValue())
90+
return SymInfo.StorageMappingClass.hasValue();
91+
92+
if (StorageMappingClass.hasValue()) {
93+
return getSMCPriority(StorageMappingClass.getValue()) <
94+
getSMCPriority(SymInfo.StorageMappingClass.getValue());
95+
}
96+
97+
return false;
98+
}

llvm/test/tools/llvm-objdump/XCOFF/disassemble-symbol-description.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
COMMON: Inputs/xcoff-section-headers.o: file format aixcoff-rs6000
2626
COMMON: Disassembly of section .text:
2727
PLAIN: 00000000 <.text>:
28-
DESC: 00000000 (idx: 4) .text:
28+
DESC: 00000000 (idx: 16) .func:
2929
COMMON-NEXT: 0: 80 62 00 04 lwz 3, 4(2)
3030
RELOC: 00000002: R_TOC (idx: 26) a[TC]
3131
COMMON-NEXT: 4: 80 63 00 00 lwz 3, 0(3)

llvm/unittests/MC/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ add_llvm_unittest(MCTests
1717
MCInstPrinter.cpp
1818
StringTableBuilderTest.cpp
1919
TargetRegistry.cpp
20+
MCDisassemblerTest.cpp
2021
)
2122

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===- MCDisassemblerTest.cpp - Tests for MCDisassembler.cpp --------------===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
10+
#include "gtest/gtest.h"
11+
12+
using namespace llvm;
13+
14+
TEST(MCDisassembler, XCOFFSymbolPriorityTest) {
15+
16+
SymbolInfoTy SIT1(0x100000, "sym1", None, 1, false);
17+
SymbolInfoTy SIT2(0x110000, "sym2", None, 2, false);
18+
SymbolInfoTy SIT3(0x120000, ".func", XCOFF::XMC_PR, 3, true);
19+
SymbolInfoTy SIT4(0x120000, ".text", XCOFF::XMC_PR, 4, false);
20+
SymbolInfoTy SIT5(0x130000, "TOC", XCOFF::XMC_TC0, 5, false);
21+
SymbolInfoTy SIT6(0x130000, "func", XCOFF::XMC_TC, 6, false);
22+
23+
// Test that higher addresses would appear later than lower ones when symbols
24+
// are sorted in ascending order.
25+
EXPECT_TRUE(SIT1 < SIT2);
26+
EXPECT_FALSE(SIT2 < SIT1);
27+
28+
// Test that symbols with a StorageMappingClass have higher priority than those
29+
// without.
30+
EXPECT_TRUE(SIT2 < SIT5);
31+
EXPECT_FALSE(SIT5 < SIT2);
32+
33+
// Test that symbols with a TC0 StorageMappingClass have lower priority than those
34+
// with some other StorageMappingClass.
35+
EXPECT_TRUE(SIT5 < SIT6);
36+
EXPECT_FALSE(SIT6 < SIT5);
37+
38+
// Test label symbols have higher priorty than non-label symbols.
39+
EXPECT_TRUE(SIT4 < SIT3);
40+
EXPECT_FALSE(SIT3 < SIT4);
41+
42+
// Test symbols comparing with themselves.
43+
EXPECT_FALSE(SIT1 < SIT1);
44+
EXPECT_FALSE(SIT2 < SIT2);
45+
EXPECT_FALSE(SIT3 < SIT3);
46+
EXPECT_FALSE(SIT4 < SIT4);
47+
EXPECT_FALSE(SIT5 < SIT5);
48+
EXPECT_FALSE(SIT6 < SIT6);
49+
}

0 commit comments

Comments
 (0)