From 3388589fc102b873ee9b73ffdab0f532ee3ceda6 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 8 Apr 2013 23:57:13 +0000 Subject: Add a SectionBase struct. Use it to share code and when we don't need to know if we have a 32 or 64 bit Section. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179072 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Object/MachOObjectFile.cpp | 48 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'lib/Object/MachOObjectFile.cpp') diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index 30ab00f450..3931c53ac8 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -509,11 +509,8 @@ error_code MachOObjectFile::getSectionNext(DataRefImpl DRI, const MachOFormat::Section * MachOObjectFile::getSection(DataRefImpl DRI) const { assert(!is64Bit()); - const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a); - uintptr_t CommandAddr = reinterpret_cast(Command); - uintptr_t SectionAddr = CommandAddr + sizeof(macho::SegmentLoadCommand) + - DRI.d.b * sizeof(MachOFormat::Section); - return reinterpret_cast*>(SectionAddr); + const MachOFormat::SectionBase *Addr = getSectionBase(DRI); + return reinterpret_cast*>(Addr); } std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const { @@ -523,14 +520,27 @@ std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const { return std::distance(Sections.begin(), loc); } +const MachOFormat::SectionBase* +MachOObjectFile::getSectionBase(DataRefImpl DRI) const { + const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a); + uintptr_t CommandAddr = reinterpret_cast(Command); + + bool Is64 = is64Bit(); + unsigned SegmentLoadSize = + Is64 ? sizeof(MachOFormat::SegmentLoadCommand) : + sizeof(MachOFormat::SegmentLoadCommand); + unsigned SectionSize = Is64 ? sizeof(MachOFormat::Section) : + sizeof(MachOFormat::Section); + + uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + DRI.d.b * SectionSize; + return reinterpret_cast(SectionAddr); +} + const MachOFormat::Section * MachOObjectFile::getSection64(DataRefImpl DRI) const { assert(is64Bit()); - const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a); - uintptr_t CommandAddr = reinterpret_cast(Command); - uintptr_t SectionAddr = CommandAddr + sizeof(macho::Segment64LoadCommand) + - DRI.d.b * sizeof(MachOFormat::Section); - return reinterpret_cast*>(SectionAddr); + const MachOFormat::SectionBase *Addr = getSectionBase(DRI); + return reinterpret_cast*>(Addr); } static StringRef parseSegmentOrSectionName(const char *P) { @@ -542,13 +552,8 @@ static StringRef parseSegmentOrSectionName(const char *P) { } ArrayRef MachOObjectFile::getSectionRawName(DataRefImpl DRI) const { - if (is64Bit()) { - const MachOFormat::Section *sec = getSection64(DRI); - return ArrayRef(sec->Name); - } else { - const MachOFormat::Section *sec = getSection(DRI); - return ArrayRef(sec->Name); - } + const MachOFormat::SectionBase *Base = getSectionBase(DRI); + return ArrayRef(Base->Name); } error_code MachOObjectFile::getSectionName(DataRefImpl DRI, @@ -560,13 +565,8 @@ error_code MachOObjectFile::getSectionName(DataRefImpl DRI, ArrayRef MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { - if (is64Bit()) { - const MachOFormat::Section *sec = getSection64(Sec); - return ArrayRef(sec->SegmentName, 16); - } else { - const MachOFormat::Section *sec = getSection(Sec); - return ArrayRef(sec->SegmentName); - } + const MachOFormat::SectionBase *Base = getSectionBase(Sec); + return ArrayRef(Base->SegmentName); } StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const { -- cgit v1.2.3-18-g5258