Skip to content

Commit

Permalink
Process SHT_AARCH64_AUTH_RELR as target-specific section type
Browse files Browse the repository at this point in the history
  • Loading branch information
kovdan01 committed Dec 7, 2023
1 parent b0c9a28 commit f9103d5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/Object/ELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
break;
case ELF::EM_AARCH64:
switch (Type) {
STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_AUTH_RELR);
STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC);
STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_STATIC);
}
Expand All @@ -299,7 +300,6 @@ StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_AUTH_RELR);
STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/ObjectYAML/ELFYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,6 @@ void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
ECase(SHT_GROUP);
ECase(SHT_SYMTAB_SHNDX);
ECase(SHT_RELR);
ECase(SHT_AARCH64_AUTH_RELR);
ECase(SHT_ANDROID_REL);
ECase(SHT_ANDROID_RELA);
ECase(SHT_ANDROID_RELR);
Expand Down Expand Up @@ -720,6 +719,7 @@ void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
ECase(SHT_MSP430_ATTRIBUTES);
break;
case ELF::EM_AARCH64:
ECase(SHT_AARCH64_AUTH_RELR);
ECase(SHT_AARCH64_MEMTAG_GLOBALS_STATIC);
ECase(SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@
# MIPS-GNU: abiflags MIPS_ABIFLAGS
# MIPS-GNU: dwarf MIPS_DWARF

# AARCH64-LLVM: Name: aarch64_auth_relr
# AARCH64-LLVM: Type: SHT_AARCH64_AUTH_RELR
# AARCH64-LLVM: Name: .memtag.globals.dynamic
# AARCH64-LLVM: Type: SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC
# AARCH64-LLVM: Name: .memtag.globals.static
# AARCH64-LLVM: Type: SHT_AARCH64_MEMTAG_GLOBALS_STATIC

# AARCH64-GNU: aarch64_auth_relr AARCH64_AUTH_RELR
# AARCH64-GNU: .memtag.globals.dynamic AARCH64_MEMTAG_GLOBALS_DYNAMIC
# AARCH64-GNU: .memtag.globals.static AARCH64_MEMTAG_GLOBALS_STATIC

Expand Down Expand Up @@ -113,6 +116,8 @@ FileHeader:
Type: ET_REL
Machine: EM_AARCH64
Sections:
- Name: aarch64_auth_relr
Type: SHT_AARCH64_AUTH_RELR
- Name: .memtag.globals.dynamic
Type: SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC
- Name: .memtag.globals.static
Expand Down
17 changes: 6 additions & 11 deletions llvm/test/tools/llvm-readobj/ELF/relr-relocs.test
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,18 @@ Sections:
# RUN: llvm-readelf --relocations %t2.broken.android 2>&1 | \
# RUN: FileCheck -DFILE=%t2.broken.android --check-prefix=BROKEN-GNU %s -DSECNAME=SHT_ANDROID_RELR

## Case C: check the case when relocations can't be read from an SHT_AARCH64_AUTH_RELR section.
## Case C: check the case when we have an SHT_AARCH64_AUTH_RELR section in non-AArch64 ELF.
## SHT_AARCH64_AUTH_RELR = 0x70000004.
# RUN: yaml2obj --docnum=2 -DENTSIZE=1 -DSHTYPE=0x70000004 %s -o %t2.broken.aarch64auth
# RUN: llvm-readobj --relocations %t2.broken.aarch64auth 2>&1 | \
# RUN: FileCheck -DFILE=%t2.broken.aarch64auth --check-prefix=BROKEN-LLVM-AARCH64-AUTH %s -DSECNAME=SHT_AARCH64_AUTH_RELR
# RUN: FileCheck -DFILE=%t2.broken.aarch64auth --check-prefix=WRONGARCH-LLVM-AARCH64-AUTH %s -DSECNAME=SHT_AARCH64_AUTH_RELR
# RUN: llvm-readelf --relocations %t2.broken.aarch64auth 2>&1 | \
# RUN: FileCheck -DFILE=%t2.broken.aarch64auth --check-prefix=BROKEN-GNU-AARCH64-AUTH %s -DSECNAME=SHT_AARCH64_AUTH_RELR
# RUN: FileCheck -DFILE=%t2.broken.aarch64auth --check-prefix=WRONGARCH-GNU-AARCH64-AUTH %s -DSECNAME=SHT_AARCH64_AUTH_RELR

# BROKEN-LLVM-AARCH64-AUTH: Relocations [
# BROKEN-LLVM-AARCH64-AUTH-NEXT: Section (1) .relr.dyn {
# BROKEN-LLVM-AARCH64-AUTH-NEXT: warning: '[[FILE]]': SHT_AARCH64_AUTH_RELR section in non-AArch64 ELF found, skipping
# BROKEN-LLVM-AARCH64-AUTH-NEXT: }
# BROKEN-LLVM-AARCH64-AUTH-NEXT: ]
# WRONGARCH-LLVM-AARCH64-AUTH: Relocations [
# WRONGARCH-LLVM-AARCH64-AUTH-NEXT: ]

# BROKEN-GNU-AARCH64-AUTH: Relocation section '.relr.dyn' at offset 0x34 contains <?> entries:
# BROKEN-GNU-AARCH64-AUTH-NEXT: Offset Info Type Sym. Value Symbol's Name
# BROKEN-GNU-AARCH64-AUTH-NEXT: warning: '[[FILE]]': SHT_AARCH64_AUTH_RELR section in non-AArch64 ELF found, skipping
# WRONGARCH-GNU-AARCH64-AUTH-NOT: Relocation section

## Check the behavior when the sh_link field of the SHT_RELR/SHT_ANDROID_RELR/SHT_AARCH64_AUTH_RELR section
## is set to an arbitrary value. Normally, it is set to 0, because such sections contains
Expand Down
5 changes: 0 additions & 5 deletions llvm/test/tools/llvm-readobj/ELF/section-types.test
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
# LLVM: Type: SHT_ANDROID_RELA
# LLVM: Name: android_relr
# LLVM: Type: SHT_ANDROID_RELR
# LLVM: Name: aarch64_auth_relr
# LLVM: Type: SHT_AARCH64_AUTH_RELR
# LLVM: Name: llvm_odrtab
# LLVM: Type: SHT_LLVM_ODRTAB
# LLVM: Name: linker_options
Expand Down Expand Up @@ -115,7 +113,6 @@
# GNU-NEXT: android_rel ANDROID_REL
# GNU-NEXT: android_rela ANDROID_RELA
# GNU-NEXT: android_relr ANDROID_RELR
# GNU-NEXT: aarch64_auth_relr AARCH64_AUTH_RELR
# GNU-NEXT: llvm_odrtab LLVM_ODRTAB
# GNU-NEXT: linker_options LLVM_LINKER_OPTIONS
# GNU-NEXT: llvm_call_graph_profile LLVM_CALL_GRAPH_PROFILE
Expand Down Expand Up @@ -194,8 +191,6 @@ Sections:
Type: SHT_ANDROID_RELA
- Name: android_relr
Type: SHT_ANDROID_RELR
- Name: aarch64_auth_relr
Type: SHT_AARCH64_AUTH_RELR
- Name: llvm_odrtab
Type: SHT_LLVM_ODRTAB
- Name: linker_options
Expand Down
33 changes: 18 additions & 15 deletions llvm/tools/llvm-readobj/ELFDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3816,10 +3816,12 @@ void GNUELFDumper<ELFT>::printRelRelaReloc(const Relocation<ELFT> &R,
}

template <class ELFT>
static void printRelocHeaderFields(formatted_raw_ostream &OS, unsigned SType) {
static void printRelocHeaderFields(formatted_raw_ostream &OS, unsigned SType,
const typename ELFT::Ehdr &EHeader) {
bool IsRela = SType == ELF::SHT_RELA || SType == ELF::SHT_ANDROID_RELA;
bool IsRelr = SType == ELF::SHT_RELR || SType == ELF::SHT_ANDROID_RELR ||
SType == ELF::SHT_AARCH64_AUTH_RELR;
bool IsRelr =
SType == ELF::SHT_RELR || SType == ELF::SHT_ANDROID_RELR ||
(EHeader.e_machine == EM_AARCH64 && SType == ELF::SHT_AARCH64_AUTH_RELR);
if (ELFT::Is64Bits)
OS << " ";
else
Expand All @@ -3844,16 +3846,18 @@ void GNUELFDumper<ELFT>::printDynamicRelocHeader(unsigned Type, StringRef Name,
uint64_t Offset = Reg.Addr - this->Obj.base();
OS << "\n'" << Name.str().c_str() << "' relocation section at offset 0x"
<< utohexstr(Offset, /*LowerCase=*/true) << " contains " << Reg.Size << " bytes:\n";
printRelocHeaderFields<ELFT>(OS, Type);
printRelocHeaderFields<ELFT>(OS, Type, this->Obj.getHeader());
}

template <class ELFT>
static bool isRelocationSec(const typename ELFT::Shdr &Sec) {
static bool isRelocationSec(const typename ELFT::Shdr &Sec,
const typename ELFT::Ehdr &EHeader) {
return Sec.sh_type == ELF::SHT_REL || Sec.sh_type == ELF::SHT_RELA ||
Sec.sh_type == ELF::SHT_RELR || Sec.sh_type == ELF::SHT_ANDROID_REL ||
Sec.sh_type == ELF::SHT_ANDROID_RELA ||
Sec.sh_type == ELF::SHT_ANDROID_RELR ||
Sec.sh_type == ELF::SHT_AARCH64_AUTH_RELR;
(EHeader.e_machine == EM_AARCH64 &&
Sec.sh_type == ELF::SHT_AARCH64_AUTH_RELR);
}

template <class ELFT> void GNUELFDumper<ELFT>::printRelocations() {
Expand All @@ -3871,7 +3875,8 @@ template <class ELFT> void GNUELFDumper<ELFT>::printRelocations() {

if (!opts::RawRelr &&
(Sec.sh_type == ELF::SHT_RELR || Sec.sh_type == ELF::SHT_ANDROID_RELR ||
Sec.sh_type == ELF::SHT_AARCH64_AUTH_RELR)) {
(this->Obj.getHeader().e_machine == EM_AARCH64 &&
Sec.sh_type == ELF::SHT_AARCH64_AUTH_RELR))) {
Expected<Elf_Relr_Range> RelrsOrErr = this->Obj.relrs(Sec);
if (!RelrsOrErr)
return RelrsOrErr.takeError();
Expand All @@ -3883,7 +3888,7 @@ template <class ELFT> void GNUELFDumper<ELFT>::printRelocations() {

bool HasRelocSections = false;
for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
if (!isRelocationSec<ELFT>(Sec))
if (!isRelocationSec<ELFT>(Sec, this->Obj.getHeader()))
continue;
HasRelocSections = true;

Expand All @@ -3900,7 +3905,7 @@ template <class ELFT> void GNUELFDumper<ELFT>::printRelocations() {
OS << "\nRelocation section '" << Name << "' at offset 0x"
<< utohexstr(Offset, /*LowerCase=*/true) << " contains " << EntriesNum
<< " entries:\n";
printRelocHeaderFields<ELFT>(OS, Sec.sh_type);
printRelocHeaderFields<ELFT>(OS, Sec.sh_type, this->Obj.getHeader());
this->printRelocationsHelper(Sec);
}
if (!HasRelocSections)
Expand Down Expand Up @@ -6235,7 +6240,8 @@ void ELFDumper<ELFT>::forEachRelocationDo(
// sh_link field as an index of a symbol table.
const Elf_Shdr *SymTab;
if (Sec.sh_type != ELF::SHT_RELR && Sec.sh_type != ELF::SHT_ANDROID_RELR &&
Sec.sh_type != ELF::SHT_AARCH64_AUTH_RELR) {
!(Obj.getHeader().e_machine == EM_AARCH64 &&
Sec.sh_type == ELF::SHT_AARCH64_AUTH_RELR)) {
Expected<const Elf_Shdr *> SymTabOrErr = Obj.getSection(Sec.sh_link);
if (!SymTabOrErr) {
Warn(SymTabOrErr.takeError(), "unable to locate a symbol table for");
Expand Down Expand Up @@ -6264,11 +6270,8 @@ void ELFDumper<ELFT>::forEachRelocationDo(
}
break;
case ELF::SHT_AARCH64_AUTH_RELR:
if (Obj.getHeader().e_machine != EM_AARCH64) {
this->reportUniqueWarning(
"SHT_AARCH64_AUTH_RELR section in non-AArch64 ELF found, skipping");
if (Obj.getHeader().e_machine != EM_AARCH64)
break;
}
[[fallthrough]];
case ELF::SHT_RELR:
case ELF::SHT_ANDROID_RELR: {
Expand Down Expand Up @@ -6966,7 +6969,7 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printRelocations() {
ListScope D(W, "Relocations");

for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
if (!isRelocationSec<ELFT>(Sec))
if (!isRelocationSec<ELFT>(Sec, this->Obj.getHeader()))
continue;

StringRef Name = this->getPrintableSectionName(Sec);
Expand Down

0 comments on commit f9103d5

Please sign in to comment.