2006-08-23 21:08:52 +00:00
|
|
|
//=== MachOWriter.h - Target-independent Mach-O writer support --*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-08-23 21:08:52 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the MachOWriter class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-02-08 01:30:50 +00:00
|
|
|
#ifndef MACHOWRITER_H
|
|
|
|
#define MACHOWRITER_H
|
2006-08-23 21:08:52 +00:00
|
|
|
|
2009-06-03 03:43:31 +00:00
|
|
|
#include "MachO.h"
|
2006-08-23 21:08:52 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2006-08-25 06:36:58 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2007-01-24 03:37:18 +00:00
|
|
|
#include "llvm/Target/TargetMachOWriterInfo.h"
|
2008-03-21 23:51:57 +00:00
|
|
|
#include <map>
|
2006-08-23 21:08:52 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class GlobalVariable;
|
|
|
|
class Mangler;
|
|
|
|
class MachineCodeEmitter;
|
|
|
|
class MachOCodeEmitter;
|
2007-02-03 02:37:51 +00:00
|
|
|
class OutputBuffer;
|
2008-08-21 00:14:44 +00:00
|
|
|
class raw_ostream;
|
2006-08-23 21:08:52 +00:00
|
|
|
|
2006-09-08 22:42:09 +00:00
|
|
|
|
2006-08-23 21:08:52 +00:00
|
|
|
/// MachOWriter - This class implements the common target-independent code for
|
|
|
|
/// writing Mach-O files. Targets should derive a class from this to
|
|
|
|
/// parameterize the output format.
|
|
|
|
///
|
|
|
|
class MachOWriter : public MachineFunctionPass {
|
|
|
|
friend class MachOCodeEmitter;
|
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2006-08-23 21:08:52 +00:00
|
|
|
MachineCodeEmitter &getMachineCodeEmitter() const {
|
|
|
|
return *(MachineCodeEmitter*)MCE;
|
|
|
|
}
|
2007-02-08 01:30:50 +00:00
|
|
|
|
2008-08-21 00:14:44 +00:00
|
|
|
MachOWriter(raw_ostream &O, TargetMachine &TM);
|
2007-01-24 07:13:56 +00:00
|
|
|
virtual ~MachOWriter();
|
2006-08-23 21:08:52 +00:00
|
|
|
|
2007-01-24 07:13:56 +00:00
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Mach-O Writer";
|
|
|
|
}
|
2006-08-23 21:08:52 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Output stream to send the resultant object file to.
|
|
|
|
///
|
2008-08-21 00:14:44 +00:00
|
|
|
raw_ostream &O;
|
2006-08-23 21:08:52 +00:00
|
|
|
|
|
|
|
/// Target machine description.
|
|
|
|
///
|
|
|
|
TargetMachine &TM;
|
|
|
|
|
|
|
|
/// Mang - The object used to perform name mangling for this module.
|
|
|
|
///
|
|
|
|
Mangler *Mang;
|
2006-09-08 22:42:09 +00:00
|
|
|
|
2006-08-23 21:08:52 +00:00
|
|
|
/// MCE - The MachineCodeEmitter object that we are exposing to emit machine
|
|
|
|
/// code for functions to the .o file.
|
2009-06-03 03:43:31 +00:00
|
|
|
|
2006-08-23 21:08:52 +00:00
|
|
|
MachOCodeEmitter *MCE;
|
|
|
|
|
|
|
|
/// is64Bit/isLittleEndian - This information is inferred from the target
|
|
|
|
/// machine directly, indicating what header values and flags to set.
|
|
|
|
|
2009-06-03 03:43:31 +00:00
|
|
|
bool is64Bit, isLittleEndian;
|
2006-08-23 21:08:52 +00:00
|
|
|
|
2009-06-03 03:43:31 +00:00
|
|
|
// Target Asm Info
|
2006-08-23 21:08:52 +00:00
|
|
|
|
2009-06-03 03:43:31 +00:00
|
|
|
const TargetAsmInfo *TAI;
|
2006-08-23 21:08:52 +00:00
|
|
|
|
|
|
|
/// Header - An instance of MachOHeader that we will update while we build
|
|
|
|
/// the file, and then emit during finalization.
|
|
|
|
|
2009-06-03 03:43:31 +00:00
|
|
|
MachOHeader Header;
|
2006-08-23 21:08:52 +00:00
|
|
|
|
2009-06-03 03:43:31 +00:00
|
|
|
/// doInitialization - Emit the file header and all of the global variables
|
|
|
|
/// for the module to the Mach-O file.
|
2006-08-23 21:08:52 +00:00
|
|
|
|
2009-06-03 03:43:31 +00:00
|
|
|
bool doInitialization(Module &M);
|
2006-09-10 23:03:44 +00:00
|
|
|
|
2009-06-03 03:43:31 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF);
|
2006-08-23 21:08:52 +00:00
|
|
|
|
2009-06-03 03:43:31 +00:00
|
|
|
/// doFinalization - Now that the module has been completely processed, emit
|
|
|
|
/// the Mach-O file to 'O'.
|
2006-08-23 21:08:52 +00:00
|
|
|
|
2009-06-03 03:43:31 +00:00
|
|
|
bool doFinalization(Module &M);
|
2006-08-23 21:08:52 +00:00
|
|
|
|
2006-08-26 15:46:34 +00:00
|
|
|
private:
|
|
|
|
|
2006-08-23 21:08:52 +00:00
|
|
|
/// SectionList - This is the list of sections that we have emitted to the
|
|
|
|
/// file. Once the file has been completely built, the segment load command
|
|
|
|
/// SectionCommands are constructed from this info.
|
2009-06-03 03:43:31 +00:00
|
|
|
|
2006-12-11 02:20:45 +00:00
|
|
|
std::vector<MachOSection*> SectionList;
|
2006-08-23 21:08:52 +00:00
|
|
|
|
|
|
|
/// SectionLookup - This is a mapping from section name to SectionList entry
|
2009-06-03 03:43:31 +00:00
|
|
|
|
2006-08-23 21:08:52 +00:00
|
|
|
std::map<std::string, MachOSection*> SectionLookup;
|
2006-12-11 02:20:45 +00:00
|
|
|
|
|
|
|
/// GVSection - This is a mapping from a GlobalValue to a MachOSection,
|
|
|
|
/// to aid in emitting relocations.
|
2009-06-03 03:43:31 +00:00
|
|
|
|
2006-12-11 02:20:45 +00:00
|
|
|
std::map<GlobalValue*, MachOSection*> GVSection;
|
|
|
|
|
|
|
|
/// GVOffset - This is a mapping from a GlobalValue to an offset from the
|
|
|
|
/// start of the section in which the GV resides, to aid in emitting
|
|
|
|
/// relocations.
|
2009-06-03 03:43:31 +00:00
|
|
|
|
2006-12-11 02:20:45 +00:00
|
|
|
std::map<GlobalValue*, intptr_t> GVOffset;
|
2006-08-23 21:08:52 +00:00
|
|
|
|
|
|
|
/// getSection - Return the section with the specified name, creating a new
|
|
|
|
/// section if one does not already exist.
|
2009-06-03 03:43:31 +00:00
|
|
|
|
2006-12-11 02:20:45 +00:00
|
|
|
MachOSection *getSection(const std::string &seg, const std::string §,
|
2006-08-23 21:08:52 +00:00
|
|
|
unsigned Flags = 0) {
|
2006-12-11 02:20:45 +00:00
|
|
|
MachOSection *MOS = SectionLookup[seg+sect];
|
|
|
|
if (MOS) return MOS;
|
|
|
|
|
|
|
|
MOS = new MachOSection(seg, sect);
|
|
|
|
SectionList.push_back(MOS);
|
|
|
|
MOS->Index = SectionList.size();
|
|
|
|
MOS->flags = MachOSection::S_REGULAR | Flags;
|
|
|
|
SectionLookup[seg+sect] = MOS;
|
|
|
|
return MOS;
|
2006-08-23 21:08:52 +00:00
|
|
|
}
|
2006-12-11 02:20:45 +00:00
|
|
|
MachOSection *getTextSection(bool isCode = true) {
|
2006-09-10 23:03:44 +00:00
|
|
|
if (isCode)
|
|
|
|
return getSection("__TEXT", "__text",
|
|
|
|
MachOSection::S_ATTR_PURE_INSTRUCTIONS |
|
|
|
|
MachOSection::S_ATTR_SOME_INSTRUCTIONS);
|
|
|
|
else
|
|
|
|
return getSection("__TEXT", "__text");
|
2006-08-23 21:08:52 +00:00
|
|
|
}
|
2006-12-11 02:20:45 +00:00
|
|
|
MachOSection *getBSSSection() {
|
2006-08-25 06:36:58 +00:00
|
|
|
return getSection("__DATA", "__bss", MachOSection::S_ZEROFILL);
|
|
|
|
}
|
2006-12-11 02:20:45 +00:00
|
|
|
MachOSection *getDataSection() {
|
2006-08-25 06:36:58 +00:00
|
|
|
return getSection("__DATA", "__data");
|
|
|
|
}
|
2007-01-29 21:20:42 +00:00
|
|
|
MachOSection *getConstSection(Constant *C) {
|
|
|
|
const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
|
|
|
|
if (CVA && CVA->isCString())
|
|
|
|
return getSection("__TEXT", "__cstring",
|
|
|
|
MachOSection::S_CSTRING_LITERALS);
|
|
|
|
|
|
|
|
const Type *Ty = C->getType();
|
2007-01-15 02:27:26 +00:00
|
|
|
if (Ty->isPrimitiveType() || Ty->isInteger()) {
|
2009-05-09 07:06:46 +00:00
|
|
|
unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
|
2006-08-25 06:36:58 +00:00
|
|
|
switch(Size) {
|
|
|
|
default: break; // Fall through to __TEXT,__const
|
|
|
|
case 4:
|
|
|
|
return getSection("__TEXT", "__literal4",
|
|
|
|
MachOSection::S_4BYTE_LITERALS);
|
|
|
|
case 8:
|
|
|
|
return getSection("__TEXT", "__literal8",
|
|
|
|
MachOSection::S_8BYTE_LITERALS);
|
|
|
|
case 16:
|
|
|
|
return getSection("__TEXT", "__literal16",
|
|
|
|
MachOSection::S_16BYTE_LITERALS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return getSection("__TEXT", "__const");
|
|
|
|
}
|
2006-12-11 02:20:45 +00:00
|
|
|
MachOSection *getJumpTableSection() {
|
2006-09-10 23:03:44 +00:00
|
|
|
if (TM.getRelocationModel() == Reloc::PIC_)
|
|
|
|
return getTextSection(false);
|
|
|
|
else
|
|
|
|
return getSection("__TEXT", "__const");
|
|
|
|
}
|
2006-08-23 21:08:52 +00:00
|
|
|
|
|
|
|
/// MachOSymTab - This struct contains information about the offsets and
|
|
|
|
/// size of symbol table information.
|
|
|
|
/// segment.
|
|
|
|
struct MachOSymTab {
|
|
|
|
uint32_t cmd; // LC_SYMTAB
|
|
|
|
uint32_t cmdsize; // sizeof( MachOSymTab )
|
|
|
|
uint32_t symoff; // symbol table offset
|
|
|
|
uint32_t nsyms; // number of symbol table entries
|
|
|
|
uint32_t stroff; // string table offset
|
|
|
|
uint32_t strsize; // string table size in bytes
|
|
|
|
|
|
|
|
// Constants for the cmd field
|
|
|
|
// see <mach-o/loader.h>
|
|
|
|
enum { LC_SYMTAB = 0x02 // link-edit stab symbol table info
|
|
|
|
};
|
|
|
|
|
|
|
|
MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
|
|
|
|
nsyms(0), stroff(0), strsize(0) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// SymTab - The "stab" style symbol table information
|
|
|
|
MachOSymTab SymTab;
|
|
|
|
/// DySymTab - symbol table info for the dynamic link editor
|
|
|
|
MachODySymTab DySymTab;
|
|
|
|
|
2006-09-08 22:42:09 +00:00
|
|
|
protected:
|
|
|
|
|
2006-08-23 21:08:52 +00:00
|
|
|
/// SymbolTable - This is the list of symbols we have emitted to the file.
|
|
|
|
/// This actually gets rearranged before emission to the file (to put the
|
|
|
|
/// local symbols first in the list).
|
|
|
|
std::vector<MachOSym> SymbolTable;
|
|
|
|
|
2006-08-26 15:46:34 +00:00
|
|
|
/// SymT - A buffer to hold the symbol table before we write it out at the
|
|
|
|
/// appropriate location in the file.
|
|
|
|
DataBuffer SymT;
|
|
|
|
|
|
|
|
/// StrT - A buffer to hold the string table before we write it out at the
|
|
|
|
/// appropriate location in the file.
|
|
|
|
DataBuffer StrT;
|
|
|
|
|
2006-08-25 06:36:58 +00:00
|
|
|
/// PendingSyms - This is a list of externally defined symbols that we have
|
|
|
|
/// been asked to emit, but have not seen a reference to. When a reference
|
|
|
|
/// is seen, the symbol will move from this list to the SymbolTable.
|
2007-02-28 07:40:50 +00:00
|
|
|
std::vector<GlobalValue*> PendingGlobals;
|
2006-08-25 06:36:58 +00:00
|
|
|
|
2006-08-23 21:08:52 +00:00
|
|
|
/// DynamicSymbolTable - This is just a vector of indices into
|
|
|
|
/// SymbolTable to aid in emitting the DYSYMTAB load command.
|
|
|
|
std::vector<unsigned> DynamicSymbolTable;
|
|
|
|
|
2006-12-11 02:20:45 +00:00
|
|
|
static void InitMem(const Constant *C, void *Addr, intptr_t Offset,
|
|
|
|
const TargetData *TD,
|
|
|
|
std::vector<MachineRelocation> &MRs);
|
2006-09-10 23:03:44 +00:00
|
|
|
|
2006-08-23 21:08:52 +00:00
|
|
|
private:
|
2006-12-11 02:20:45 +00:00
|
|
|
void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
|
2006-08-23 21:08:52 +00:00
|
|
|
void EmitGlobal(GlobalVariable *GV);
|
|
|
|
void EmitHeaderAndLoadCommands();
|
|
|
|
void EmitSections();
|
2009-06-03 03:43:31 +00:00
|
|
|
void EmitRelocations();
|
2006-08-26 15:46:34 +00:00
|
|
|
void BufferSymbolAndStringTable();
|
2006-12-11 02:20:45 +00:00
|
|
|
void CalculateRelocations(MachOSection &MOS);
|
2006-09-08 22:42:09 +00:00
|
|
|
|
2007-01-24 03:37:18 +00:00
|
|
|
MachineRelocation GetJTRelocation(unsigned Offset,
|
|
|
|
MachineBasicBlock *MBB) const {
|
|
|
|
return TM.getMachOWriterInfo()->GetJTRelocation(Offset, MBB);
|
|
|
|
}
|
2007-02-03 02:37:51 +00:00
|
|
|
|
|
|
|
/// GetTargetRelocation - Returns the number of relocations.
|
|
|
|
unsigned GetTargetRelocation(MachineRelocation &MR,
|
|
|
|
unsigned FromIdx,
|
|
|
|
unsigned ToAddr,
|
|
|
|
unsigned ToIndex,
|
|
|
|
OutputBuffer &RelocOut,
|
|
|
|
OutputBuffer &SecOut,
|
2007-02-28 07:40:50 +00:00
|
|
|
bool Scattered,
|
|
|
|
bool Extern) {
|
2007-02-03 02:37:51 +00:00
|
|
|
return TM.getMachOWriterInfo()->GetTargetRelocation(MR, FromIdx, ToAddr,
|
|
|
|
ToIndex, RelocOut,
|
2007-02-28 07:40:50 +00:00
|
|
|
SecOut, Scattered,
|
|
|
|
Extern);
|
2007-02-03 02:37:51 +00:00
|
|
|
}
|
2006-08-23 21:08:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|