2010-12-16 16:08:33 +00:00
|
|
|
//===-- llvm/MC/MCMachObjectWriter.h - Mach Object Writer -------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_MC_MCMACHOBJECTWRITER_H
|
|
|
|
#define LLVM_MC_MCMACHOBJECTWRITER_H
|
|
|
|
|
2011-06-24 23:44:37 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
2010-12-16 16:08:33 +00:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
2014-10-06 17:05:19 +00:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2010-12-16 17:21:02 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2013-09-01 04:28:48 +00:00
|
|
|
#include "llvm/Support/MachO.h"
|
2011-06-24 23:44:37 +00:00
|
|
|
#include <vector>
|
2010-12-16 16:08:33 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2011-06-24 23:44:37 +00:00
|
|
|
class MCSectionData;
|
|
|
|
class MachObjectWriter;
|
|
|
|
|
2010-12-16 16:09:19 +00:00
|
|
|
class MCMachObjectTargetWriter {
|
2010-12-16 17:21:02 +00:00
|
|
|
const unsigned Is64Bit : 1;
|
|
|
|
const uint32_t CPUType;
|
|
|
|
const uint32_t CPUSubtype;
|
2010-12-17 05:50:29 +00:00
|
|
|
// FIXME: Remove this, we should just always use it once we no longer care
|
|
|
|
// about Darwin 'as' compatibility.
|
|
|
|
const unsigned UseAggressiveSymbolFolding : 1;
|
2010-12-21 15:26:45 +00:00
|
|
|
unsigned LocalDifference_RIT;
|
2010-12-16 17:21:02 +00:00
|
|
|
|
2010-12-16 16:09:19 +00:00
|
|
|
protected:
|
2010-12-16 17:21:02 +00:00
|
|
|
MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
|
2010-12-17 05:50:29 +00:00
|
|
|
uint32_t CPUSubtype_,
|
|
|
|
bool UseAggressiveSymbolFolding_ = false);
|
2010-12-16 16:09:19 +00:00
|
|
|
|
2010-12-21 15:26:45 +00:00
|
|
|
void setLocalDifferenceRelocationType(unsigned Type) {
|
|
|
|
LocalDifference_RIT = Type;
|
|
|
|
}
|
|
|
|
|
2010-12-16 16:09:19 +00:00
|
|
|
public:
|
|
|
|
virtual ~MCMachObjectTargetWriter();
|
2010-12-16 17:21:02 +00:00
|
|
|
|
2012-12-14 18:52:11 +00:00
|
|
|
/// @name Lifetime Management
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
virtual void reset() {};
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
2010-12-16 17:21:02 +00:00
|
|
|
/// @name Accessors
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
bool is64Bit() const { return Is64Bit; }
|
2010-12-17 05:50:29 +00:00
|
|
|
bool useAggressiveSymbolFolding() const { return UseAggressiveSymbolFolding; }
|
2010-12-16 17:21:02 +00:00
|
|
|
uint32_t getCPUType() const { return CPUType; }
|
|
|
|
uint32_t getCPUSubtype() const { return CPUSubtype; }
|
2010-12-21 15:26:45 +00:00
|
|
|
unsigned getLocalDifferenceRelocationType() const {
|
|
|
|
return LocalDifference_RIT;
|
|
|
|
}
|
2010-12-16 17:21:02 +00:00
|
|
|
|
|
|
|
/// @}
|
2011-06-24 23:44:37 +00:00
|
|
|
|
|
|
|
/// @name API
|
|
|
|
/// @{
|
|
|
|
|
2015-01-19 21:11:14 +00:00
|
|
|
virtual void RecordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
|
2011-06-24 23:44:37 +00:00
|
|
|
const MCAsmLayout &Layout,
|
|
|
|
const MCFragment *Fragment,
|
2015-01-19 21:11:14 +00:00
|
|
|
const MCFixup &Fixup, MCValue Target,
|
2011-06-24 23:44:37 +00:00
|
|
|
uint64_t &FixedValue) = 0;
|
|
|
|
|
|
|
|
/// @}
|
2010-12-16 16:09:19 +00:00
|
|
|
};
|
|
|
|
|
2011-06-24 23:44:37 +00:00
|
|
|
class MachObjectWriter : public MCObjectWriter {
|
|
|
|
/// MachSymbolData - Helper struct for containing some precomputed information
|
|
|
|
/// on symbols.
|
|
|
|
struct MachSymbolData {
|
|
|
|
MCSymbolData *SymbolData;
|
|
|
|
uint64_t StringIndex;
|
|
|
|
uint8_t SectionIndex;
|
|
|
|
|
|
|
|
// Support lexicographic sorting.
|
|
|
|
bool operator<(const MachSymbolData &RHS) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The target specific Mach-O writer instance.
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
|
2011-06-24 23:44:37 +00:00
|
|
|
|
|
|
|
/// @name Relocation Data
|
|
|
|
/// @{
|
|
|
|
|
2015-01-19 21:11:14 +00:00
|
|
|
struct RelAndSymbol {
|
|
|
|
const MCSymbolData *Sym;
|
|
|
|
MachO::any_relocation_info MRE;
|
|
|
|
RelAndSymbol(const MCSymbolData *Sym, const MachO::any_relocation_info &MRE)
|
|
|
|
: Sym(Sym), MRE(MRE) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
llvm::DenseMap<const MCSectionData *, std::vector<RelAndSymbol>> Relocations;
|
2011-06-24 23:44:37 +00:00
|
|
|
llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Symbol Table Data
|
|
|
|
/// @{
|
|
|
|
|
2014-10-06 17:05:19 +00:00
|
|
|
StringTableBuilder StringTable;
|
2011-06-24 23:44:37 +00:00
|
|
|
std::vector<MachSymbolData> LocalSymbolData;
|
|
|
|
std::vector<MachSymbolData> ExternalSymbolData;
|
|
|
|
std::vector<MachSymbolData> UndefinedSymbolData;
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
2014-05-30 13:22:59 +00:00
|
|
|
MachSymbolData *findSymbolData(const MCSymbol &Sym);
|
|
|
|
|
2011-06-24 23:44:37 +00:00
|
|
|
public:
|
2015-03-16 18:06:57 +00:00
|
|
|
MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &OS,
|
|
|
|
bool IsLittleEndian)
|
|
|
|
: MCObjectWriter(OS, IsLittleEndian), TargetObjectWriter(MOTW) {}
|
2011-06-24 23:44:37 +00:00
|
|
|
|
2012-12-14 18:52:11 +00:00
|
|
|
/// @name Lifetime management Methods
|
|
|
|
/// @{
|
|
|
|
|
2014-03-08 07:02:02 +00:00
|
|
|
void reset() override;
|
2012-12-14 18:52:11 +00:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
2011-06-24 23:44:37 +00:00
|
|
|
/// @name Utility Methods
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
|
|
|
|
|
|
|
|
SectionAddrMap SectionAddress;
|
|
|
|
|
|
|
|
SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
|
|
|
|
|
|
|
|
uint64_t getSectionAddress(const MCSectionData* SD) const {
|
|
|
|
return SectionAddress.lookup(SD);
|
|
|
|
}
|
|
|
|
uint64_t getSymbolAddress(const MCSymbolData* SD,
|
|
|
|
const MCAsmLayout &Layout) const;
|
|
|
|
|
|
|
|
uint64_t getFragmentAddress(const MCFragment *Fragment,
|
|
|
|
const MCAsmLayout &Layout) const;
|
|
|
|
|
|
|
|
uint64_t getPaddingSize(const MCSectionData *SD,
|
|
|
|
const MCAsmLayout &Layout) const;
|
|
|
|
|
|
|
|
bool doesSymbolRequireExternRelocation(const MCSymbolData *SD);
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// @name Target Writer Proxy Accessors
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
|
2014-03-11 21:22:57 +00:00
|
|
|
bool isX86_64() const {
|
|
|
|
uint32_t CPUType = TargetObjectWriter->getCPUType();
|
|
|
|
return CPUType == MachO::CPU_TYPE_X86_64;
|
|
|
|
}
|
2011-06-24 23:44:37 +00:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
|
|
|
|
bool SubsectionsViaSymbols);
|
|
|
|
|
|
|
|
/// WriteSegmentLoadCommand - Write a segment load command.
|
|
|
|
///
|
2012-09-14 14:57:36 +00:00
|
|
|
/// \param NumSections The number of sections in this segment.
|
|
|
|
/// \param SectionDataSize The total size of the sections.
|
2011-06-24 23:44:37 +00:00
|
|
|
void WriteSegmentLoadCommand(unsigned NumSections,
|
|
|
|
uint64_t VMSize,
|
|
|
|
uint64_t SectionDataStartOffset,
|
|
|
|
uint64_t SectionDataSize);
|
|
|
|
|
|
|
|
void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
|
|
|
|
const MCSectionData &SD, uint64_t FileOffset,
|
|
|
|
uint64_t RelocationsStart, unsigned NumRelocations);
|
|
|
|
|
|
|
|
void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
|
|
|
|
uint32_t StringTableOffset,
|
|
|
|
uint32_t StringTableSize);
|
|
|
|
|
|
|
|
void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
|
|
|
|
uint32_t NumLocalSymbols,
|
|
|
|
uint32_t FirstExternalSymbol,
|
|
|
|
uint32_t NumExternalSymbols,
|
|
|
|
uint32_t FirstUndefinedSymbol,
|
|
|
|
uint32_t NumUndefinedSymbols,
|
|
|
|
uint32_t IndirectSymbolOffset,
|
|
|
|
uint32_t NumIndirectSymbols);
|
|
|
|
|
|
|
|
void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
|
|
|
|
|
2012-05-18 19:12:01 +00:00
|
|
|
void WriteLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
|
|
|
|
uint32_t DataSize);
|
|
|
|
|
2013-01-18 01:26:07 +00:00
|
|
|
void WriteLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
|
|
|
|
|
2011-06-24 23:44:37 +00:00
|
|
|
// FIXME: We really need to improve the relocation validation. Basically, we
|
|
|
|
// want to implement a separate computation which evaluates the relocation
|
|
|
|
// entry as the linker would, and verifies that the resultant fixup value is
|
|
|
|
// exactly what the encoder wanted. This will catch several classes of
|
|
|
|
// problems:
|
|
|
|
//
|
|
|
|
// - Relocation entry bugs, the two algorithms are unlikely to have the same
|
|
|
|
// exact bug.
|
|
|
|
//
|
|
|
|
// - Relaxation issues, where we forget to relax something.
|
|
|
|
//
|
|
|
|
// - Input errors, where something cannot be correctly encoded. 'as' allows
|
|
|
|
// these through in many cases.
|
|
|
|
|
2015-01-19 21:11:14 +00:00
|
|
|
// Add a relocation to be output in the object file. At the time this is
|
|
|
|
// called, the symbol indexes are not know, so if the relocation refers
|
|
|
|
// to a symbol it should be passed as \p RelSymbol so that it can be updated
|
|
|
|
// afterwards. If the relocation doesn't refer to a symbol, nullptr should be
|
|
|
|
// used.
|
|
|
|
void addRelocation(const MCSymbolData *RelSymbol, const MCSectionData *SD,
|
2013-09-01 04:28:48 +00:00
|
|
|
MachO::any_relocation_info &MRE) {
|
2015-01-19 21:11:14 +00:00
|
|
|
RelAndSymbol P(RelSymbol, MRE);
|
|
|
|
Relocations[SD].push_back(P);
|
2011-06-24 23:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RecordScatteredRelocation(const MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout,
|
|
|
|
const MCFragment *Fragment,
|
|
|
|
const MCFixup &Fixup, MCValue Target,
|
|
|
|
unsigned Log2Size,
|
|
|
|
uint64_t &FixedValue);
|
|
|
|
|
|
|
|
void RecordTLVPRelocation(const MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout,
|
|
|
|
const MCFragment *Fragment,
|
|
|
|
const MCFixup &Fixup, MCValue Target,
|
|
|
|
uint64_t &FixedValue);
|
|
|
|
|
2015-01-19 21:11:14 +00:00
|
|
|
void RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
|
2011-06-24 23:44:37 +00:00
|
|
|
const MCFragment *Fragment, const MCFixup &Fixup,
|
2014-03-29 06:26:49 +00:00
|
|
|
MCValue Target, bool &IsPCRel,
|
|
|
|
uint64_t &FixedValue) override;
|
2011-06-24 23:44:37 +00:00
|
|
|
|
|
|
|
void BindIndirectSymbols(MCAssembler &Asm);
|
|
|
|
|
|
|
|
/// ComputeSymbolTable - Compute the symbol table data
|
|
|
|
///
|
2014-10-06 17:05:19 +00:00
|
|
|
void ComputeSymbolTable(MCAssembler &Asm,
|
2011-06-24 23:44:37 +00:00
|
|
|
std::vector<MachSymbolData> &LocalSymbolData,
|
|
|
|
std::vector<MachSymbolData> &ExternalSymbolData,
|
|
|
|
std::vector<MachSymbolData> &UndefinedSymbolData);
|
|
|
|
|
|
|
|
void computeSectionAddresses(const MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout);
|
|
|
|
|
2014-03-08 07:02:02 +00:00
|
|
|
void ExecutePostLayoutBinding(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout) override;
|
2011-06-24 23:44:37 +00:00
|
|
|
|
2014-03-08 07:02:02 +00:00
|
|
|
bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
|
|
|
|
const MCSymbolData &DataA,
|
2015-04-06 15:27:57 +00:00
|
|
|
const MCSymbolData *DataB,
|
2014-03-08 07:02:02 +00:00
|
|
|
const MCFragment &FB,
|
|
|
|
bool InSet,
|
|
|
|
bool IsPCRel) const override;
|
2011-06-24 23:44:37 +00:00
|
|
|
|
2014-03-08 07:02:02 +00:00
|
|
|
void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
|
2011-06-24 23:44:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-16 16:09:19 +00:00
|
|
|
/// \brief Construct a new Mach-O writer instance.
|
|
|
|
///
|
|
|
|
/// This routine takes ownership of the target writer subclass.
|
|
|
|
///
|
|
|
|
/// \param MOTW - The target specific Mach-O writer subclass.
|
|
|
|
/// \param OS - The stream to write to.
|
|
|
|
/// \returns The constructed object writer.
|
|
|
|
MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
|
2010-12-16 17:21:02 +00:00
|
|
|
raw_ostream &OS, bool IsLittleEndian);
|
2010-12-16 16:08:33 +00:00
|
|
|
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|