2009-06-02 17:52:33 +00:00
|
|
|
//=== MachOWriter.h - Target-independent Mach-O writer support --*- 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 MachOWriter class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef MACHOWRITER_H
|
|
|
|
#define MACHOWRITER_H
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2009-10-14 17:57:32 +00:00
|
|
|
#include <vector>
|
2009-06-02 17:52:33 +00:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace llvm {
|
2009-10-14 17:57:32 +00:00
|
|
|
class Constant;
|
2009-06-02 17:52:33 +00:00
|
|
|
class GlobalVariable;
|
|
|
|
class Mangler;
|
2009-10-14 17:57:32 +00:00
|
|
|
class MachineBasicBlock;
|
|
|
|
class MachineRelocation;
|
2009-06-02 17:52:33 +00:00
|
|
|
class MachOCodeEmitter;
|
2009-10-14 17:57:32 +00:00
|
|
|
struct MachODySymTab;
|
|
|
|
struct MachOHeader;
|
|
|
|
struct MachOSection;
|
|
|
|
struct MachOSym;
|
|
|
|
class TargetData;
|
|
|
|
class TargetMachine;
|
|
|
|
class MCAsmInfo;
|
|
|
|
class ObjectCodeEmitter;
|
2009-06-02 17:52:33 +00:00
|
|
|
class OutputBuffer;
|
|
|
|
class raw_ostream;
|
|
|
|
|
|
|
|
/// 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:
|
|
|
|
static char ID;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
ObjectCodeEmitter *getObjectCodeEmitter() {
|
|
|
|
return reinterpret_cast<ObjectCodeEmitter*>(MachOCE);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MachOWriter(raw_ostream &O, TargetMachine &TM);
|
|
|
|
virtual ~MachOWriter();
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Mach-O Writer";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Output stream to send the resultant object file to.
|
|
|
|
///
|
|
|
|
raw_ostream &O;
|
|
|
|
|
|
|
|
/// Target machine description.
|
|
|
|
///
|
|
|
|
TargetMachine &TM;
|
|
|
|
|
|
|
|
/// Mang - The object used to perform name mangling for this module.
|
|
|
|
///
|
|
|
|
Mangler *Mang;
|
2009-06-03 13:28:00 +00:00
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
/// MachOCE - The MachineCodeEmitter object that we are exposing to emit
|
|
|
|
/// machine code for functions to the .o file.
|
|
|
|
MachOCodeEmitter *MachOCE;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
/// is64Bit/isLittleEndian - This information is inferred from the target
|
|
|
|
/// machine directly, indicating what header values and flags to set.
|
2009-06-03 13:28:00 +00:00
|
|
|
bool is64Bit, isLittleEndian;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2009-06-03 13:28:00 +00:00
|
|
|
// Target Asm Info
|
2009-10-14 17:57:32 +00:00
|
|
|
const MCAsmInfo *MAI;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
/// Header - An instance of MachOHeader that we will update while we build
|
|
|
|
/// the file, and then emit during finalization.
|
2009-06-03 13:28:00 +00:00
|
|
|
MachOHeader Header;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2009-06-03 13:28:00 +00:00
|
|
|
/// doInitialization - Emit the file header and all of the global variables
|
|
|
|
/// for the module to the Mach-O file.
|
|
|
|
bool doInitialization(Module &M);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2009-06-03 13:28:00 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2009-06-03 13:28:00 +00:00
|
|
|
/// doFinalization - Now that the module has been completely processed, emit
|
|
|
|
/// the Mach-O file to 'O'.
|
|
|
|
bool doFinalization(Module &M);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
std::vector<MachOSection*> SectionList;
|
|
|
|
|
|
|
|
/// SectionLookup - This is a mapping from section name to SectionList entry
|
|
|
|
std::map<std::string, MachOSection*> SectionLookup;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
/// GVSection - This is a mapping from a GlobalValue to a MachOSection,
|
|
|
|
/// to aid in emitting relocations.
|
|
|
|
std::map<GlobalValue*, MachOSection*> GVSection;
|
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
/// GVOffset - This is a mapping from a GlobalValue to an offset from the
|
2009-06-02 17:52:33 +00:00
|
|
|
/// start of the section in which the GV resides, to aid in emitting
|
|
|
|
/// relocations.
|
|
|
|
std::map<GlobalValue*, intptr_t> GVOffset;
|
|
|
|
|
|
|
|
/// getSection - Return the section with the specified name, creating a new
|
|
|
|
/// section if one does not already exist.
|
|
|
|
MachOSection *getSection(const std::string &seg, const std::string §,
|
2009-10-14 17:57:32 +00:00
|
|
|
unsigned Flags = 0);
|
|
|
|
|
|
|
|
/// getTextSection - Return text section with different flags for code/data
|
|
|
|
MachOSection *getTextSection(bool isCode = true);
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
MachOSection *getDataSection() {
|
|
|
|
return getSection("__DATA", "__data");
|
|
|
|
}
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
MachOSection *getBSSSection();
|
|
|
|
MachOSection *getConstSection(Constant *C);
|
|
|
|
MachOSection *getJumpTableSection();
|
|
|
|
|
|
|
|
/// MachOSymTab - This struct contains information about the offsets and
|
2009-06-02 17:52:33 +00:00
|
|
|
/// 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
|
|
|
|
};
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
|
|
|
|
nsyms(0), stroff(0), strsize(0) { }
|
|
|
|
};
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
/// SymTab - The "stab" style symbol table information
|
2009-10-14 17:57:32 +00:00
|
|
|
MachOSymTab SymTab;
|
2009-06-02 17:52:33 +00:00
|
|
|
/// DySymTab - symbol table info for the dynamic link editor
|
|
|
|
MachODySymTab DySymTab;
|
|
|
|
|
|
|
|
protected:
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2009-06-02 17:52:33 +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;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
/// SymT - A buffer to hold the symbol table before we write it out at the
|
|
|
|
/// appropriate location in the file.
|
2009-10-14 17:57:32 +00:00
|
|
|
std::vector<unsigned char> SymT;
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
/// StrT - A buffer to hold the string table before we write it out at the
|
|
|
|
/// appropriate location in the file.
|
2009-10-14 17:57:32 +00:00
|
|
|
std::vector<unsigned char> StrT;
|
|
|
|
|
2009-06-02 17:52:33 +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.
|
|
|
|
std::vector<GlobalValue*> PendingGlobals;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
/// DynamicSymbolTable - This is just a vector of indices into
|
|
|
|
/// SymbolTable to aid in emitting the DYSYMTAB load command.
|
|
|
|
std::vector<unsigned> DynamicSymbolTable;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
static void InitMem(const Constant *C, uintptr_t Offset,
|
|
|
|
const TargetData *TD, MachOSection* mos);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
|
|
|
|
void EmitGlobal(GlobalVariable *GV);
|
|
|
|
void EmitHeaderAndLoadCommands();
|
|
|
|
void EmitSections();
|
2009-06-03 13:28:00 +00:00
|
|
|
void EmitRelocations();
|
2009-06-02 17:52:33 +00:00
|
|
|
void BufferSymbolAndStringTable();
|
|
|
|
void CalculateRelocations(MachOSection &MOS);
|
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
// GetJTRelocation - Get a relocation a new BB relocation based
|
|
|
|
// on target information.
|
2009-06-02 17:52:33 +00:00
|
|
|
MachineRelocation GetJTRelocation(unsigned Offset,
|
2009-10-14 17:57:32 +00:00
|
|
|
MachineBasicBlock *MBB) const;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
/// GetTargetRelocation - Returns the number of relocations.
|
2009-10-14 17:57:32 +00:00
|
|
|
unsigned GetTargetRelocation(MachineRelocation &MR, unsigned FromIdx,
|
|
|
|
unsigned ToAddr, unsigned ToIndex,
|
|
|
|
OutputBuffer &RelocOut, OutputBuffer &SecOut,
|
|
|
|
bool Scattered, bool Extern);
|
2009-06-02 17:52:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|