Skip to content

Commit 0252e6e

Browse files
committed
[obj2yaml,yaml2obj] Add NumBlocks to the BBAddrMapEntry yaml field.
As discussed in D95511, this allows us to encode invalid BBAddrMap sections to be used in more rigorous testing. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D96831
1 parent 0d45342 commit 0252e6e

File tree

6 files changed

+53
-36
lines changed

6 files changed

+53
-36
lines changed

llvm/include/llvm/ObjectYAML/ELFYAML.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ struct BBAddrMapEntry {
161161
llvm::yaml::Hex64 Metadata;
162162
};
163163
llvm::yaml::Hex64 Address;
164+
Optional<uint32_t> NumBlocks;
164165
Optional<std::vector<BBEntry>> BBEntries;
165166
};
166167

llvm/lib/ObjectYAML/ELFEmitter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,12 +1358,14 @@ void ELFState<ELFT>::writeSectionContent(
13581358
for (const ELFYAML::BBAddrMapEntry &E : *Section.Entries) {
13591359
// Write the address of the function.
13601360
CBA.write<uintX_t>(E.Address, ELFT::TargetEndianness);
1361-
// Write number of BBEntries (number of basic blocks in the function).
1362-
size_t NumBlocks = E.BBEntries ? E.BBEntries->size() : 0;
1361+
// Write number of BBEntries (number of basic blocks in the function). This
1362+
// is overriden by the 'NumBlocks' YAML field if specified.
1363+
uint32_t NumBlocks =
1364+
E.NumBlocks.getValueOr(E.BBEntries ? E.BBEntries->size() : 0);
13631365
SHeader.sh_size += sizeof(uintX_t) + CBA.writeULEB128(NumBlocks);
1364-
if (!NumBlocks)
1365-
continue;
13661366
// Write all BBEntries.
1367+
if (!E.BBEntries)
1368+
continue;
13671369
for (const ELFYAML::BBAddrMapEntry::BBEntry &BBE : *E.BBEntries)
13681370
SHeader.sh_size += CBA.writeULEB128(BBE.AddressOffset) +
13691371
CBA.writeULEB128(BBE.Size) +

llvm/lib/ObjectYAML/ELFYAML.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,7 @@ void MappingTraits<ELFYAML::BBAddrMapEntry>::mapping(
16651665
assert(IO.getContext() && "The IO context is not initialized");
16661666
IO.mapOptional("Address", E.Address, Hex64(0));
16671667
IO.mapOptional("BBEntries", E.BBEntries);
1668+
IO.mapOptional("NumBlocks", E.NumBlocks);
16681669
}
16691670

16701671
void MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry>::mapping(

llvm/test/tools/obj2yaml/ELF/bb-addr-map.yaml

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ FileHeader:
3939
Sections:
4040
- Name: .llvm_bb_addr_map
4141
Type: SHT_LLVM_BB_ADDR_MAP
42+
ShSize: [[SIZE=<none>]]
4243
Entries:
4344
- Address: 0x0
45+
NumBlocks: [[NUMBLOCKS=<none>]]
4446
BBEntries:
4547
- AddressOffset: 0x1
4648
Size: 0x2
@@ -57,37 +59,10 @@ Sections:
5759
Size: 0xB
5860
Metadata: 0xC
5961

60-
## Check that obj2yaml uses the "Content" tag to describe an .llvm_bb_addr_map section
61-
## when it can't extract the entries. For instance, when truncated data is given as
62-
## 'Content'.
63-
64-
# RUN: yaml2obj --docnum=2 %s -o %t2
65-
# RUN: obj2yaml %t2 | FileCheck %s --check-prefix=INVALID
66-
67-
# INVALID: --- !ELF
68-
# INVALID-NEXT: FileHeader:
69-
# INVALID-NEXT: Class: ELFCLASS64
70-
# INVALID-NEXT: Data: ELFDATA2LSB
71-
# INVALID-NEXT: Type: ET_EXEC
72-
# INVALID-NEXT: Sections:
73-
# INVALID-NEXT: - Name: .llvm_bb_addr_map
74-
# INVALID-NEXT: Type: SHT_LLVM_BB_ADDR_MAP
75-
# INVALID-NEXT: Content: '10000000000000'
76-
77-
--- !ELF
78-
FileHeader:
79-
Class: ELFCLASS64
80-
Data: ELFDATA2LSB
81-
Type: ET_EXEC
82-
Sections:
83-
- Name: .llvm_bb_addr_map
84-
Type: SHT_LLVM_BB_ADDR_MAP
85-
Content: '10000000000000'
86-
8762
## Check obj2yaml can dump empty .llvm_bb_addr_map sections.
8863

89-
# RUN: yaml2obj --docnum=3 %s -o %t3
90-
# RUN: obj2yaml %t3 | FileCheck %s --check-prefix=EMPTY
64+
# RUN: yaml2obj --docnum=2 %s -o %t2
65+
# RUN: obj2yaml %t2 | FileCheck %s --check-prefix=EMPTY
9166

9267
# EMPTY: --- !ELF
9368
# EMPTY-NEXT: FileHeader:
@@ -111,8 +86,8 @@ Sections:
11186

11287
## Check obj2yaml can dump multiple .llvm_bb_addr_map sections.
11388

114-
# RUN: yaml2obj --docnum=4 %s -o %t4
115-
# RUN: obj2yaml %t4 | FileCheck %s --check-prefix=MULTI
89+
# RUN: yaml2obj --docnum=3 %s -o %t3
90+
# RUN: obj2yaml %t3 | FileCheck %s --check-prefix=MULTI
11691

11792
# MULTI: --- !ELF
11893
# MULTI-NEXT: FileHeader:
@@ -153,3 +128,24 @@ Sections:
153128
Type: SHT_LLVM_BB_ADDR_MAP
154129
Entries:
155130
- Address: 0x20
131+
132+
## Check that obj2yaml uses the "Content" tag to describe an .llvm_bb_addr_map section
133+
## when it can't extract the entries, for example, when section is truncated, or when
134+
## an invalid NumBlocks field is specified.
135+
136+
# RUN: yaml2obj --docnum=1 -DSIZE=0x8 %s -o %t4
137+
# RUN: obj2yaml %t4 | FileCheck %s --check-prefixes=TRUNCATED,INVALID
138+
139+
# RUN: yaml2obj --docnum=1 -DNUMBLOCKS=2 %s -o %t5
140+
# RUN: obj2yaml %t5 | FileCheck %s --check-prefixes=BADNUMBLOCKS,INVALID
141+
142+
# INVALID: --- !ELF
143+
# INVALID-NEXT: FileHeader:
144+
# INVALID-NEXT: Class: ELFCLASS64
145+
# INVALID-NEXT: Data: ELFDATA2LSB
146+
# INVALID-NEXT: Type: ET_EXEC
147+
# INVALID-NEXT: Sections:
148+
# INVALID-NEXT: - Name: .llvm_bb_addr_map
149+
# INVALID-NEXT: Type: SHT_LLVM_BB_ADDR_MAP
150+
# BADNUMBLOCKS-NEXT: Content: {{([[:xdigit:]]+)}}{{$}}
151+
# TRUNCATED-NEXT: Content: '{{([[:xdigit:]]{16})}}'{{$}}

llvm/test/tools/yaml2obj/ELF/bb-addr-map.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
# CHECK-NEXT: 0000: 00000000 00000000 01010203
4848
# CHECK-NEXT: )
4949

50+
# Case 6: Override the NumBlocks field.
51+
# CHECK: Name: .llvm_bb_addr_map (1)
52+
# CHECK: SectionData (
53+
# CHECK-NEXT: 0000: 20000000 00000000 02010203
54+
# CHECK-NEXT: )
55+
5056
--- !ELF
5157
FileHeader:
5258
Class: ELFCLASS64
@@ -95,6 +101,17 @@ Sections:
95101
Size: 0x00000002
96102
Metadata: 0x00000003
97103

104+
## 6) We can override the NumBlocks field with a value different from the
105+
## actual number of BB Entries.
106+
- Name: '.llvm_bb_addr_map (6)'
107+
Type: SHT_LLVM_BB_ADDR_MAP
108+
Entries:
109+
- Address: 0x0000000000000020
110+
NumBlocks: 2
111+
BBEntries:
112+
- AddressOffset: 0x00000001
113+
Size: 0x00000002
114+
Metadata: 0x00000003
98115

99116
## Check we can't use Entries at the same time as either Content or Size.
100117
# RUN: not yaml2obj --docnum=2 -DCONTENT="00" %s 2>&1 | FileCheck %s --check-prefix=INVALID

llvm/tools/obj2yaml/elf2yaml.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ ELFDumper<ELFT>::dumpBBAddrMapSection(const Elf_Shdr *Shdr) {
861861
uint64_t Metadata = Data.getULEB128(Cur);
862862
BBEntries.push_back({Offset, Size, Metadata});
863863
}
864-
Entries.push_back({Address, BBEntries});
864+
Entries.push_back({Address, /* NumBlocks */ {}, BBEntries});
865865
}
866866

867867
if (!Cur) {

0 commit comments

Comments
 (0)