Skip to content

Commit 7a28b0b

Browse files
committed
[llvm-objdump] Support CHPE code ranges in disassembler.
Reviewed By: jhenderson, MaskRay Differential Revision: https://reviews.llvm.org/D149095
1 parent c95361e commit 7a28b0b

File tree

2 files changed

+170
-10
lines changed

2 files changed

+170
-10
lines changed

llvm/test/tools/llvm-objdump/COFF/arm64ec.yaml

Lines changed: 109 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,72 @@
11
# REQUIRES: aarch64-registered-target
22

33
## Check that AMD64 image file with CHPE data is recognized as ARM64EC.
4-
# RUN: yaml2obj %s -o %t -DMACHINE=IMAGE_FILE_MACHINE_AMD64
5-
# RUN: llvm-objdump -d %t | FileCheck --check-prefixes=DISASM,ARM64EC %s
6-
# RUN: llvm-readobj --coff-load-config %t | FileCheck --check-prefix=CODEMAP %s
4+
# RUN: yaml2obj --docnum=1 %s -o %t1 -DMACHINE=IMAGE_FILE_MACHINE_AMD64
5+
# RUN: llvm-readobj --coff-load-config %t1 | FileCheck --check-prefix=CODEMAP %s
6+
# RUN: %if x86-registered-target %{ \
7+
# RUN: llvm-objdump -d %t1 | FileCheck --check-prefixes=DISASM,ARM64EC %s \
8+
# RUN: %} %else %{ \
9+
# RUN: llvm-objdump -d %t1 2>&1 | FileCheck --check-prefixes=DISASM-NO-X64,ARM64EC,WARN1 %s \
10+
# RUN: %}
711

812
## Check that ARM64 image file with CHPE data is recognized as ARM64X.
9-
# RUN: yaml2obj %s -o %t -DMACHINE=IMAGE_FILE_MACHINE_ARM64
10-
# RUN: llvm-objdump -d %t | FileCheck --check-prefixes=DISASM,ARM64X %s
11-
# RUN: llvm-readobj --coff-load-config %t | FileCheck --check-prefix=CODEMAP %s
13+
# RUN: yaml2obj --docnum=1 %s -o %t2 -DMACHINE=IMAGE_FILE_MACHINE_ARM64
14+
# RUN: llvm-readobj --coff-load-config %t2 | FileCheck --check-prefix=CODEMAP %s
15+
# RUN: %if x86-registered-target %{ \
16+
# RUN: llvm-objdump -d %t2 | FileCheck --check-prefixes=DISASM,ARM64X %s \
17+
# RUN: %} %else %{ \
18+
# RUN: llvm-objdump -d %t2 2>&1 | FileCheck --check-prefixes=DISASM-NO-X64,ARM64X,WARN1 %s \
19+
# RUN: %}
20+
21+
## Check handling of an explicit, non-default triple.
22+
# RUN: %if !x86-registered-target %{ \
23+
# RUN: llvm-objdump --triple arm64ec-w64-windows-gnu -d %t1 2>&1 \
24+
# RUN: | FileCheck --check-prefixes=DISASM-NO-X64,ARM64EC,WARN2 %s \
25+
# RUN: %}
1226

1327
# ARM64EC: file format coff-arm64ec
1428
# ARM64X: file format coff-arm64x
1529

16-
# DISASM: 180001000: 52800040 mov w0, #0x2
30+
# WARN1: llvm-objdump: warning: '{{.*}}': unable to get target for 'x86_64--', see --version and --triple.
31+
# WARN2: llvm-objdump: warning: '{{.*}}': unable to get target for 'x86_64-w64-windows-gnu', see --version and --triple.
32+
33+
# DISASM: Disassembly of section .text:
34+
# DISASM-EMPTY:
35+
# DISASM-NEXT: 0000000180001000 <.text>:
36+
# DISASM-NEXT: 180001000: 52800040 mov w0, #0x2
1737
# DISASM-NEXT: 180001004: d65f03c0 ret
1838
# DISASM-NEXT: ...
19-
# DISASM: 180002020: 528000a0 mov w0, #0x5
39+
# DISASM-NEXT: 180001020: b8 03 00 00 00 movl $0x3, %eax
40+
# DISASM-NEXT: 180001025: c3 retq
41+
# DISASM-EMPTY:
42+
# DISASM-NEXT: Disassembly of section .test:
43+
# DISASM-EMPTY:
44+
# DISASM-NEXT: 0000000180002000 <.test>:
45+
# DISASM-NEXT: 180002000: b8 06 00 00 00 movl $0x6, %eax
46+
# DISASM-NEXT: 180002005: c3 retq
47+
# DISASM-NEXT: 180002006: cc int3
48+
# DISASM-NEXT: ...
49+
# DISASM-NEXT: 180002020: 528000a0 mov w0, #0x5
2050
# DISASM-NEXT: 180002024: d65f03c0 ret
2151

