Skip to content

Commit 808b1c1

Browse files
[ELF] Add support for CREL to getSectionAndRelocations
This patch updates the getSectionAndRelocations function to also support CREL relocation sections. Unit tests have been added. This patch also updates consumers to say they explicitly do not support CREL format relocations. Subsequent patches will make the consumers work with CREL format relocations and also add in testing support. Reviewers: red1bluelost, MaskRay, rlavaee Reviewed By: MaskRay Pull Request: #126445
1 parent 196a1ac commit 808b1c1

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

llvm/lib/Object/ELF.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,13 @@ 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.");
750757
Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas = EF.relas(*RelaSec);
751758
if (!Relas)
752759
return createError("unable to read relocations for section " +
@@ -958,7 +965,8 @@ ELFFile<ELFT>::getSectionAndRelocations(
958965
continue;
959966
}
960967

961-
if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL)
968+
if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL &&
969+
Sec.sh_type != ELF::SHT_CREL)
962970
continue;
963971

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

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)