2011-10-11 17:32:27 +00:00
|
|
|
//===- MachO.h - MachO object file implementation ---------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares the MachOObjectFile class, which binds the MachOObject
|
|
|
|
// class to the generic ObjectFile wrapper.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_OBJECT_MACHO_H
|
|
|
|
#define LLVM_OBJECT_MACHO_H
|
|
|
|
|
2013-04-05 15:15:22 +00:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2011-10-11 17:32:27 +00:00
|
|
|
#include "llvm/Object/MachOObject.h"
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2013-04-05 18:45:28 +00:00
|
|
|
#include "llvm/Support/Endian.h"
|
2011-10-11 17:32:27 +00:00
|
|
|
#include "llvm/Support/MachO.h"
|
2011-10-26 20:42:54 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-10-11 17:32:27 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace object {
|
|
|
|
|
2013-04-05 18:45:28 +00:00
|
|
|
namespace MachOFormat {
|
|
|
|
struct Section {
|
|
|
|
char Name[16];
|
|
|
|
char SegmentName[16];
|
|
|
|
support::ulittle32_t Address;
|
|
|
|
support::ulittle32_t Size;
|
|
|
|
support::ulittle32_t Offset;
|
|
|
|
support::ulittle32_t Align;
|
|
|
|
support::ulittle32_t RelocationTableOffset;
|
|
|
|
support::ulittle32_t NumRelocationTableEntries;
|
|
|
|
support::ulittle32_t Flags;
|
|
|
|
support::ulittle32_t Reserved1;
|
|
|
|
support::ulittle32_t Reserved2;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Section64 {
|
|
|
|
char Name[16];
|
|
|
|
char SegmentName[16];
|
|
|
|
support::ulittle64_t Address;
|
|
|
|
support::ulittle64_t Size;
|
|
|
|
support::ulittle32_t Offset;
|
|
|
|
support::ulittle32_t Align;
|
|
|
|
support::ulittle32_t RelocationTableOffset;
|
|
|
|
support::ulittle32_t NumRelocationTableEntries;
|
|
|
|
support::ulittle32_t Flags;
|
|
|
|
support::ulittle32_t Reserved1;
|
|
|
|
support::ulittle32_t Reserved2;
|
|
|
|
support::ulittle32_t Reserved3;
|
|
|
|
};
|
2013-04-06 01:24:11 +00:00
|
|
|
|
|
|
|
struct RelocationEntry {
|
|
|
|
support::ulittle32_t Word0;
|
|
|
|
support::ulittle32_t Word1;
|
|
|
|
};
|
2013-04-06 01:59:05 +00:00
|
|
|
|
|
|
|
struct SymbolTableEntry {
|
|
|
|
support::ulittle32_t StringIndex;
|
|
|
|
uint8_t Type;
|
|
|
|
uint8_t SectionIndex;
|
|
|
|
support::ulittle16_t Flags;
|
|
|
|
support::ulittle32_t Value;
|
|
|
|
};
|
|
|
|
|
2013-04-06 02:15:44 +00:00
|
|
|
struct Symbol64TableEntry {
|
|
|
|
support::ulittle32_t StringIndex;
|
|
|
|
uint8_t Type;
|
|
|
|
uint8_t SectionIndex;
|
|
|
|
support::ulittle16_t Flags;
|
|
|
|
support::ulittle64_t Value;
|
|
|
|
};
|
2013-04-06 03:31:08 +00:00
|
|
|
|
2013-04-07 18:08:12 +00:00
|
|
|
struct LoadCommand {
|
|
|
|
support::ulittle32_t Type;
|
|
|
|
support::ulittle32_t Size;
|
|
|
|
};
|
|
|
|
|
2013-04-06 03:31:08 +00:00
|
|
|
struct SymtabLoadCommand {
|
|
|
|
support::ulittle32_t Type;
|
|
|
|
support::ulittle32_t Size;
|
|
|
|
support::ulittle32_t SymbolTableOffset;
|
|
|
|
support::ulittle32_t NumSymbolTableEntries;
|
|
|
|
support::ulittle32_t StringTableOffset;
|
|
|
|
support::ulittle32_t StringTableSize;
|
|
|
|
};
|
2013-04-06 03:50:05 +00:00
|
|
|
|
|
|
|
struct SegmentLoadCommand {
|
|
|
|
support::ulittle32_t Type;
|
|
|
|
support::ulittle32_t Size;
|
|
|
|
char Name[16];
|
|
|
|
support::ulittle32_t VMAddress;
|
|
|
|
support::ulittle32_t VMSize;
|
|
|
|
support::ulittle32_t FileOffset;
|
|
|
|
support::ulittle32_t FileSize;
|
|
|
|
support::ulittle32_t MaxVMProtection;
|
|
|
|
support::ulittle32_t InitialVMProtection;
|
|
|
|
support::ulittle32_t NumSections;
|
|
|
|
support::ulittle32_t Flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Segment64LoadCommand {
|
|
|
|
support::ulittle32_t Type;
|
|
|
|
support::ulittle32_t Size;
|
|
|
|
char Name[16];
|
|
|
|
support::ulittle64_t VMAddress;
|
|
|
|
support::ulittle64_t VMSize;
|
|
|
|
support::ulittle64_t FileOffset;
|
|
|
|
support::ulittle64_t FileSize;
|
|
|
|
support::ulittle32_t MaxVMProtection;
|
|
|
|
support::ulittle32_t InitialVMProtection;
|
|
|
|
support::ulittle32_t NumSections;
|
|
|
|
support::ulittle32_t Flags;
|
|
|
|
};
|
2013-04-07 14:40:18 +00:00
|
|
|
|
|
|
|
struct LinkeditDataLoadCommand {
|
|
|
|
support::ulittle32_t Type;
|
|
|
|
support::ulittle32_t Size;
|
|
|
|
support::ulittle32_t DataOffset;
|
|
|
|
support::ulittle32_t DataSize;
|
|
|
|
};
|
2013-04-05 18:45:28 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 17:32:27 +00:00
|
|
|
class MachOObjectFile : public ObjectFile {
|
|
|
|
public:
|
2013-04-07 16:58:48 +00:00
|
|
|
MachOObjectFile(MemoryBuffer *Object, error_code &ec);
|
2011-10-11 17:32:27 +00:00
|
|
|
|
|
|
|
virtual symbol_iterator begin_symbols() const;
|
|
|
|
virtual symbol_iterator end_symbols() const;
|
2012-02-28 00:40:37 +00:00
|
|
|
virtual symbol_iterator begin_dynamic_symbols() const;
|
|
|
|
virtual symbol_iterator end_dynamic_symbols() const;
|
2012-03-01 01:36:50 +00:00
|
|
|
virtual library_iterator begin_libraries_needed() const;
|
|
|
|
virtual library_iterator end_libraries_needed() const;
|
2011-10-11 17:32:27 +00:00
|
|
|
virtual section_iterator begin_sections() const;
|
|
|
|
virtual section_iterator end_sections() const;
|
|
|
|
|
|
|
|
virtual uint8_t getBytesInAddress() const;
|
|
|
|
virtual StringRef getFileFormatName() const;
|
|
|
|
virtual unsigned getArch() const;
|
2012-03-01 22:19:54 +00:00
|
|
|
virtual StringRef getLoadName() const;
|
2011-10-11 17:32:27 +00:00
|
|
|
|
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
|
|
|
// In a MachO file, sections have a segment name. This is used in the .o
|
|
|
|
// files. They have a single segment, but this field specifies which segment
|
|
|
|
// a section should be put in in the final object.
|
2013-04-05 15:15:22 +00:00
|
|
|
StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
|
|
|
|
|
|
|
|
// Names are stored as 16 bytes. These returns the raw 16 bytes without
|
|
|
|
// interpreting them as a C string.
|
|
|
|
ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
|
|
|
|
ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) 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
|
|
|
|
2013-04-07 15:05:12 +00:00
|
|
|
const MachOFormat::Section64 *getSection64(DataRefImpl DRI) const;
|
|
|
|
const MachOFormat::Section *getSection(DataRefImpl DRI) const;
|
2013-04-07 15:35:18 +00:00
|
|
|
const MachOFormat::Symbol64TableEntry *
|
|
|
|
getSymbol64TableEntry(DataRefImpl DRI) const;
|
|
|
|
const MachOFormat::SymbolTableEntry *
|
|
|
|
getSymbolTableEntry(DataRefImpl DRI) const;
|
2013-04-07 15:46:05 +00:00
|
|
|
bool is64Bit() const;
|
2013-04-07 18:42:06 +00:00
|
|
|
const MachOFormat::LoadCommand *getLoadCommandInfo(unsigned Index) const;
|
2013-04-07 16:07:35 +00:00
|
|
|
void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
|
|
|
|
const macho::Header &getHeader() const;
|
2011-10-17 21:37:03 +00:00
|
|
|
|
2011-10-17 23:53:37 +00:00
|
|
|
static inline bool classof(const Binary *v) {
|
2012-03-09 20:41:57 +00:00
|
|
|
return v->isMachO();
|
2011-10-17 23:53:37 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 17:32:27 +00:00
|
|
|
protected:
|
|
|
|
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
|
|
|
|
virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
|
2011-11-29 17:40:10 +00:00
|
|
|
virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
|
2011-10-11 17:32:27 +00:00
|
|
|
virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
|
|
|
|
virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
|
|
|
|
virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
|
2012-02-28 23:47:53 +00:00
|
|
|
virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
|
2011-10-17 20:19:29 +00:00
|
|
|
virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
|
2011-10-17 23:54:46 +00:00
|
|
|
virtual error_code getSymbolSection(DataRefImpl Symb,
|
|
|
|
section_iterator &Res) const;
|
2012-10-29 10:47:00 +00:00
|
|
|
virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
|
2011-10-11 17:32:27 +00:00
|
|
|
|
|
|
|
virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
|
|
|
|
virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
|
|
|
|
virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
|
|
|
|
virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
|
|
|
|
virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
|
|
|
|
virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
|
|
|
|
virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
|
|
|
|
virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
|
|
|
|
virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
|
2012-04-12 20:13:57 +00:00
|
|
|
virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
|
|
|
|
bool &Res) const;
|
|
|
|
virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
|
|
|
|
virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
|
2012-10-10 01:41:33 +00:00
|
|
|
virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
|
2011-10-11 17:32:27 +00:00
|
|
|
virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
|
|
|
|
bool &Result) const;
|
|
|
|
virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
|
|
|
|
virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
|
|
|
|
|
|
|
|
virtual error_code getRelocationNext(DataRefImpl Rel,
|
|
|
|
RelocationRef &Res) const;
|
|
|
|
virtual error_code getRelocationAddress(DataRefImpl Rel,
|
|
|
|
uint64_t &Res) const;
|
2011-11-29 17:40:10 +00:00
|
|
|
virtual error_code getRelocationOffset(DataRefImpl Rel,
|
|
|
|
uint64_t &Res) const;
|
2011-10-11 17:32:27 +00:00
|
|
|
virtual error_code getRelocationSymbol(DataRefImpl Rel,
|
|
|
|
SymbolRef &Res) const;
|
|
|
|
virtual error_code getRelocationType(DataRefImpl Rel,
|
2011-10-26 17:08:49 +00:00
|
|
|
uint64_t &Res) const;
|
2011-10-11 17:32:27 +00:00
|
|
|
virtual error_code getRelocationTypeName(DataRefImpl Rel,
|
|
|
|
SmallVectorImpl<char> &Result) const;
|
|
|
|
virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
|
|
|
|
int64_t &Res) const;
|
|
|
|
virtual error_code getRelocationValueString(DataRefImpl Rel,
|
|
|
|
SmallVectorImpl<char> &Result) const;
|
2011-10-25 20:35:53 +00:00
|
|
|
virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
|
2011-10-11 17:32:27 +00:00
|
|
|
|
2012-03-01 01:36:50 +00:00
|
|
|
virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
|
|
|
|
virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
|
|
|
|
|
2011-10-11 17:32:27 +00:00
|
|
|
private:
|
2012-11-29 19:14:11 +00:00
|
|
|
OwningPtr<MachOObject> MachOObj;
|
2011-10-11 17:32:27 +00:00
|
|
|
typedef SmallVector<DataRefImpl, 1> SectionList;
|
|
|
|
SectionList Sections;
|
|
|
|
|
|
|
|
|
|
|
|
void moveToNextSection(DataRefImpl &DRI) const;
|
2013-04-06 03:31:08 +00:00
|
|
|
|
|
|
|
const MachOFormat::SymbolTableEntry *
|
|
|
|
getSymbolTableEntry(DataRefImpl DRI,
|
|
|
|
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const;
|
|
|
|
|
|
|
|
const MachOFormat::Symbol64TableEntry *
|
|
|
|
getSymbol64TableEntry(DataRefImpl DRI,
|
|
|
|
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const;
|
|
|
|
|
2011-10-11 17:32:27 +00:00
|
|
|
void moveToNextSymbol(DataRefImpl &DRI) const;
|
2013-04-06 01:24:11 +00:00
|
|
|
const MachOFormat::RelocationEntry *getRelocation(DataRefImpl Rel) const;
|
2011-10-11 17:32:27 +00:00
|
|
|
std::size_t getSectionIndex(DataRefImpl Sec) const;
|
2011-10-24 23:20:07 +00:00
|
|
|
|
2013-04-06 01:24:11 +00:00
|
|
|
void printRelocationTargetName(const MachOFormat::RelocationEntry *RE,
|
2011-10-26 20:42:54 +00:00
|
|
|
raw_string_ostream &fmt) const;
|
2011-10-11 17:32:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|