Skip to content

Commit 5825650

Browse files
boomanaiden154joaosaffran
authored andcommitted
[ELF] Add support for CREL locations for SHT_LLVM_BB_ADDR_MAP
This patch adds support for properly decoding SHT_LLVM_BB_ADDR_MAP sections in relocatable object files when the relocation format is CREL. Reviewers: rlavaee, jh7370, red1bluelost, MaskRay Reviewed By: MaskRay Pull Request: llvm#126446
1 parent f46bbe3 commit 5825650

File tree

2 files changed

+75
-17
lines changed

2 files changed

+75
-17
lines changed

llvm/lib/Object/ELF.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -747,20 +747,25 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
747747
assert(RelaSec &&
748748
"Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
749749
"object file without providing a relocation section.");
750-
// We might end up with relocations in CREL here. If we do, return an
751-
// error since we do not currently support them.
752-
if (RelaSec->sh_type == ELF::SHT_CREL)
753-
return createError("unable to read relocations for section " +
754-
describe(EF, Sec) +
755-
" as the corresponding relocation section format is "
756-
"CREL, which is not currently supported.");
757-
Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas = EF.relas(*RelaSec);
758-
if (!Relas)
759-
return createError("unable to read relocations for section " +
760-
describe(EF, Sec) + ": " +
761-
toString(Relas.takeError()));
762-
for (typename ELFFile<ELFT>::Elf_Rela Rela : *Relas)
763-
FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
750+
if (RelaSec->sh_type == ELF::SHT_CREL) {
751+
Expected<typename ELFFile<ELFT>::RelsOrRelas> Relas = EF.crels(*RelaSec);
752+
if (!Relas)
753+
return createError("unable to read CREL relocations for section " +
754+
describe(EF, Sec) + ": " +
755+
toString(Relas.takeError()));
756+
for (typename ELFFile<ELFT>::Elf_Rela Rela : std::get<1>(*Relas)) {
757+
FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
758+
}
759+
} else {
760+
Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas =
761+
EF.relas(*RelaSec);
762+
if (!Relas)
763+
return createError("unable to read relocations for section " +
764+
describe(EF, Sec) + ": " +
765+
toString(Relas.takeError()));
766+
for (typename ELFFile<ELFT>::Elf_Rela Rela : *Relas)
767+
FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
768+
}
764769
}
765770
auto GetAddressForRelocation =
766771
[&](unsigned RelocationOffsetInSection) -> Expected<unsigned> {

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## This test checks how we handle the --bb-addr-map option on relocatable
22
## object files.
33

4-
# RUN: yaml2obj %s -o %t1.o
4+
# RUN: yaml2obj -D RELOCATION_SECTION_NAME=.rela.llvm_bb_addr_map \
5+
# RUN: -D RELOCATION_SECTION_TYPE=SHT_RELA %s -o %t1.o
56
# RUN: llvm-readobj %t1.o --bb-addr-map | FileCheck %s
67

78
# CHECK: BBAddrMap [
@@ -77,8 +78,8 @@ Sections:
7778
AddressOffset: 0x0
7879
Size: 0x11
7980
Metadata: 0x8
80-
- Name: .rela.llvm_bb_addr_map
81-
Type: SHT_RELA
81+
- Name: [[RELOCATION_SECTION_NAME]]
82+
Type: [[RELOCATION_SECTION_TYPE]]
8283
Flags: [ SHF_INFO_LINK ]
8384
Link: .symtab
8485
Info: .llvm_bb_addr_map
@@ -228,3 +229,55 @@ Sections:
228229
# ET-DYN-NO-WARNING: ]
229230
# ET-DYN-NO-WARNING: }
230231
# ET-DYN-NO-WARNING: ]
232+
233+
## Check that we can correctly decode a BBAddrMap in a reloctable object file
234+
## with CREL relocations.
235+
236+
# RUN: yaml2obj -D RELOCATION_SECTION_NAME=.crel.llvm_bb_addr_map \
237+
# RUN: -D RELOCATION_SECTION_TYPE=SHT_CREL %s -o %t6.o
238+
# RUN: llvm-readobj %t6.o --bb-addr-map | FileCheck %s --check-prefix=CREL
239+
240+
# CREL: BBAddrMap [
241+
# CREL-NEXT: Function {
242+
# CREL-NEXT: At: 0x0
243+
# CREL-NEXT: Name: <?>
244+
# CREL-NEXT: BB Ranges [
245+
# CREL-NEXT: {
246+
# CREL-NEXT: Base Address: 0x0
247+
# CREL-NEXT: BB Entries [
248+
# CREL-NEXT: {
249+
# CREL-NEXT: ID: 0
250+
# CREL-NEXT: Offset: 0x0
251+
# CREL-NEXT: Size: 0xF
252+
# CREL-NEXT: HasReturn: Yes
253+
# CREL-NEXT: HasTailCall: No
254+
# CREL-NEXT: IsEHPad: No
255+
# CREL-NEXT: CanFallThrough: No
256+
# CREL-NEXT: HasIndirectBranch: No
257+
# CREL-NEXT: }
258+
# CREL-NEXT: ]
259+
# CREL-NEXT: }
260+
# CREL-NEXT: ]
261+
# CREL-NEXT: }
262+
# CREL-NEXT: Function {
263+
# CREL-NEXT: At: 0x10
264+
# CREL-NEXT: Name: <?>
265+
# CREL-NEXT: BB Ranges [
266+
# CREL-NEXT: {
267+
# CREL-NEXT: Base Address: 0x10
268+
# CREL-NEXT: BB Entries [
269+
# CREL-NEXT: {
270+
# CREL-NEXT: ID: 0
271+
# CREL-NEXT: Offset: 0x0
272+
# CREL-NEXT: Size: 0x11
273+
# CREL-NEXT: HasReturn: No
274+
# CREL-NEXT: HasTailCall: No
275+
# CREL-NEXT: IsEHPad: No
276+
# CREL-NEXT: CanFallThrough: Yes
277+
# CREL-NEXT: HasIndirectBranch: No
278+
# CREL-NEXT: }
279+
# CREL-NEXT: ]
280+
# CREL-NEXT: }
281+
# CREL-NEXT: ]
282+
# CREL-NEXT: }
283+
# CREL-NEXT: ]

0 commit comments

Comments
 (0)