[llvm] r179072 - Add a SectionBase struct.
Rafael Espindola
rafael.espindola at gmail.com
Mon Apr 8 16:57:13 PDT 2013
Author: rafael
Date: Mon Apr 8 18:57:13 2013
New Revision: 179072
URL: http://llvm.org/viewvc/llvm-project?rev=179072&view=rev
Log:
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.
Modified:
llvm/trunk/include/llvm/Object/MachO.h
llvm/trunk/lib/Object/MachOObjectFile.cpp
Modified: llvm/trunk/include/llvm/Object/MachO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachO.h?rev=179072&r1=179071&r2=179072&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachO.h (original)
+++ llvm/trunk/include/llvm/Object/MachO.h Mon Apr 8 18:57:13 2013
@@ -27,6 +27,11 @@ namespace llvm {
namespace object {
namespace MachOFormat {
+ struct SectionBase {
+ char Name[16];
+ char SegmentName[16];
+ };
+
template<bool is64Bits>
struct Section;
@@ -254,6 +259,7 @@ private:
typedef SmallVector<DataRefImpl, 1> SectionList;
SectionList Sections;
+ const MachOFormat::SectionBase *getSectionBase(DataRefImpl DRI) const;
void moveToNextSection(DataRefImpl &DRI) const;
Modified: llvm/trunk/lib/Object/MachOObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObjectFile.cpp?rev=179072&r1=179071&r2=179072&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/MachOObjectFile.cpp Mon Apr 8 18:57:13 2013
@@ -509,11 +509,8 @@ error_code MachOObjectFile::getSectionNe
const MachOFormat::Section<false> *
MachOObjectFile::getSection(DataRefImpl DRI) const {
assert(!is64Bit());
- const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
- uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(Command);
- uintptr_t SectionAddr = CommandAddr + sizeof(macho::SegmentLoadCommand) +
- DRI.d.b * sizeof(MachOFormat::Section<false>);
- return reinterpret_cast<const MachOFormat::Section<false>*>(SectionAddr);
+ const MachOFormat::SectionBase *Addr = getSectionBase(DRI);
+ return reinterpret_cast<const MachOFormat::Section<false>*>(Addr);
}
std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
@@ -523,14 +520,27 @@ std::size_t MachOObjectFile::getSectionI
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<uintptr_t>(Command);
+
+ bool Is64 = is64Bit();
+ unsigned SegmentLoadSize =
+ Is64 ? sizeof(MachOFormat::SegmentLoadCommand<true>) :
+ sizeof(MachOFormat::SegmentLoadCommand<false>);
+ unsigned SectionSize = Is64 ? sizeof(MachOFormat::Section<true>) :
+ sizeof(MachOFormat::Section<false>);
+
+ uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + DRI.d.b * SectionSize;
+ return reinterpret_cast<const MachOFormat::SectionBase*>(SectionAddr);
+}
+
const MachOFormat::Section<true> *
MachOObjectFile::getSection64(DataRefImpl DRI) const {
assert(is64Bit());
- const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
- uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(Command);
- uintptr_t SectionAddr = CommandAddr + sizeof(macho::Segment64LoadCommand) +
- DRI.d.b * sizeof(MachOFormat::Section<true>);
- return reinterpret_cast<const MachOFormat::Section<true>*>(SectionAddr);
+ const MachOFormat::SectionBase *Addr = getSectionBase(DRI);
+ return reinterpret_cast<const MachOFormat::Section<true>*>(Addr);
}
static StringRef parseSegmentOrSectionName(const char *P) {
@@ -542,13 +552,8 @@ static StringRef parseSegmentOrSectionNa
}
ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
- if (is64Bit()) {
- const MachOFormat::Section<true> *sec = getSection64(DRI);
- return ArrayRef<char>(sec->Name);
- } else {
- const MachOFormat::Section<false> *sec = getSection(DRI);
- return ArrayRef<char>(sec->Name);
- }
+ const MachOFormat::SectionBase *Base = getSectionBase(DRI);
+ return ArrayRef<char>(Base->Name);
}
error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
@@ -560,13 +565,8 @@ error_code MachOObjectFile::getSectionNa
ArrayRef<char>
MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
- if (is64Bit()) {
- const MachOFormat::Section<true> *sec = getSection64(Sec);
- return ArrayRef<char>(sec->SegmentName, 16);
- } else {
- const MachOFormat::Section<false> *sec = getSection(Sec);
- return ArrayRef<char>(sec->SegmentName);
- }
+ const MachOFormat::SectionBase *Base = getSectionBase(Sec);
+ return ArrayRef<char>(Base->SegmentName);
}
StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
More information about the llvm-commits
mailing list