Skip to content

Commit 1d71001

Browse files
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
2 parents 560cea6 + 33e3686 commit 1d71001

File tree

4 files changed

+103
-12
lines changed

4 files changed

+103
-12
lines changed

llvm/lib/Object/ELF.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -747,13 +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-
Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas = EF.relas(*RelaSec);
751-
if (!Relas)
752-
return createError("unable to read relocations for section " +
753-
describe(EF, Sec) + ": " +
754-
toString(Relas.takeError()));
755-
for (typename ELFFile<ELFT>::Elf_Rela Rela : *Relas)
756-
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+
}
757769
}
758770
auto GetAddressForRelocation =
759771
[&](unsigned RelocationOffsetInSection) -> Expected<unsigned> {
@@ -958,7 +970,8 @@ ELFFile<ELFT>::getSectionAndRelocations(
958970
continue;
959971
}
960972

961-
if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL)
973+
if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL &&
974+
Sec.sh_type != ELF::SHT_CREL)
962975
continue;
963976

964977
Expected<const Elf_Shdr *> RelSecOrErr = this->getSection(Sec.sh_info);

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: ]

llvm/tools/llvm-readobj/ELFDumper.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6833,6 +6833,16 @@ void ELFDumper<ELFT>::printRelocatableStackSizes(
68336833
continue;
68346834
}
68356835

6836+
// We might end up with relocations in CREL here. If we do, report a
6837+
// warning since we do not currently support them.
6838+
if (RelocSec->sh_type == ELF::SHT_CREL) {
6839+
reportWarning(createError(".stack_sizes (" + describe(*StackSizesELFSec) +
6840+
") has a corresponding CREL relocation "
6841+
"section, which is not currently supported."),
6842+
FileName);
6843+
continue;
6844+
}
6845+
68366846
// A .stack_sizes section header's sh_link field is supposed to point
68376847
// to the section that contains the functions whose stack sizes are
68386848
// described in it.

llvm/unittests/Object/ELFObjectFileTest.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/Object/ELFObjectFile.h"
1010
#include "llvm/ADT/STLExtras.h"
11+
#include "llvm/BinaryFormat/ELF.h"
1112
#include "llvm/ObjectYAML/yaml2obj.h"
1213
#include "llvm/Support/BlockFrequency.h"
1314
#include "llvm/Support/MemoryBuffer.h"
@@ -1433,7 +1434,8 @@ TEST(ELFObjectFileTest, GetSectionAndRelocations) {
14331434
// Basic verification to make sure we have the correct section types.
14341435
for (auto const &[Sec, RelaSec] : *SecToRelocMapOrErr) {
14351436
ASSERT_EQ(Sec->sh_type, ELF::SHT_PROGBITS);
1436-
ASSERT_EQ(RelaSec->sh_type, ELF::SHT_RELA);
1437+
ASSERT_TRUE(RelaSec->sh_type == ELF::SHT_RELA ||
1438+
RelaSec->sh_type == ELF::SHT_CREL);
14371439
}
14381440
};
14391441

@@ -1503,6 +1505,19 @@ TEST(ELFObjectFileTest, GetSectionAndRelocations) {
15031505
DoCheckFails(MissingRelocatableContent, DefaultMatcher,
15041506
"SHT_RELA section with index 1: failed to get a "
15051507
"relocated section: invalid section index: 255");
1508+
1509+
StringRef OneTextSectionCREL = R"(
1510+
Sections:
1511+
- Name: .text
1512+
Type: SHT_PROGBITS
1513+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
1514+
- Name: .crel.tex
1515+
Type: SHT_CREL
1516+
Flags: [ SHF_INFO_LINK ]
1517+
Info: .text
1518+
)";
1519+
1520+
DoCheckSucceeds(OneTextSectionCREL, DefaultMatcher);
15061521
}
15071522

15081523
TEST(ELFObjectFileTest, ELFSymbolRefLess) {

0 commit comments

Comments
 (0)