From 3da9f8c04cfcaeda5433f968c0d23729b8a0b90a Mon Sep 17 00:00:00 2001 From: George Rimar Date: Fri, 1 Feb 2019 07:50:08 +0000 Subject: [PATCH] [obj2yaml] - Merge dumpRelSection and dumpRelaSection. NFC. These methods are very similar, patch merge them into one. Differential revision: https://reviews.llvm.org/D57461 llvm-svn: 352840 --- tools/obj2yaml/elf2yaml.cpp | 74 +++++++++++++------------------------ 1 file changed, 25 insertions(+), 49 deletions(-) diff --git a/tools/obj2yaml/elf2yaml.cpp b/tools/obj2yaml/elf2yaml.cpp index ec87a20fc2b..e2fb46306d3 100644 --- a/tools/obj2yaml/elf2yaml.cpp +++ b/tools/obj2yaml/elf2yaml.cpp @@ -51,8 +51,7 @@ class ELFDumper { std::error_code dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab, ELFYAML::Relocation &R); - ErrorOr dumpRelSection(const Elf_Shdr *Shdr); - ErrorOr dumpRelaSection(const Elf_Shdr *Shdr); + ErrorOr dumpRelocSection(const Elf_Shdr *Shdr); ErrorOr dumpContentSection(const Elf_Shdr *Shdr); ErrorOr dumpNoBitsSection(const Elf_Shdr *Shdr); @@ -147,15 +146,9 @@ template ErrorOr ELFDumper::dump() { ShndxTable = *TableOrErr; break; } + case ELF::SHT_REL: case ELF::SHT_RELA: { - ErrorOr S = dumpRelaSection(&Sec); - if (std::error_code EC = S.getError()) - return EC; - Y->Sections.push_back(std::unique_ptr(S.get())); - break; - } - case ELF::SHT_REL: { - ErrorOr S = dumpRelSection(&Sec); + ErrorOr S = dumpRelocSection(&Sec); if (std::error_code EC = S.getError()) return EC; Y->Sections.push_back(std::unique_ptr(S.get())); @@ -359,10 +352,8 @@ ELFDumper::dumpCommonRelocationSection(const Elf_Shdr *Shdr, template ErrorOr -ELFDumper::dumpRelSection(const Elf_Shdr *Shdr) { - assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL"); +ELFDumper::dumpRelocSection(const Elf_Shdr *Shdr) { auto S = make_unique(); - if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S)) return EC; @@ -371,42 +362,27 @@ ELFDumper::dumpRelSection(const Elf_Shdr *Shdr) { return errorToErrorCode(SymTabOrErr.takeError()); const Elf_Shdr *SymTab = *SymTabOrErr; - auto Rels = Obj.rels(Shdr); - if (!Rels) - return errorToErrorCode(Rels.takeError()); - for (const Elf_Rel &Rel : *Rels) { - ELFYAML::Relocation R; - if (std::error_code EC = dumpRelocation(&Rel, SymTab, R)) - return EC; - S->Relocations.push_back(R); - } - - return S.release(); -} - -template -ErrorOr -ELFDumper::dumpRelaSection(const Elf_Shdr *Shdr) { - assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA"); - auto S = make_unique(); - - if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S)) - return EC; - - auto SymTabOrErr = Obj.getSection(Shdr->sh_link); - if (!SymTabOrErr) - return errorToErrorCode(SymTabOrErr.takeError()); - const Elf_Shdr *SymTab = *SymTabOrErr; - - auto Rels = Obj.relas(Shdr); - if (!Rels) - return errorToErrorCode(Rels.takeError()); - for (const Elf_Rela &Rel : *Rels) { - ELFYAML::Relocation R; - if (std::error_code EC = dumpRelocation(&Rel, SymTab, R)) - return EC; - R.Addend = Rel.r_addend; - S->Relocations.push_back(R); + if (Shdr->sh_type == ELF::SHT_REL) { + auto Rels = Obj.rels(Shdr); + if (!Rels) + return errorToErrorCode(Rels.takeError()); + for (const Elf_Rel &Rel : *Rels) { + ELFYAML::Relocation R; + if (std::error_code EC = dumpRelocation(&Rel, SymTab, R)) + return EC; + S->Relocations.push_back(R); + } + } else { + auto Rels = Obj.relas(Shdr); + if (!Rels) + return errorToErrorCode(Rels.takeError()); + for (const Elf_Rela &Rel : *Rels) { + ELFYAML::Relocation R; + if (std::error_code EC = dumpRelocation(&Rel, SymTab, R)) + return EC; + R.Addend = Rel.r_addend; + S->Relocations.push_back(R); + } } return S.release();