Skip to content

Commit

Permalink
Add llvm-objdump tests for bbsections with bb-addr-map.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlavaee committed Feb 1, 2024
1 parent af720f8 commit a5bac7e
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 64 deletions.
29 changes: 15 additions & 14 deletions llvm/include/llvm/Object/ELFTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -879,10 +879,11 @@ struct BBAddrMap {
}
};

uint32_t ID; // Unique ID of this basic block.
uint32_t Offset; // Offset of basic block relative to the base address.
uint32_t Size; // Size of the basic block.
Metadata MD; // Metdata for this basic block.
uint32_t ID = 0; // Unique ID of this basic block.
uint32_t Offset = 0; // Offset of basic block relative to the base address.
uint32_t Size = 0; // Size of the basic block.
Metadata MD = {false, false, false, false,
false}; // Metdata for this basic block.

BBEntry(uint32_t ID, uint32_t Offset, uint32_t Size, Metadata MD)
: ID(ID), Offset(Offset), Size(Size), MD(MD){};
Expand All @@ -902,7 +903,7 @@ struct BBAddrMap {
// Struct representing the BBAddrMap information for a contiguous range of
// basic blocks (a function or a basic block section).
struct BBRangeEntry {
uint64_t BaseAddress; // Base address of the range.
uint64_t BaseAddress = 0; // Base address of the range.
std::vector<BBEntry> BBEntries; // Basic block entries for this range.

// Equality operator for unit testing.
Expand All @@ -913,16 +914,14 @@ struct BBAddrMap {
}
};

// All ranges for this function. The first range always corresponds to the
// function entry.
// All ranges for this function. Cannot be empty. The first range always
// corresponds to the function entry.
std::vector<BBRangeEntry> BBRanges;

// Returns the function address associated with this BBAddrMap, which is
// stored as the `BaseAddress` of its first BBRangeEntry. Returns 0 if
// BBRanges is empty.
// stored as the `BaseAddress` of its first BBRangeEntry.
uint64_t getFunctionAddress() const {
if (BBRanges.empty())
return 0;
assert(!BBRanges.empty());
return BBRanges.front().BaseAddress;
}

Expand All @@ -944,11 +943,13 @@ struct BBAddrMap {
return {};
}

// Returns bb entries in the bb range indexed `BBRangeIndex`.
const std::vector<BBEntry> &getBBEntries(unsigned BBRangeIndex = 0) const {
return BBRanges.at(BBRangeIndex).BBEntries;
// Returns bb entries in the first range.
const std::vector<BBEntry> &getBBEntries() const {
return BBRanges.front().BBEntries;
}

const std::vector<BBRangeEntry> &getBBRanges() const { return BBRanges; }

// Equality operator for unit testing.
bool operator==(const BBAddrMap &Other) const {
return std::equal(BBRanges.begin(), BBRanges.end(), Other.BBRanges.begin());
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/Object/ELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,12 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
std::vector<BBAddrMap::BBEntry> BBEntries;
if (FeatEnable.MultiBBRange) {
NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
if (!Cur || ULEBSizeErr)
break;
if (!NumBBRanges)
return createError("invalid zero number of BB ranges at offset " +
Twine::utohexstr(Cur.tell()) + " in " +
describe(EF, Sec));
} else {
auto AddressOrErr = ExtractAddress();
if (!AddressOrErr)
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/ObjectYAML/ELFEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1427,8 +1427,8 @@ void ELFState<ELFT>::writeSectionContent(
MultiBBRangeFeatureEnabled = FeatureOrErr->MultiBBRange;
bool MultiBBRange =
MultiBBRangeFeatureEnabled ||
(E.NumBBRanges.has_value() && E.NumBBRanges.value() > 1) ||
(E.BBRanges && E.BBRanges->size() > 1);
(E.NumBBRanges.has_value() && E.NumBBRanges.value() != 1) ||
(E.BBRanges && E.BBRanges->size() != 1);
if (MultiBBRange && !MultiBBRangeFeatureEnabled)
WithColor::warning() << "feature value(" << E.Feature
<< ") does not support multiple BB ranges.";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
## Test that in the presence of SHT_LLVM_BB_ADDR_MAP sections,
## --symbolize-operands can display <BB*> labels.

## Executable object file.
# RUN: yaml2obj --docnum=1 -DFOO_ADDR=0x4000 -DBAR_ADDR=0x5000 %s -o %t1
# RUN: llvm-objdump %t1 -d --symbolize-operands -M intel --no-show-raw-insn --no-leading-addr | \
# RUN: FileCheck %s -DSYM=symbol --match-full-lines --check-prefixes=INTEL
# RUN: llvm-objdump %t1 -d --symbolize-operands -M att --no-show-raw-insn --no-leading-addr | \
# RUN: FileCheck %s -DSYM=symbol --match-full-lines --check-prefixes=ATT
## Executable object file with separate SHT_LLVM_BB_ADDR_MAP sections for multiple text sections.
## RUN: yaml2obj --docnum=1 %s -o %t1
## RUN: llvm-objdump %t1 -d --symbolize-operands -M intel --no-show-raw-insn --no-leading-addr | \
## RUN: FileCheck %s --match-full-lines --check-prefixes=INTEL
## RUN: llvm-objdump %t1 -d --symbolize-operands -M att --no-show-raw-insn --no-leading-addr | \
## RUN: FileCheck %s --match-full-lines --check-prefixes=ATT

## Executable object file with a single SHT_LLVM_BB_ADDR_MAP for multiple text sections.
# RUN: yaml2obj --docnum=2 %s -o %t3
# RUN: llvm-objdump %t3 -d --symbolize-operands -M intel --no-show-raw-insn --no-leading-addr | \
# RUN: FileCheck %s -DSYM=symbol --match-full-lines --check-prefixes=INTEL
# RUN: llvm-objdump %t3 -d --symbolize-operands -M att --no-show-raw-insn --no-leading-addr | \
# RUN: FileCheck %s -DSYM=symbol --match-full-lines --check-prefixes=ATT
# RUN: yaml2obj --docnum=2 %s -o %t2
# RUN: llvm-objdump %t2 -d --symbolize-operands -M intel --no-show-raw-insn --no-leading-addr | \
# RUN: FileCheck %s --match-full-lines --check-prefixes=INTEL
# RUN: llvm-objdump %t2 -d --symbolize-operands -M att --no-show-raw-insn --no-leading-addr | \
# RUN: FileCheck %s --match-full-lines --check-prefixes=ATT

## Expect to find the branch and basic block labels and global variable name.
# ATT: <foo>:
# ATT-NEXT: <BB3>:
# ATT-NEXT: pushq %rax
# ATT-NEXT: <BB1>:
# ATT-NEXT: cmpl , %eax <[[SYM]]>
# ATT-NEXT: cmpl , %eax <symbol>
# ATT-NEXT: nop
# ATT-NEXT: <BB2>:
# ATT-NEXT: jge <BB5>
# ATT-NEXT: jmp <BB1>
# ATT-NEXT: <BB4>:
# ATT-NEXT: jge <foo.cold>
# ATT-NEXT: <BB5>:
# ATT-NEXT: retq
# ATT: <bar>:
Expand All @@ -37,16 +39,21 @@
# ATT-NEXT: <BB2>:
# ATT-NEXT: callq <bar>
# ATT-NEXT: retq
# ATT: <foo.cold>:
# ATT-NEXT: <BB6>:
# ATT-NEXT: retq

# INTEL: <foo>:
# INTEL-NEXT: <BB3>:
# INTEL-NEXT: push rax
# INTEL-NEXT: <BB1>:
# INTEL-NEXT: cmp eax, dword ptr <[[SYM]]>
# INTEL-NEXT: cmp eax, dword ptr <symbol>
# INTEL-NEXT: nop
# INTEL-NEXT: <BB2>:
# INTEL-NEXT: jge <BB5>
# INTEL-NEXT: jmp <BB1>
# INTEL-NEXT: <BB4>:
# INTEL-NEXT: jge <foo.cold>
# INTEL-NEXT: <BB5>:
# INTEL-NEXT: ret
# INTEL: <bar>:
Expand All @@ -59,6 +66,9 @@
# INTEL-NEXT: <BB2>:
# INTEL-NEXT: call <bar>
# INTEL-NEXT: ret
# INTEL: <foo.cold>:
# INTEL-NEXT: <BB6>:
# INTEL-NEXT: ret

## This object file contains a separate text section and SHT_LLVM_BB_ADDR_MAP
## section for each of the two functions foo and bar.
Expand All @@ -73,49 +83,65 @@ FileHeader:
Sections:
- Name: .text.foo
Type: SHT_PROGBITS
Address: [[FOO_ADDR]]
Address: 0x4000
Flags: [SHF_ALLOC, SHF_EXECINSTR]
Content: '503b0505200000907d02ebf5c3'
Content: '503b0505300000907d08ebf50f8dee1f0000c3'
- Name: .text.bar
Type: SHT_PROGBITS
Address: [[BAR_ADDR]]
Address: 0x5000
Flags: [SHF_ALLOC, SHF_EXECINSTR]
Content: '5089d0740231f6e8f4ffffffc3'
- Name: .text.split
Type: SHT_PROGBITS
Address: 0x6000
Flags: [SHF_ALLOC, SHF_EXECINSTR]
Content: 'c3'
- Name: .data
Type: SHT_PROGBITS
Flags: [SHF_ALLOC, SHF_WRITE]
Address: 0x6000
Address: 0x7000
- Name: .llvm_bb_addr_map.foo
Type: SHT_LLVM_BB_ADDR_MAP
Link: .text.foo
Entries:
- Version: 2
Feature: 0x8
BBRanges:
- BaseAddress: [[FOO_ADDR]]
BBEntries:
- ID: 3
AddressOffset: 0x0
Size: 0x1
Metadata: 0x1
- ID: 1
AddressOffset: 0x0
Size: 0x6
Metadata: 0x0
- ID: 2
AddressOffset: 0x1
Size: 0x4
Metadata: 0x0
- ID: 5
AddressOffset: 0x0
Size: 0x1
Metadata: 0x2
- BaseAddress: 0x4000
BBEntries:
- ID: 3
AddressOffset: 0x0
Size: 0x1
Metadata: 0x1
- ID: 1
AddressOffset: 0x0
Size: 0x6
Metadata: 0x0
- ID: 2
AddressOffset: 0x1
Size: 0x4
Metadata: 0x0
- ID: 4
AddressOffset: 0x0
Size: 0x6
Metadata: 0x1
- ID: 5
AddressOffset: 0x0
Size: 0x1
Metadata: 0x2
- BaseAddress: 0x6000
BBEntries:
- ID: 6
AddressOffset: 0x0
Size: 0x1
Metadata: 0x2
- Name: .llvm_bb_addr_map.bar
Type: SHT_LLVM_BB_ADDR_MAP
Link: .text.bar
Entries:
- Version: 1
BBRanges:
- BaseAddress: [[BAR_ADDR]]
- BaseAddress: 0x5000
BBEntries:
- AddressOffset: 0x0
Size: 0x1
Expand All @@ -130,13 +156,16 @@ Sections:
Symbols:
- Name: foo
Section: .text.foo
Value: [[FOO_ADDR]]
Value: 0x4000
- Name: foo.cold
Section: .text.split
Value: 0x6000
- Name: bar
Section: .text.bar
Value: [[BAR_ADDR]]
Value: 0x5000
- Name: symbol
Section: .data
Value: 0x600c
Value: 0x700c

## This object file contains a single SHT_LLVM_BB_ADDR_MAP for two text
## sections .text.foo and .text.bar.
Expand All @@ -151,21 +180,27 @@ Sections:
Type: SHT_PROGBITS
Address: 0x4000
Flags: [SHF_ALLOC, SHF_EXECINSTR]
Content: '503b0505200000907d02ebf5c3'
Content: '503b0505300000907d08ebf50f8dee1f0000c3'
- Name: .text.bar
Type: SHT_PROGBITS
Address: 0x5000
Flags: [SHF_ALLOC, SHF_EXECINSTR]
Content: '5089d0740231f6e8f4ffffffc3'
- Name: .text.split
Type: SHT_PROGBITS
Address: 0x6000
Flags: [SHF_ALLOC, SHF_EXECINSTR]
Content: 'c3'
- Name: .data
Type: SHT_PROGBITS
Flags: [SHF_ALLOC, SHF_WRITE]
Address: 0x6000
Address: 0x7000
- Name: .llvm_bb_addr_map.foo
Type: SHT_LLVM_BB_ADDR_MAP
Link: .text.foo
Entries:
- Version: 2
Feature: 0x8
BBRanges:
- BaseAddress: 0x4000
BBEntries:
Expand All @@ -181,10 +216,20 @@ Sections:
AddressOffset: 0x1
Size: 0x4
Metadata: 0x0
- ID: 4
AddressOffset: 0x0
Size: 0x6
Metadata: 0x1
- ID: 5
AddressOffset: 0x0
Size: 0x1
Metadata: 0x2
- BaseAddress: 0x6000
BBEntries:
- ID: 6
AddressOffset: 0x0
Size: 0x1
Metadata: 0x2
- Version: 1
BBRanges:
- BaseAddress: 0x5000
Expand All @@ -203,9 +248,12 @@ Symbols:
- Name: foo
Section: .text.foo
Value: 0x4000
- Name: foo.cold
Section: .text.split
Value: 0x6000
- Name: bar
Section: .text.bar
Value: 0x5000
- Name: symbol
Section: .data
Value: 0x600c
Value: 0x700c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,27 @@ Sections:
- ID: 0
AddressOffset: 0x0
Size: 0x1
Metadata: 0x1
Metadata: 0xa
- Version: 2
BBRanges:
- BBEntries:
- ID: 0
AddressOffset: 0x0
Size: 0x1
Metadata: 0x1
Metadata: 0xb
- Version: 2
Feature: 0x8
BBRanges:
- BBEntries:
- ID: 0
AddressOffset: 0x0
Size: 0x1
Metadata: 0xc
- BBEntries:
- ID: 1
AddressOffset: 0x0
Size: 0x1
Metadata: 0xd
- Name: .rela.llvm_bb_addr_map
Type: SHT_RELA
Flags: [ SHF_INFO_LINK ]
Expand All @@ -50,13 +63,28 @@ Sections:
Symbol: .text
Type: R_X86_64_64
Addend: 1
- Offset: 0x21
Symbol: .text
Type: R_X86_64_64
Addend: 0x2
- Offset: 0x2e
Symbol: .text
Type: R_X86_64_64
Addend: 0x3

Symbols:
- Name: a
Section: .text
Value: 0x0
- Name: c
Section: .text
Value: 0x1
- Name: h
Section: .text
Value: 0x2
- Name: h1
Section: .text
Value: 0x3
- Name: .text
Type: STT_SECTION
Section: .text
Expand Down
Loading

0 comments on commit a5bac7e

Please sign in to comment.