52+
# DISASM-NO-X64: Disassembly of section .text:
53+
# DISASM-NO-X64-EMPTY:
54+
# DISASM-NO-X64-NEXT: 0000000180001000 <.text>:
55+
# DISASM-NO-X64-NEXT: 180001000: 52800040 mov w0, #0x2
56+
# DISASM-NO-X64-NEXT: 180001004: d65f03c0 ret
57+
# DISASM-NO-X64-NEXT: ...
58+
# DISASM-NO-X64-NEXT: 180001020: 000003b8 udf #0x3b8
59+
# DISASM-NO-X64-NEXT: 180001024: 00 c3 <unknown>
60+
# DISASM-NO-X64-EMPTY:
61+
# DISASM-NO-X64-NEXT: Disassembly of section .test:
62+
# DISASM-NO-X64-EMPTY:
63+
# DISASM-NO-X64-NEXT: 0000000180002000 <.test>:
64+
# DISASM-NO-X64-NEXT: 180002000: 000006b8 udf #0x6b8
65+
# DISASM-NO-X64-NEXT: 180002004: 00ccc300 <unknown>
66+
# DISASM-NO-X64-NEXT: ...
67+
# DISASM-NO-X64-NEXT: 180002020: 528000a0 mov w0, #0x5
68+
# DISASM-NO-X64-NEXT: 180002024: d65f03c0 ret
69+
2270
# CODEMAP: CodeMap [
2371
# CODEMAP-NEXT: 0x1000 - 0x1008 ARM64EC
2472
# CODEMAP-NEXT: 0x1020 - 0x2007 X64
@@ -88,3 +136,56 @@ sections:
88136
- UInt32: 0x8
89137
symbols: []
90138
...
139+
140+
## Check error handling of invalid code map RVA.
141+
# RUN: yaml2obj --docnum=2 %s -o %t-invalid
142+
# RUN: not llvm-objdump -d %t-invalid 2>&1 | FileCheck --check-prefixes=ERR %s -DFILE=%t-invalid
143+
# ERR: error: '[[FILE]]': RVA 0x6000 for CHPE code map not found
144+
145+
--- !COFF
146+
OptionalHeader:
147+
ImageBase: 0x180000000
148+
SectionAlignment: 4096
149+
FileAlignment: 512
150+
DLLCharacteristics: [ ]
151+
LoadConfigTable:
152+
RelativeVirtualAddress: 0x3000
153+
Size: 320
154+
header:
155+
Machine: IMAGE_FILE_MACHINE_AMD64
156+
Characteristics: [ IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LARGE_ADDRESS_AWARE, IMAGE_FILE_DLL ]
157+
sections:
158+
- Name: .rdata
159+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
160+
VirtualAddress: 0x3000
161+
VirtualSize: 328
162+
StructuredData:
163+
- LoadConfig:
164+
CHPEMetadataPointer: 0x180004000
165+
- Name: .data
166+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
167+
VirtualAddress: 0x4000
168+
VirtualSize: 112
169+
StructuredData:
170+
- UInt32: 1 # Version
171+
- UInt32: 0x6000 # CodeMap
172+
- UInt32: 3 # CodeMapCount
173+
- UInt32: 0 # CodeRangesToEntryPoints
174+
- UInt32: 0 # RedirectionMetadata
175+
- UInt32: 0
176+
- UInt32: 0
177+
- UInt32: 0
178+
- UInt32: 0
179+
- UInt32: 0
180+
- UInt32: 0
181+
- UInt32: 0
182+
- UInt32: 0 # CodeRangesToEntryPointsCount
183+
- UInt32: 0 # RedirectionMetadataCount
184+
- UInt32: 0
185+
- UInt32: 0
186+
- UInt32: 0
187+
- UInt32: 0
188+
- UInt32: 0
189+
- UInt32: 0
190+
symbols: []
191+
...

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

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,8 +1412,31 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
14121412
SourcePrinter &SP, bool InlineRelocs) {
14131413
DisassemblerTarget *DT = &PrimaryTarget;
14141414
bool PrimaryIsThumb = false;
1415-
if (isArmElf(Obj))
1416-
PrimaryIsThumb = PrimaryTarget.SubtargetInfo->checkFeatures("+thumb-mode");
1415+
SmallVector<std::pair<uint64_t, uint64_t>, 0> CHPECodeMap;
1416+
1417+
if (SecondaryTarget) {
1418+
if (isArmElf(Obj)) {
1419+
PrimaryIsThumb =
1420+
PrimaryTarget.SubtargetInfo->checkFeatures("+thumb-mode");
1421+
} else if (const auto *COFFObj = dyn_cast<COFFObjectFile>(&Obj)) {
1422+
const chpe_metadata *CHPEMetadata = COFFObj->getCHPEMetadata();
1423+
if (CHPEMetadata && CHPEMetadata->CodeMapCount) {
1424+
uintptr_t CodeMapInt;
1425+
cantFail(COFFObj->getRvaPtr(CHPEMetadata->CodeMap, CodeMapInt));
1426+
auto CodeMap = reinterpret_cast<const chpe_range_entry *>(CodeMapInt);
1427+
1428+
for (uint32_t i = 0; i < CHPEMetadata->CodeMapCount; ++i) {
1429+
if (CodeMap[i].getType() == chpe_range_type::Amd64 &&
1430+
CodeMap[i].Length) {
1431+
// Store x86_64 CHPE code ranges.
1432+
uint64_t Start = CodeMap[i].getStart() + COFFObj->getImageBase();
1433+
CHPECodeMap.emplace_back(Start, Start + CodeMap[i].Length);
1434+
}
1435+
}
1436+
llvm::sort(CHPECodeMap);
1437+
}
1438+
}
1439+
}
14171440

14181441
std::map<SectionRef, std::vector<RelocationRef>> RelocMap;
14191442
if (InlineRelocs)
@@ -1902,6 +1925,24 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
19021925
DT = PrimaryIsThumb ? &PrimaryTarget : &*SecondaryTarget;
19031926
}
19041927
}
1928+
} else if (!CHPECodeMap.empty()) {
1929+
uint64_t Address = SectionAddr + Index;
1930+
auto It = partition_point(
1931+
CHPECodeMap,
1932+
[Address](const std::pair<uint64_t, uint64_t> &Entry) {
1933+
return Entry.first <= Address;
1934+
});
1935+
if (It != CHPECodeMap.begin() && Address < (It - 1)->second) {
1936+
DT = &*SecondaryTarget;
1937+
} else {
1938+
DT = &PrimaryTarget;
1939+
// X64 disassembler range may have left Index unaligned, so
1940+
// make sure that it's aligned when we switch back to ARM64
1941+
// code.
1942+
Index = llvm::alignTo(Index, 4);
1943+
if (Index >= End)
1944+
break;
1945+
}
19051946
}
19061947

