2011-04-22 03:19:48 +00:00
|
|
|
//===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the MachOObjectFile class, which binds the MachOObject
|
|
|
|
// class to the generic ObjectFile wrapper.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-10-11 17:32:27 +00:00
|
|
|
#include "llvm/Object/MachO.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2011-04-22 03:19:48 +00:00
|
|
|
#include "llvm/Object/MachOFormat.h"
|
2013-04-09 14:49:08 +00:00
|
|
|
#include "llvm/Support/Casting.h"
|
2013-04-07 20:01:29 +00:00
|
|
|
#include "llvm/Support/DataExtractor.h"
|
2011-10-26 20:42:54 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
2011-04-22 03:19:48 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include <cctype>
|
|
|
|
#include <cstring>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
|
|
|
namespace llvm {
|
2011-10-11 17:32:27 +00:00
|
|
|
namespace object {
|
2011-04-22 03:19:48 +00:00
|
|
|
|
2013-04-13 01:45:40 +00:00
|
|
|
MachOObjectFileBase::MachOObjectFileBase(MemoryBuffer *Object,
|
|
|
|
bool IsLittleEndian, bool Is64bits,
|
2013-04-09 14:49:08 +00:00
|
|
|
error_code &ec)
|
2013-04-13 01:45:40 +00:00
|
|
|
: ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object) {
|
2011-09-08 20:52:17 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
bool MachOObjectFileBase::is64Bit() const {
|
2013-04-13 01:45:40 +00:00
|
|
|
return isa<MachOObjectFileLE64>(this) || isa<MachOObjectFileBE64>(this);
|
2013-04-07 16:07:35 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
void MachOObjectFileBase::ReadULEB128s(uint64_t Index,
|
|
|
|
SmallVectorImpl<uint64_t> &Out) const {
|
2013-04-07 20:01:29 +00:00
|
|
|
DataExtractor extractor(ObjectFile::getData(), true, 0);
|
|
|
|
|
|
|
|
uint32_t offset = Index;
|
|
|
|
uint64_t data = 0;
|
|
|
|
while (uint64_t delta = extractor.getULEB128(&offset)) {
|
|
|
|
data += delta;
|
|
|
|
Out.push_back(data);
|
|
|
|
}
|
2013-04-07 16:07:35 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
unsigned MachOObjectFileBase::getHeaderSize() const {
|
2013-04-07 19:31:49 +00:00
|
|
|
return is64Bit() ? macho::Header64Size : macho::Header32Size;
|
2013-04-07 19:05:30 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
StringRef MachOObjectFileBase::getData(size_t Offset, size_t Size) const {
|
2013-04-07 19:42:15 +00:00
|
|
|
return ObjectFile::getData().substr(Offset, Size);
|
2013-04-07 19:05:30 +00:00
|
|
|
}
|
|
|
|
|
2011-04-22 03:19:48 +00:00
|
|
|
ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
|
2013-04-08 13:25:33 +00:00
|
|
|
StringRef Magic = Buffer->getBuffer().slice(0, 4);
|
2011-06-25 17:54:50 +00:00
|
|
|
error_code ec;
|
2013-04-09 14:49:08 +00:00
|
|
|
ObjectFile *Ret;
|
2013-04-13 01:45:40 +00:00
|
|
|
if (Magic == "\xFE\xED\xFA\xCE")
|
|
|
|
Ret = new MachOObjectFileBE32(Buffer, ec);
|
|
|
|
else if (Magic == "\xCE\xFA\xED\xFE")
|
|
|
|
Ret = new MachOObjectFileLE32(Buffer, ec);
|
|
|
|
else if (Magic == "\xFE\xED\xFA\xCF")
|
|
|
|
Ret = new MachOObjectFileBE64(Buffer, ec);
|
|
|
|
else if (Magic == "\xCF\xFA\xED\xFE")
|
|
|
|
Ret = new MachOObjectFileLE64(Buffer, ec);
|
2013-04-09 14:49:08 +00:00
|
|
|
else
|
2013-04-13 01:45:40 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-04-07 16:58:48 +00:00
|
|
|
if (ec)
|
2011-04-22 03:19:48 +00:00
|
|
|
return NULL;
|
2013-04-07 16:58:48 +00:00
|
|
|
return Ret;
|
2011-04-22 03:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===-- Symbols -----------------------------------------------------------===*/
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
error_code MachOObjectFileBase::getSymbolValue(DataRefImpl Symb,
|
|
|
|
uint64_t &Val) const {
|
|
|
|
report_fatal_error("getSymbolValue unimplemented in MachOObjectFileBase");
|
2012-10-29 10:47:00 +00:00
|
|
|
}
|
2011-09-14 01:22:52 +00:00
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
symbol_iterator MachOObjectFileBase::begin_dynamic_symbols() const {
|
2012-02-28 00:40:37 +00:00
|
|
|
// TODO: implement
|
2013-04-09 14:49:08 +00:00
|
|
|
report_fatal_error("Dynamic symbols unimplemented in MachOObjectFileBase");
|
2012-02-28 00:40:37 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
symbol_iterator MachOObjectFileBase::end_dynamic_symbols() const {
|
2012-02-28 00:40:37 +00:00
|
|
|
// TODO: implement
|
2013-04-09 14:49:08 +00:00
|
|
|
report_fatal_error("Dynamic symbols unimplemented in MachOObjectFileBase");
|
2012-02-28 00:40:37 +00:00
|
|
|
}
|
2011-04-22 03:19:48 +00:00
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
library_iterator MachOObjectFileBase::begin_libraries_needed() const {
|
2012-03-01 01:36:50 +00:00
|
|
|
// TODO: implement
|
2013-04-09 14:49:08 +00:00
|
|
|
report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
|
2012-03-01 01:36:50 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
library_iterator MachOObjectFileBase::end_libraries_needed() const {
|
2012-03-01 01:36:50 +00:00
|
|
|
// TODO: implement
|
2013-04-09 14:49:08 +00:00
|
|
|
report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
|
2012-03-01 01:36:50 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
StringRef MachOObjectFileBase::getLoadName() const {
|
2012-03-01 22:19:54 +00:00
|
|
|
// TODO: Implement
|
2013-04-09 14:49:08 +00:00
|
|
|
report_fatal_error("get_load_name() unimplemented in MachOObjectFileBase");
|
2012-03-01 22:19:54 +00:00
|
|
|
}
|
|
|
|
|
2011-04-22 03:19:48 +00:00
|
|
|
/*===-- Sections ----------------------------------------------------------===*/
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
std::size_t MachOObjectFileBase::getSectionIndex(DataRefImpl Sec) const {
|
2011-10-07 19:25:32 +00:00
|
|
|
SectionList::const_iterator loc =
|
|
|
|
std::find(Sections.begin(), Sections.end(), Sec);
|
|
|
|
assert(loc != Sections.end() && "Sec is not a valid section!");
|
|
|
|
return std::distance(Sections.begin(), loc);
|
|
|
|
}
|
|
|
|
|
2013-04-13 01:45:40 +00:00
|
|
|
StringRef MachOObjectFileBase::parseSegmentOrSectionName(const char *P) const {
|
Add a function to get the segment name of a section.
On MachO, sections also have segment names. When a tool looking at a .o file
prints a segment name, this is what they mean. In reality, a .o has only one
anonymous, segment.
This patch adds a MachO only function to fetch that segment name. I named it
getSectionFinalSegmentName since the main use for the name seems to be inform
the linker with segment this section should go to.
The patch also changes MachOObjectFile::getSectionName to return just the
section name instead of computing SegmentName,SectionName.
The main difference from the previous patch is that it doesn't use
InMemoryStruct. It is extremely dangerous: if the endians match it returns
a pointer to the file buffer, if not, it returns a pointer to an internal buffer
that is overwritten in the next API call.
We should change all of this code to use
support::detail::packed_endian_specific_integral like ELF, but since these
functions only handle strings, they work with big and little endian machines
as is.
I have tested this by installing ubuntu 12.10 ppc on qemu, that is why it took
so long :-)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170838 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-21 03:47:03 +00:00
|
|
|
if (P[15] == 0)
|
|
|
|
// Null terminated.
|
|
|
|
return P;
|
|
|
|
// Not null terminated, so this is a 16 char string.
|
|
|
|
return StringRef(P, 16);
|
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
error_code MachOObjectFileBase::isSectionData(DataRefImpl DRI,
|
|
|
|
bool &Result) const {
|
2011-09-28 20:57:30 +00:00
|
|
|
// FIXME: Unimplemented.
|
|
|
|
Result = false;
|
|
|
|
return object_error::success;
|
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
error_code MachOObjectFileBase::isSectionBSS(DataRefImpl DRI,
|
|
|
|
bool &Result) const {
|
2011-09-28 20:57:30 +00:00
|
|
|
// FIXME: Unimplemented.
|
|
|
|
Result = false;
|
|
|
|
return object_error::success;
|
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
error_code
|
|
|
|
MachOObjectFileBase::isSectionRequiredForExecution(DataRefImpl Sec,
|
|
|
|
bool &Result) const {
|
2012-10-10 01:45:52 +00:00
|
|
|
// FIXME: Unimplemented.
|
2012-04-12 20:13:57 +00:00
|
|
|
Result = true;
|
|
|
|
return object_error::success;
|
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
error_code MachOObjectFileBase::isSectionVirtual(DataRefImpl Sec,
|
|
|
|
bool &Result) const {
|
2012-10-10 01:45:52 +00:00
|
|
|
// FIXME: Unimplemented.
|
2012-04-12 20:13:57 +00:00
|
|
|
Result = false;
|
|
|
|
return object_error::success;
|
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
error_code MachOObjectFileBase::isSectionReadOnlyData(DataRefImpl Sec,
|
|
|
|
bool &Result) const {
|
2012-10-10 01:41:33 +00:00
|
|
|
// Consider using the code from isSectionText to look for __const sections.
|
|
|
|
// Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
|
|
|
|
// to use section attributes to distinguish code from data.
|
|
|
|
|
|
|
|
// FIXME: Unimplemented.
|
|
|
|
Result = false;
|
|
|
|
return object_error::success;
|
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
relocation_iterator MachOObjectFileBase::getSectionRelBegin(DataRefImpl Sec) const {
|
2011-10-07 19:25:32 +00:00
|
|
|
DataRefImpl ret;
|
|
|
|
ret.d.b = getSectionIndex(Sec);
|
|
|
|
return relocation_iterator(RelocationRef(ret, this));
|
|
|
|
}
|
2013-04-08 20:45:01 +00:00
|
|
|
|
2011-04-22 03:19:48 +00:00
|
|
|
|
2011-09-08 20:52:17 +00:00
|
|
|
/*===-- Relocations -------------------------------------------------------===*/
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
error_code MachOObjectFileBase::getRelocationNext(DataRefImpl Rel,
|
|
|
|
RelocationRef &Res) const {
|
2011-09-08 20:52:17 +00:00
|
|
|
++Rel.d.a;
|
|
|
|
Res = RelocationRef(Rel, this);
|
|
|
|
return object_error::success;
|
|
|
|
}
|
2011-10-24 23:20:07 +00:00
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
error_code MachOObjectFileBase::getLibraryNext(DataRefImpl LibData,
|
|
|
|
LibraryRef &Res) const {
|
|
|
|
report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
|
2012-03-01 01:36:50 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
error_code MachOObjectFileBase::getLibraryPath(DataRefImpl LibData,
|
|
|
|
StringRef &Res) const {
|
|
|
|
report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
|
2012-03-01 01:36:50 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 02:21:31 +00:00
|
|
|
error_code MachOObjectFileBase::getRelocationAdditionalInfo(DataRefImpl Rel,
|
|
|
|
int64_t &Res) const {
|
|
|
|
Res = 0;
|
|
|
|
return object_error::success;
|
|
|
|
}
|
|
|
|
|
2012-03-01 01:36:50 +00:00
|
|
|
|
2011-04-22 03:19:48 +00:00
|
|
|
/*===-- Miscellaneous -----------------------------------------------------===*/
|
|
|
|
|
2013-04-09 14:49:08 +00:00
|
|
|
uint8_t MachOObjectFileBase::getBytesInAddress() const {
|
2013-04-07 19:05:30 +00:00
|
|
|
return is64Bit() ? 8 : 4;
|
2011-04-22 03:19:48 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 17:32:27 +00:00
|
|
|
} // end namespace object
|
2011-04-22 03:19:48 +00:00
|
|
|
} // end namespace llvm
|