summaryrefslogtreecommitdiff
path: root/lib/Object/MachOObjectFile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Object/MachOObjectFile.cpp')
-rw-r--r--lib/Object/MachOObjectFile.cpp84
1 files changed, 80 insertions, 4 deletions
diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp
index 2e3415618e5f..e422903f2805 100644
--- a/lib/Object/MachOObjectFile.cpp
+++ b/lib/Object/MachOObjectFile.cpp
@@ -107,7 +107,8 @@ getSectionPtr(const MachOObjectFile &O, MachOObjectFile::LoadCommandInfo L,
}
static const char *getPtr(const MachOObjectFile &O, size_t Offset) {
- return O.getData().substr(Offset, 1).data();
+ assert(Offset <= O.getData().size());
+ return O.getData().data() + Offset;
}
static MachO::nlist_base
@@ -1011,7 +1012,43 @@ static Error checkThreadCommand(const MachOObjectFile &Obj,
CmdName + " command");
}
} else if (cputype == MachO::CPU_TYPE_X86_64) {
- if (flavor == MachO::x86_THREAD_STATE64) {
+ if (flavor == MachO::x86_THREAD_STATE) {
+ if (count != MachO::x86_THREAD_STATE_COUNT)
+ return malformedError("load command " + Twine(LoadCommandIndex) +
+ " count not x86_THREAD_STATE_COUNT for "
+ "flavor number " + Twine(nflavor) + " which is "
+ "a x86_THREAD_STATE flavor in " + CmdName +
+ " command");
+ if (state + sizeof(MachO::x86_thread_state_t) > end)
+ return malformedError("load command " + Twine(LoadCommandIndex) +
+ " x86_THREAD_STATE extends past end of "
+ "command in " + CmdName + " command");
+ state += sizeof(MachO::x86_thread_state_t);
+ } else if (flavor == MachO::x86_FLOAT_STATE) {
+ if (count != MachO::x86_FLOAT_STATE_COUNT)
+ return malformedError("load command " + Twine(LoadCommandIndex) +
+ " count not x86_FLOAT_STATE_COUNT for "
+ "flavor number " + Twine(nflavor) + " which is "
+ "a x86_FLOAT_STATE flavor in " + CmdName +
+ " command");
+ if (state + sizeof(MachO::x86_float_state_t) > end)
+ return malformedError("load command " + Twine(LoadCommandIndex) +
+ " x86_FLOAT_STATE extends past end of "
+ "command in " + CmdName + " command");
+ state += sizeof(MachO::x86_float_state_t);
+ } else if (flavor == MachO::x86_EXCEPTION_STATE) {
+ if (count != MachO::x86_EXCEPTION_STATE_COUNT)
+ return malformedError("load command " + Twine(LoadCommandIndex) +
+ " count not x86_EXCEPTION_STATE_COUNT for "
+ "flavor number " + Twine(nflavor) + " which is "
+ "a x86_EXCEPTION_STATE flavor in " + CmdName +
+ " command");
+ if (state + sizeof(MachO::x86_exception_state_t) > end)
+ return malformedError("load command " + Twine(LoadCommandIndex) +
+ " x86_EXCEPTION_STATE extends past end of "
+ "command in " + CmdName + " command");
+ state += sizeof(MachO::x86_exception_state_t);
+ } else if (flavor == MachO::x86_THREAD_STATE64) {
if (count != MachO::x86_THREAD_STATE64_COUNT)
return malformedError("load command " + Twine(LoadCommandIndex) +
" count not x86_THREAD_STATE64_COUNT for "
@@ -1023,6 +1060,18 @@ static Error checkThreadCommand(const MachOObjectFile &Obj,
" x86_THREAD_STATE64 extends past end of "
"command in " + CmdName + " command");
state += sizeof(MachO::x86_thread_state64_t);
+ } else if (flavor == MachO::x86_EXCEPTION_STATE64) {
+ if (count != MachO::x86_EXCEPTION_STATE64_COUNT)
+ return malformedError("load command " + Twine(LoadCommandIndex) +
+ " count not x86_EXCEPTION_STATE64_COUNT for "
+ "flavor number " + Twine(nflavor) + " which is "
+ "a x86_EXCEPTION_STATE64 flavor in " + CmdName +
+ " command");
+ if (state + sizeof(MachO::x86_exception_state64_t) > end)
+ return malformedError("load command " + Twine(LoadCommandIndex) +
+ " x86_EXCEPTION_STATE64 extends past end of "
+ "command in " + CmdName + " command");
+ state += sizeof(MachO::x86_exception_state64_t);
} else {
return malformedError("load command " + Twine(LoadCommandIndex) +
" unknown flavor (" + Twine(flavor) + ") for "
@@ -1659,6 +1708,10 @@ void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
Expected<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const {
StringRef StringTable = getStringTableData();
MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb);
+ if (Entry.n_strx == 0)
+ // A n_strx value of 0 indicates that no name is associated with a
+ // particular symbol table entry.
+ return StringRef();
const char *Start = &StringTable.data()[Entry.n_strx];
if (Start < getData().begin() || Start >= getData().end()) {
return malformedError("bad string index: " + Twine(Entry.n_strx) +
@@ -1886,6 +1939,27 @@ uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const {
return uint64_t(1) << Align;
}
+Expected<SectionRef> MachOObjectFile::getSection(unsigned SectionIndex) const {
+ if (SectionIndex < 1 || SectionIndex > Sections.size())
+ return malformedError("bad section index: " + Twine((int)SectionIndex));
+
+ DataRefImpl DRI;
+ DRI.d.a = SectionIndex - 1;
+ return SectionRef(DRI, this);
+}
+
+Expected<SectionRef> MachOObjectFile::getSection(StringRef SectionName) const {
+ StringRef SecName;
+ for (const SectionRef &Section : sections()) {
+ if (std::error_code E = Section.getName(SecName))
+ return errorCodeToError(E);
+ if (SecName == SectionName) {
+ return Section;
+ }
+ }
+ return errorCodeToError(object_error::parse_failed);
+}
+
bool MachOObjectFile::isSectionCompressed(DataRefImpl Sec) const {
return false;
}
@@ -1916,8 +1990,10 @@ unsigned MachOObjectFile::getSectionID(SectionRef Sec) const {
}
bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const {
- // FIXME: Unimplemented.
- return false;
+ uint32_t Flags = getSectionFlags(*this, Sec);
+ unsigned SectionType = Flags & MachO::SECTION_TYPE;
+ return SectionType == MachO::S_ZEROFILL ||
+ SectionType == MachO::S_GB_ZEROFILL;
}
bool MachOObjectFile::isSectionBitcode(DataRefImpl Sec) const {