19071948
if (DumpARMELFData) {
@@ -2211,6 +2252,24 @@ static void disassembleObject(ObjectFile *Obj, bool InlineRelocs) {
22112252
Features.AddFeature("+thumb-mode");
22122253
SecondaryTarget.emplace(PrimaryTarget, Features);
22132254
}
2255+
} else if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) {
2256+
const chpe_metadata *CHPEMetadata = COFFObj->getCHPEMetadata();
2257+
if (CHPEMetadata && CHPEMetadata->CodeMapCount) {
2258+
// Set up x86_64 disassembler for ARM64EC binaries.
2259+
Triple X64Triple(TripleName);
2260+
X64Triple.setArch(Triple::ArchType::x86_64);
2261+
2262+
std::string Error;
2263+
const Target *X64Target =
2264+
TargetRegistry::lookupTarget("", X64Triple, Error);
2265+
if (X64Target) {
2266+
SubtargetFeatures X64Features;
2267+
SecondaryTarget.emplace(X64Target, *Obj, X64Triple.getTriple(), "",
2268+
X64Features);
2269+
} else {
2270+
reportWarning(Error, Obj->getFileName());
2271+
}
2272+
}
22142273
}
22152274

22162275
const ObjectFile *DbgObj = Obj;

0 commit comments

Comments
 (0)