From 16a081f64fcbae385a0ccf55bd1c539103186c10 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 29 Jun 2015 23:55:05 +0000 Subject: [PATCH] Use asserts for checks that should never fail. If a section is not SHT_REL or SHT_RELA, we never create a valid iterator, so the getRelocation* methods should always see a section with the correct type. llvm-svn: 241023 --- llvm/include/llvm/Object/ELFObjectFile.h | 48 ++++++------------------ 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h index 9c82fa8486b9..39c504848e77 100644 --- a/llvm/include/llvm/Object/ELFObjectFile.h +++ b/llvm/include/llvm/Object/ELFObjectFile.h @@ -637,18 +637,10 @@ symbol_iterator ELFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { uint32_t symbolIdx; const Elf_Shdr *sec = getRelSection(Rel); - switch (sec->sh_type) { - default: - report_fatal_error("Invalid section type in Rel!"); - case ELF::SHT_REL: { + if (sec->sh_type == ELF::SHT_REL) symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL()); - break; - } - case ELF::SHT_RELA: { + else symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL()); - break; - } - } if (!symbolIdx) return symbol_end(); @@ -697,32 +689,20 @@ uint64_t ELFObjectFile::getRelocationOffset(DataRefImpl Rel) const { template uint64_t ELFObjectFile::getROffset(DataRefImpl Rel) const { const Elf_Shdr *sec = getRelSection(Rel); - switch (sec->sh_type) { - default: - report_fatal_error("Invalid section type in Rel!"); - case ELF::SHT_REL: + if (sec->sh_type == ELF::SHT_REL) return getRel(Rel)->r_offset; - case ELF::SHT_RELA: - return getRela(Rel)->r_offset; - } + + return getRela(Rel)->r_offset; } template std::error_code ELFObjectFile::getRelocationType(DataRefImpl Rel, uint64_t &Result) const { const Elf_Shdr *sec = getRelSection(Rel); - switch (sec->sh_type) { - default: - report_fatal_error("Invalid section type in Rel!"); - case ELF::SHT_REL: { + if (sec->sh_type == ELF::SHT_REL) Result = getRel(Rel)->getType(EF.isMips64EL()); - break; - } - case ELF::SHT_RELA: { + else Result = getRela(Rel)->getType(EF.isMips64EL()); - break; - } - } return std::error_code(); } @@ -736,18 +716,10 @@ std::error_code ELFObjectFile::getRelocationTypeName( DataRefImpl Rel, SmallVectorImpl &Result) const { const Elf_Shdr *sec = getRelSection(Rel); uint32_t type; - switch (sec->sh_type) { - default: - return object_error::parse_failed; - case ELF::SHT_REL: { + if (sec->sh_type == ELF::SHT_REL) type = getRel(Rel)->getType(EF.isMips64EL()); - break; - } - case ELF::SHT_RELA: { + else type = getRela(Rel)->getType(EF.isMips64EL()); - break; - } - } EF.getRelocationTypeName(type, Result); return std::error_code(); @@ -775,12 +747,14 @@ ELFObjectFile::getSymbol(DataRefImpl Symb) const { template const typename ELFObjectFile::Elf_Rel * ELFObjectFile::getRel(DataRefImpl Rel) const { + assert(getRelSection(Rel)->sh_type == ELF::SHT_REL); return EF.template getEntry(Rel.d.a, Rel.d.b); } template const typename ELFObjectFile::Elf_Rela * ELFObjectFile::getRela(DataRefImpl Rela) const { + assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA); return EF.template getEntry(Rela.d.a, Rela.d.b); }