Skip to content

[ELF] Add support for CREL to getSectionAndRelocations #126445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion llvm/lib/Object/ELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,13 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
assert(RelaSec &&
"Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
"object file without providing a relocation section.");
// We might end up with relocations in CREL here. If we do, return an
// error since we do not currently support them.
if (RelaSec->sh_type == ELF::SHT_CREL)
return createError("unable to read relocations for section " +
describe(EF, Sec) +
" as the corresponding relocation section format is "
"CREL, which is not currently supported.");
Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas = EF.relas(*RelaSec);
if (!Relas)
return createError("unable to read relocations for section " +
Expand Down Expand Up @@ -958,7 +965,8 @@ ELFFile<ELFT>::getSectionAndRelocations(
continue;
}

if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL)
if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL &&
Sec.sh_type != ELF::SHT_CREL)
continue;

Expected<const Elf_Shdr *> RelSecOrErr = this->getSection(Sec.sh_info);
Expand Down
10 changes: 10 additions & 0 deletions llvm/tools/llvm-readobj/ELFDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6833,6 +6833,16 @@ void ELFDumper<ELFT>::printRelocatableStackSizes(
continue;
}

// We might end up with relocations in CREL here. If we do, report a
// warning since we do not currently support them.
if (RelocSec->sh_type == ELF::SHT_CREL) {
reportWarning(createError(".stack_sizes (" + describe(*StackSizesELFSec) +
") has a corresponding CREL relocation "
"section, which is not currently supported"),
FileName);
continue;
}

// A .stack_sizes section header's sh_link field is supposed to point
// to the section that contains the functions whose stack sizes are
// described in it.
Expand Down
17 changes: 16 additions & 1 deletion llvm/unittests/Object/ELFObjectFileTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "llvm/Object/ELFObjectFile.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/ObjectYAML/yaml2obj.h"
#include "llvm/Support/BlockFrequency.h"
#include "llvm/Support/MemoryBuffer.h"
Expand Down Expand Up @@ -1433,7 +1434,8 @@ TEST(ELFObjectFileTest, GetSectionAndRelocations) {
// Basic verification to make sure we have the correct section types.
for (auto const &[Sec, RelaSec] : *SecToRelocMapOrErr) {
ASSERT_EQ(Sec->sh_type, ELF::SHT_PROGBITS);
ASSERT_EQ(RelaSec->sh_type, ELF::SHT_RELA);
ASSERT_TRUE(RelaSec->sh_type == ELF::SHT_RELA ||
RelaSec->sh_type == ELF::SHT_CREL);
}
};

Expand Down Expand Up @@ -1503,6 +1505,19 @@ TEST(ELFObjectFileTest, GetSectionAndRelocations) {
DoCheckFails(MissingRelocatableContent, DefaultMatcher,
"SHT_RELA section with index 1: failed to get a "
"relocated section: invalid section index: 255");

StringRef OneTextSectionCREL = R"(
Sections:
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
- Name: .crel.tex
Type: SHT_CREL
Flags: [ SHF_INFO_LINK ]
Info: .text
)";

DoCheckSucceeds(OneTextSectionCREL, DefaultMatcher);
}

TEST(ELFObjectFileTest, ELFSymbolRefLess) {
Expand Down