Skip to content

Commit 506d359

Browse files
committed
Support decompressing the SHT_LLVM_BB_ADDR_MAP section.
1 parent 68a346f commit 506d359

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

llvm/lib/Object/ELF.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "llvm/Object/ELF.h"
1010
#include "llvm/ADT/StringExtras.h"
1111
#include "llvm/BinaryFormat/ELF.h"
12+
#include "llvm/Object/Decompressor.h"
1213
#include "llvm/Support/Compiler.h"
1314
#include "llvm/Support/DataExtractor.h"
1415

@@ -782,6 +783,26 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
782783
if (!ContentsOrErr)
783784
return ContentsOrErr.takeError();
784785
ArrayRef<uint8_t> Content = *ContentsOrErr;
786+
787+
// Decompress the section if needed.
788+
std::unique_ptr<uint8_t[]> DecompressedContent;
789+
if (Sec.sh_flags & llvm::ELF::SHF_COMPRESSED) {
790+
Expected<StringRef> SectionNameOrErr = EF.getSectionName(Sec);
791+
if (!SectionNameOrErr)
792+
return SectionNameOrErr.takeError();
793+
auto DecompressorOrErr =
794+
Decompressor::create(*SectionNameOrErr, toStringRef(*ContentsOrErr),
795+
EF.isLE(), ELFT::Is64Bits);
796+
if (!DecompressorOrErr)
797+
return DecompressorOrErr.takeError();
798+
size_t DecompressedSize = DecompressorOrErr->getDecompressedSize();
799+
DecompressedContent = std::make_unique<uint8_t[]>(DecompressedSize);
800+
if (Error Err = DecompressorOrErr->decompress(
801+
{DecompressedContent.get(), size_t(DecompressedSize)}))
802+
return std::move(Err);
803+
Content = {DecompressedContent.get(), DecompressedSize};
804+
}
805+
785806
DataExtractor Data(Content, EF.isLE(), ELFT::Is64Bits ? 8 : 4);
786807
std::vector<BBAddrMap> FunctionEntries;
787808

llvm/test/tools/llvm-readobj/ELF/bb-addr-map.test

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
# RUN: llvm-readobj %t1.x64.o --bb-addr-map 2>&1 | FileCheck %s -DADDR=0x999999999 -DFILE=%t1.x64.o --check-prefix=CHECK
66
# RUN: llvm-readelf %t1.x64.o --bb-addr-map | FileCheck %s --check-prefix=GNU
77

8-
## Check 64-bit:
9-
# RUN: yaml2obj --docnum=1 %s -DBITS=64 -DADDR=0x999999999 -o %t1.v1.x64.o
10-
# RUN: llvm-readobj %t1.v1.x64.o --bb-addr-map 2>&1 | FileCheck %s -DADDR=0x999999999 -DFILE=%t1.v1.x64.o --check-prefix=CHECK
8+
## Check compressed sections:
9+
# RUN: llvm-objcopy %t1.x64.o --compress-sections .llvm_bb_addr_map=zstd %t1.x64.compressed.o
10+
# RUN: llvm-readobj %t1.x64.compressed.o --bb-addr-map 2>&1 | FileCheck %s -DADDR=0x999999999 -DFILE=%t1.x64.compressed.o --check-prefix=CHECK
1111

1212
## Check 32-bit:
1313
# RUN: yaml2obj --docnum=1 %s -DBITS=32 -o %t1.x32.o
1414
# RUN: llvm-readobj %t1.x32.o --bb-addr-map 2>&1 | FileCheck -DADDR=0x11111 %s -DFILE=%t1.x32.o --check-prefix=CHECK
1515
# RUN: llvm-readelf %t1.x32.o --bb-addr-map | FileCheck %s --check-prefix=GNU
1616

17+
## Check compressed sections:
18+
# RUN: llvm-objcopy %t1.x32.o --compress-sections .llvm_bb_addr_map=zstd %t1.x32.compressed.o
19+
# RUN: llvm-readobj %t1.x32.compressed.o --bb-addr-map 2>&1 | FileCheck %s -DADDR=0x11111 -DFILE=%t1.x32.compressed.o --check-prefix=CHECK
20+
1721
## Check that a malformed section can be handled.
1822
# RUN: yaml2obj --docnum=1 %s -DBITS=32 -DSIZE=7 -o %t2.o
1923
# RUN: llvm-readobj %t2.o --bb-addr-map 2>&1 | FileCheck %s -DOFFSET=0x00000007 -DFILE=%t2.o --check-prefix=TRUNCATED

0 commit comments

Comments
 (0)