2017-02-08 07:02:00 +08:00
|
|
|
//===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
|
2010-07-12 06:07:02 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains an implementation of a Win32 COFF object file writer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-07-26 10:17:32 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-02-08 07:02:00 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-07-26 10:17:32 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-07-06 20:13:10 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/COFF.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MCAsmLayout.h"
|
|
|
|
#include "llvm/MC/MCAssembler.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
2017-02-08 07:02:00 +08:00
|
|
|
#include "llvm/MC/MCFixup.h"
|
|
|
|
#include "llvm/MC/MCFragment.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
|
|
|
#include "llvm/MC/MCSection.h"
|
|
|
|
#include "llvm/MC/MCSectionCOFF.h"
|
2017-02-08 07:02:00 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2015-06-09 01:17:12 +08:00
|
|
|
#include "llvm/MC/MCSymbolCOFF.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MCValue.h"
|
2017-02-08 07:02:00 +08:00
|
|
|
#include "llvm/MC/MCWinCOFFObjectWriter.h"
|
2014-09-30 06:43:20 +08:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2014-09-28 08:22:27 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2010-07-26 10:17:32 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-09-02 05:23:58 +08:00
|
|
|
#include "llvm/Support/JamCRC.h"
|
2017-02-08 07:02:00 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include <algorithm>
|
2017-02-08 07:02:00 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2015-09-02 07:46:11 +08:00
|
|
|
#include <ctime>
|
2017-02-08 07:02:00 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2010-07-12 06:07:02 +08:00
|
|
|
using namespace llvm;
|
2017-02-15 07:28:19 +08:00
|
|
|
using llvm::support::endian::write32le;
|
2010-07-12 06:07:02 +08:00
|
|
|
|
2014-04-22 11:04:17 +08:00
|
|
|
#define DEBUG_TYPE "WinCOFFObjectWriter"
|
|
|
|
|
2010-07-12 06:07:02 +08:00
|
|
|
namespace {
|
2017-02-08 07:02:00 +08:00
|
|
|
|
2017-04-27 06:31:39 +08:00
|
|
|
using name = SmallString<COFF::NameSize>;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
|
|
|
enum AuxiliaryType {
|
|
|
|
ATFunctionDefinition,
|
|
|
|
ATbfAndefSymbol,
|
|
|
|
ATWeakExternal,
|
|
|
|
ATFile,
|
|
|
|
ATSectionDefinition
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AuxSymbol {
|
2015-05-27 22:37:12 +08:00
|
|
|
AuxiliaryType AuxType;
|
2010-07-26 10:17:32 +08:00
|
|
|
COFF::Auxiliary Aux;
|
|
|
|
};
|
|
|
|
|
2010-09-27 16:58:26 +08:00
|
|
|
class COFFSection;
|
|
|
|
|
2010-07-26 10:17:32 +08:00
|
|
|
class COFFSymbol {
|
|
|
|
public:
|
2017-02-15 07:28:01 +08:00
|
|
|
COFF::symbol Data = {};
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2017-04-27 06:31:39 +08:00
|
|
|
using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2015-05-27 22:37:12 +08:00
|
|
|
name Name;
|
|
|
|
int Index;
|
2010-07-26 10:17:32 +08:00
|
|
|
AuxiliarySymbols Aux;
|
2017-02-08 07:02:00 +08:00
|
|
|
COFFSymbol *Other = nullptr;
|
|
|
|
COFFSection *Section = nullptr;
|
|
|
|
int Relocations = 0;
|
|
|
|
const MCSymbol *MC = nullptr;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2017-02-15 07:28:01 +08:00
|
|
|
COFFSymbol(StringRef Name) : Name(Name) {}
|
2017-02-08 07:02:00 +08:00
|
|
|
|
2010-07-26 10:17:32 +08:00
|
|
|
void set_name_offset(uint32_t Offset);
|
2010-09-27 16:58:26 +08:00
|
|
|
|
2015-05-30 12:56:02 +08:00
|
|
|
int64_t getIndex() const { return Index; }
|
|
|
|
void setIndex(int Value) {
|
|
|
|
Index = Value;
|
|
|
|
if (MC)
|
|
|
|
MC->setIndex(static_cast<uint32_t>(Value));
|
|
|
|
}
|
2010-07-26 10:17:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// This class contains staging data for a COFF relocation entry.
|
|
|
|
struct COFFRelocation {
|
|
|
|
COFF::relocation Data;
|
2017-02-08 07:02:00 +08:00
|
|
|
COFFSymbol *Symb = nullptr;
|
|
|
|
|
|
|
|
COFFRelocation() = default;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
|
|
|
static size_t size() { return COFF::RelocationSize; }
|
|
|
|
};
|
|
|
|
|
2017-04-27 06:31:39 +08:00
|
|
|
using relocations = std::vector<COFFRelocation>;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
|
|
|
class COFFSection {
|
|
|
|
public:
|
2017-02-15 07:28:01 +08:00
|
|
|
COFF::section Header = {};
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2015-05-27 22:37:12 +08:00
|
|
|
std::string Name;
|
|
|
|
int Number;
|
2017-02-08 07:02:00 +08:00
|
|
|
MCSectionCOFF const *MCSection = nullptr;
|
|
|
|
COFFSymbol *Symbol = nullptr;
|
2015-05-27 22:37:12 +08:00
|
|
|
relocations Relocations;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2017-02-15 07:28:01 +08:00
|
|
|
COFFSection(StringRef Name) : Name(Name) {}
|
2010-07-26 10:17:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class WinCOFFObjectWriter : public MCObjectWriter {
|
|
|
|
public:
|
2017-04-27 06:31:39 +08:00
|
|
|
using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
|
|
|
|
using sections = std::vector<std::unique_ptr<COFFSection>>;
|
2010-07-12 06:07:02 +08:00
|
|
|
|
2017-04-27 06:31:39 +08:00
|
|
|
using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
|
|
|
|
using section_map = DenseMap<MCSection const *, COFFSection *>;
|
2010-07-12 06:07:02 +08:00
|
|
|
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
|
2011-12-24 10:14:02 +08:00
|
|
|
|
2010-07-26 10:17:32 +08:00
|
|
|
// Root level file contents.
|
2017-02-15 07:28:01 +08:00
|
|
|
COFF::header Header = {};
|
2015-05-27 22:37:12 +08:00
|
|
|
sections Sections;
|
|
|
|
symbols Symbols;
|
2015-10-24 05:48:05 +08:00
|
|
|
StringTableBuilder Strings{StringTableBuilder::WinCOFF};
|
2010-07-12 06:07:02 +08:00
|
|
|
|
2010-07-26 10:17:32 +08:00
|
|
|
// Maps used during object file creation.
|
|
|
|
section_map SectionMap;
|
2015-05-27 22:37:12 +08:00
|
|
|
symbol_map SymbolMap;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2014-09-16 03:42:42 +08:00
|
|
|
bool UseBigObj;
|
|
|
|
|
2015-04-15 06:14:34 +08:00
|
|
|
WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
|
2015-04-10 02:08:15 +08:00
|
|
|
|
2014-09-17 05:31:04 +08:00
|
|
|
void reset() override {
|
|
|
|
memset(&Header, 0, sizeof(Header));
|
|
|
|
Header.Machine = TargetObjectWriter->getMachine();
|
|
|
|
Sections.clear();
|
|
|
|
Symbols.clear();
|
|
|
|
Strings.clear();
|
|
|
|
SectionMap.clear();
|
|
|
|
SymbolMap.clear();
|
|
|
|
MCObjectWriter::reset();
|
|
|
|
}
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2010-10-16 16:25:57 +08:00
|
|
|
COFFSymbol *createSymbol(StringRef Name);
|
2015-05-27 22:37:12 +08:00
|
|
|
COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
|
2010-10-16 16:25:57 +08:00
|
|
|
COFFSection *createSection(StringRef Name);
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2015-05-27 22:45:54 +08:00
|
|
|
void defineSection(MCSectionCOFF const &Sec);
|
2016-05-27 04:31:00 +08:00
|
|
|
|
|
|
|
COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
|
2015-05-21 03:34:08 +08:00
|
|
|
void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
|
2013-09-18 07:18:05 +08:00
|
|
|
const MCAsmLayout &Layout);
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2014-09-30 06:43:20 +08:00
|
|
|
void SetSymbolName(COFFSymbol &S);
|
|
|
|
void SetSectionName(COFFSection &S);
|
2010-09-27 16:58:26 +08:00
|
|
|
|
|
|
|
bool IsPhysicalSection(COFFSection *S);
|
|
|
|
|
2010-07-26 10:17:32 +08:00
|
|
|
// Entity writing methods.
|
|
|
|
|
|
|
|
void WriteFileHeader(const COFF::header &Header);
|
2014-04-15 13:25:03 +08:00
|
|
|
void WriteSymbol(const COFFSymbol &S);
|
2010-07-26 10:17:32 +08:00
|
|
|
void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
|
2017-02-18 01:32:54 +08:00
|
|
|
void writeSectionHeaders();
|
2010-07-26 10:17:32 +08:00
|
|
|
void WriteRelocation(const COFF::relocation &R);
|
2017-02-16 10:56:06 +08:00
|
|
|
uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
|
|
|
|
const MCSection &MCSec);
|
2017-02-16 10:35:48 +08:00
|
|
|
void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
|
|
|
|
const COFFSection &Sec, const MCSection &MCSec);
|
2010-07-26 10:17:32 +08:00
|
|
|
|
|
|
|
// MCObjectWriter interface implementation.
|
|
|
|
|
2015-06-05 07:25:54 +08:00
|
|
|
void executePostLayoutBinding(MCAssembler &Asm,
|
2014-03-08 15:02:02 +08:00
|
|
|
const MCAsmLayout &Layout) override;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2015-06-05 06:24:41 +08:00
|
|
|
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
|
2015-05-16 09:01:55 +08:00
|
|
|
const MCSymbol &SymA,
|
2014-11-11 16:43:57 +08:00
|
|
|
const MCFragment &FB, bool InSet,
|
|
|
|
bool IsPCRel) const override;
|
|
|
|
|
2015-06-05 06:24:41 +08:00
|
|
|
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
|
2014-03-08 15:02:02 +08:00
|
|
|
const MCFragment *Fragment, const MCFixup &Fixup,
|
2017-07-12 07:56:10 +08:00
|
|
|
MCValue Target, uint64_t &FixedValue) override;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2017-02-16 10:35:48 +08:00
|
|
|
void createFileSymbols(MCAssembler &Asm);
|
|
|
|
void assignSectionNumbers();
|
|
|
|
void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
|
|
|
|
|
2015-06-05 06:24:41 +08:00
|
|
|
void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
|
2010-07-26 10:17:32 +08:00
|
|
|
};
|
2017-02-08 07:02:00 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2010-07-26 10:17:32 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Symbol class implementation
|
|
|
|
|
|
|
|
// In the case that the name does not fit within 8 bytes, the offset
|
|
|
|
// into the string table is stored in the last 4 bytes instead, leaving
|
|
|
|
// the first 4 bytes as 0.
|
|
|
|
void COFFSymbol::set_name_offset(uint32_t Offset) {
|
2017-02-15 07:28:19 +08:00
|
|
|
write32le(Data.Name + 0, 0);
|
|
|
|
write32le(Data.Name + 4, Offset);
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// WinCOFFObjectWriter class implementation
|
|
|
|
|
2011-12-24 10:14:02 +08:00
|
|
|
WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
|
2015-04-15 06:14:34 +08:00
|
|
|
raw_pwrite_stream &OS)
|
2014-09-16 03:42:42 +08:00
|
|
|
: MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
|
2011-12-24 10:14:02 +08:00
|
|
|
Header.Machine = TargetObjectWriter->getMachine();
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
2010-10-16 16:25:57 +08:00
|
|
|
COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
|
2017-02-15 07:58:19 +08:00
|
|
|
Symbols.push_back(make_unique<COFFSymbol>(Name));
|
|
|
|
return Symbols.back().get();
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
2014-12-04 16:30:39 +08:00
|
|
|
COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
|
2017-02-15 07:47:34 +08:00
|
|
|
COFFSymbol *&Ret = SymbolMap[Symbol];
|
|
|
|
if (!Ret)
|
2017-02-15 07:58:19 +08:00
|
|
|
Ret = createSymbol(Symbol->getName());
|
2017-02-15 07:47:34 +08:00
|
|
|
return Ret;
|
2010-10-16 16:25:57 +08:00
|
|
|
}
|
|
|
|
|
2013-01-14 00:01:15 +08:00
|
|
|
COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
|
2017-02-15 07:58:19 +08:00
|
|
|
Sections.emplace_back(make_unique<COFFSection>(Name));
|
|
|
|
return Sections.back().get();
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
2017-02-15 08:15:54 +08:00
|
|
|
static uint32_t getAlignment(const MCSectionCOFF &Sec) {
|
|
|
|
switch (Sec.getAlignment()) {
|
|
|
|
case 1:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_1BYTES;
|
|
|
|
case 2:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_2BYTES;
|
|
|
|
case 4:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_4BYTES;
|
|
|
|
case 8:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_8BYTES;
|
|
|
|
case 16:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_16BYTES;
|
|
|
|
case 32:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_32BYTES;
|
|
|
|
case 64:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_64BYTES;
|
|
|
|
case 128:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_128BYTES;
|
|
|
|
case 256:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_256BYTES;
|
|
|
|
case 512:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_512BYTES;
|
|
|
|
case 1024:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_1024BYTES;
|
|
|
|
case 2048:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_2048BYTES;
|
|
|
|
case 4096:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_4096BYTES;
|
|
|
|
case 8192:
|
|
|
|
return COFF::IMAGE_SCN_ALIGN_8192BYTES;
|
|
|
|
}
|
|
|
|
llvm_unreachable("unsupported section alignment");
|
|
|
|
}
|
|
|
|
|
2010-07-26 10:17:32 +08:00
|
|
|
/// This function takes a section data object from the assembler
|
|
|
|
/// and creates the associated COFF section staging object.
|
2017-02-15 08:28:48 +08:00
|
|
|
void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec) {
|
|
|
|
COFFSection *Section = createSection(MCSec.getSectionName());
|
|
|
|
COFFSymbol *Symbol = createSymbol(MCSec.getSectionName());
|
|
|
|
Section->Symbol = Symbol;
|
|
|
|
Symbol->Section = Section;
|
|
|
|
Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
|
2017-02-15 08:15:54 +08:00
|
|
|
|
|
|
|
// Create a COMDAT symbol if needed.
|
2017-02-15 08:28:48 +08:00
|
|
|
if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
|
|
|
|
if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
|
2014-06-07 03:26:12 +08:00
|
|
|
COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
|
|
|
|
if (COMDATSymbol->Section)
|
|
|
|
report_fatal_error("two sections have the same comdat");
|
2017-02-15 08:28:48 +08:00
|
|
|
COMDATSymbol->Section = Section;
|
2014-06-07 03:26:12 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-26 10:17:32 +08:00
|
|
|
|
|
|
|
// In this case the auxiliary symbol is a Section Definition.
|
2017-02-15 08:28:48 +08:00
|
|
|
Symbol->Aux.resize(1);
|
|
|
|
Symbol->Aux[0] = {};
|
|
|
|
Symbol->Aux[0].AuxType = ATSectionDefinition;
|
|
|
|
Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2017-02-15 08:28:48 +08:00
|
|
|
// Set section alignment.
|
|
|
|
Section->Header.Characteristics = MCSec.getCharacteristics();
|
|
|
|
Section->Header.Characteristics |= getAlignment(MCSec);
|
2010-07-26 10:17:32 +08:00
|
|
|
|
|
|
|
// Bind internal COFF section to MC section.
|
2017-02-15 08:28:48 +08:00
|
|
|
Section->MCSection = &MCSec;
|
|
|
|
SectionMap[&MCSec] = Section;
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-21 03:34:08 +08:00
|
|
|
static uint64_t getSymbolValue(const MCSymbol &Symbol,
|
2014-05-01 08:10:17 +08:00
|
|
|
const MCAsmLayout &Layout) {
|
2015-05-30 05:45:01 +08:00
|
|
|
if (Symbol.isCommon() && Symbol.isExternal())
|
2015-05-30 01:48:04 +08:00
|
|
|
return Symbol.getCommonSize();
|
2014-05-01 08:10:17 +08:00
|
|
|
|
|
|
|
uint64_t Res;
|
2015-05-21 03:34:08 +08:00
|
|
|
if (!Layout.getSymbolOffset(Symbol, Res))
|
2014-05-01 08:10:17 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:31:00 +08:00
|
|
|
COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
|
|
|
|
if (!Symbol.isVariable())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const MCSymbolRefExpr *SymRef =
|
|
|
|
dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
|
|
|
|
if (!SymRef)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const MCSymbol &Aliasee = SymRef->getSymbol();
|
|
|
|
if (!Aliasee.isUndefined())
|
|
|
|
return nullptr;
|
|
|
|
return GetOrCreateCOFFSymbol(&Aliasee);
|
|
|
|
}
|
|
|
|
|
2014-04-09 06:33:40 +08:00
|
|
|
/// This function takes a symbol data object from the assembler
|
2010-07-26 10:17:32 +08:00
|
|
|
/// and creates the associated COFF symbol staging object.
|
2017-02-15 09:09:01 +08:00
|
|
|
void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
|
2013-09-18 07:18:05 +08:00
|
|
|
MCAssembler &Assembler,
|
|
|
|
const MCAsmLayout &Layout) {
|
2017-02-15 09:09:01 +08:00
|
|
|
COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
|
|
|
|
const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
|
2016-05-27 02:48:23 +08:00
|
|
|
COFFSection *Sec = nullptr;
|
|
|
|
if (Base && Base->getFragment()) {
|
|
|
|
Sec = SectionMap[Base->getFragment()->getParent()];
|
2017-02-15 09:09:01 +08:00
|
|
|
if (Sym->Section && Sym->Section != Sec)
|
2016-05-27 02:48:23 +08:00
|
|
|
report_fatal_error("conflicting sections for symbol");
|
|
|
|
}
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2016-05-27 04:31:00 +08:00
|
|
|
COFFSymbol *Local = nullptr;
|
2017-02-15 09:09:01 +08:00
|
|
|
if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
|
|
|
|
Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2017-02-15 09:09:01 +08:00
|
|
|
COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
|
2016-05-27 04:31:00 +08:00
|
|
|
if (!WeakDefault) {
|
2017-02-15 09:09:01 +08:00
|
|
|
std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
|
2016-05-27 04:31:00 +08:00
|
|
|
WeakDefault = createSymbol(WeakName);
|
2016-05-27 02:48:23 +08:00
|
|
|
if (!Sec)
|
|
|
|
WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
|
|
|
|
else
|
|
|
|
WeakDefault->Section = Sec;
|
2016-05-27 04:31:00 +08:00
|
|
|
Local = WeakDefault;
|
2010-10-16 16:25:57 +08:00
|
|
|
}
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2017-02-15 09:09:01 +08:00
|
|
|
Sym->Other = WeakDefault;
|
2016-05-27 04:31:00 +08:00
|
|
|
|
2010-07-26 10:17:32 +08:00
|
|
|
// Setup the Weak External auxiliary symbol.
|
2017-02-15 09:09:01 +08:00
|
|
|
Sym->Aux.resize(1);
|
|
|
|
memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
|
|
|
|
Sym->Aux[0].AuxType = ATWeakExternal;
|
|
|
|
Sym->Aux[0].Aux.WeakExternal.TagIndex = 0;
|
|
|
|
Sym->Aux[0].Aux.WeakExternal.Characteristics =
|
2015-05-27 22:37:12 +08:00
|
|
|
COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
|
2013-04-23 02:48:56 +08:00
|
|
|
} else {
|
2016-05-27 04:31:00 +08:00
|
|
|
if (!Base)
|
2017-02-15 09:09:01 +08:00
|
|
|
Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
|
2016-05-27 04:31:00 +08:00
|
|
|
else
|
2017-02-15 09:09:01 +08:00
|
|
|
Sym->Section = Sec;
|
|
|
|
Local = Sym;
|
2016-05-27 04:31:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Local) {
|
2017-02-15 09:09:01 +08:00
|
|
|
Local->Data.Value = getSymbolValue(MCSym, Layout);
|
2013-09-18 07:18:05 +08:00
|
|
|
|
2017-02-15 09:09:01 +08:00
|
|
|
const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
|
2016-05-27 04:31:00 +08:00
|
|
|
Local->Data.Type = SymbolCOFF.getType();
|
|
|
|
Local->Data.StorageClass = SymbolCOFF.getClass();
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2013-04-23 02:48:56 +08:00
|
|
|
// If no storage class was specified in the streamer, define it here.
|
2016-05-27 04:31:00 +08:00
|
|
|
if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
|
2017-02-15 09:09:01 +08:00
|
|
|
bool IsExternal = MCSym.isExternal() ||
|
|
|
|
(!MCSym.getFragment() && !MCSym.isVariable());
|
2010-09-27 16:58:26 +08:00
|
|
|
|
2016-05-27 04:31:00 +08:00
|
|
|
Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
|
|
|
|
: COFF::IMAGE_SYM_CLASS_STATIC;
|
2013-04-23 02:48:56 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-27 04:31:00 +08:00
|
|
|
|
2017-02-15 09:09:01 +08:00
|
|
|
Sym->MC = &MCSym;
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
2014-02-25 17:50:40 +08:00
|
|
|
// Maximum offsets for different string table entry encodings.
|
2016-01-25 04:46:11 +08:00
|
|
|
enum : unsigned { Max7DecimalOffset = 9999999U };
|
|
|
|
enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
|
2014-02-25 17:50:40 +08:00
|
|
|
|
2014-02-23 00:12:20 +08:00
|
|
|
// Encode a string table entry offset in base 64, padded to 6 chars, and
|
|
|
|
// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
|
|
|
|
// Buffer must be at least 8 bytes large. No terminating null appended.
|
2015-05-27 22:37:12 +08:00
|
|
|
static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
|
2014-02-25 17:50:40 +08:00
|
|
|
assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
|
2014-02-23 00:12:20 +08:00
|
|
|
"Illegal section name encoding for value");
|
|
|
|
|
|
|
|
static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"0123456789+/";
|
|
|
|
|
|
|
|
Buffer[0] = '/';
|
|
|
|
Buffer[1] = '/';
|
|
|
|
|
2015-05-27 22:37:12 +08:00
|
|
|
char *Ptr = Buffer + 7;
|
2014-02-23 00:12:20 +08:00
|
|
|
for (unsigned i = 0; i < 6; ++i) {
|
|
|
|
unsigned Rem = Value % 64;
|
|
|
|
Value /= 64;
|
|
|
|
*(Ptr--) = Alphabet[Rem];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 06:43:20 +08:00
|
|
|
void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
|
2017-02-15 09:09:20 +08:00
|
|
|
if (S.Name.size() <= COFF::NameSize) {
|
2010-09-27 16:58:26 +08:00
|
|
|
std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
|
2017-02-15 09:09:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t StringTableEntry = Strings.getOffset(S.Name);
|
|
|
|
if (StringTableEntry <= Max7DecimalOffset) {
|
2017-02-15 09:48:33 +08:00
|
|
|
SmallVector<char, COFF::NameSize> Buffer;
|
|
|
|
Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
|
|
|
|
assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
|
|
|
|
std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
|
2017-02-15 09:09:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (StringTableEntry <= MaxBase64Offset) {
|
|
|
|
// Starting with 10,000,000, offsets are encoded as base64.
|
|
|
|
encodeBase64StringEntry(S.Header.Name, StringTableEntry);
|
|
|
|
return;
|
2016-01-25 04:46:11 +08:00
|
|
|
}
|
2017-02-15 09:09:20 +08:00
|
|
|
report_fatal_error("COFF string table is greater than 64 GB.");
|
2010-09-27 16:58:26 +08:00
|
|
|
}
|
|
|
|
|
2014-09-30 06:43:20 +08:00
|
|
|
void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
|
|
|
|
if (S.Name.size() > COFF::NameSize)
|
|
|
|
S.set_name_offset(Strings.getOffset(S.Name));
|
|
|
|
else
|
2010-09-27 16:58:26 +08:00
|
|
|
std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
|
2015-05-27 22:37:12 +08:00
|
|
|
return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
|
|
|
|
0;
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// entity writing methods
|
|
|
|
|
|
|
|
void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
|
2014-09-16 03:42:42 +08:00
|
|
|
if (UseBigObj) {
|
2015-06-05 06:24:41 +08:00
|
|
|
writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
|
|
|
|
writeLE16(0xFFFF);
|
|
|
|
writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
|
|
|
|
writeLE16(Header.Machine);
|
|
|
|
writeLE32(Header.TimeDateStamp);
|
|
|
|
writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
|
|
|
|
writeLE32(0);
|
|
|
|
writeLE32(0);
|
|
|
|
writeLE32(0);
|
|
|
|
writeLE32(0);
|
|
|
|
writeLE32(Header.NumberOfSections);
|
|
|
|
writeLE32(Header.PointerToSymbolTable);
|
|
|
|
writeLE32(Header.NumberOfSymbols);
|
2014-09-16 03:42:42 +08:00
|
|
|
} else {
|
2015-06-05 06:24:41 +08:00
|
|
|
writeLE16(Header.Machine);
|
|
|
|
writeLE16(static_cast<int16_t>(Header.NumberOfSections));
|
|
|
|
writeLE32(Header.TimeDateStamp);
|
|
|
|
writeLE32(Header.PointerToSymbolTable);
|
|
|
|
writeLE32(Header.NumberOfSymbols);
|
|
|
|
writeLE16(Header.SizeOfOptionalHeader);
|
|
|
|
writeLE16(Header.Characteristics);
|
2014-09-16 03:42:42 +08:00
|
|
|
}
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 13:25:03 +08:00
|
|
|
void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
|
2015-06-05 06:24:41 +08:00
|
|
|
writeBytes(StringRef(S.Data.Name, COFF::NameSize));
|
|
|
|
writeLE32(S.Data.Value);
|
2014-09-16 03:42:42 +08:00
|
|
|
if (UseBigObj)
|
2015-06-05 06:24:41 +08:00
|
|
|
writeLE32(S.Data.SectionNumber);
|
2014-09-16 03:42:42 +08:00
|
|
|
else
|
2015-06-05 06:24:41 +08:00
|
|
|
writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
|
|
|
|
writeLE16(S.Data.Type);
|
|
|
|
write8(S.Data.StorageClass);
|
|
|
|
write8(S.Data.NumberOfAuxSymbols);
|
2014-04-15 13:25:03 +08:00
|
|
|
WriteAuxiliarySymbols(S.Aux);
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFObjectWriter::WriteAuxiliarySymbols(
|
2015-05-27 22:37:12 +08:00
|
|
|
const COFFSymbol::AuxiliarySymbols &S) {
|
2016-06-26 22:49:00 +08:00
|
|
|
for (const AuxSymbol &i : S) {
|
|
|
|
switch (i.AuxType) {
|
2010-07-26 10:17:32 +08:00
|
|
|
case ATFunctionDefinition:
|
2016-06-26 22:49:00 +08:00
|
|
|
writeLE32(i.Aux.FunctionDefinition.TagIndex);
|
|
|
|
writeLE32(i.Aux.FunctionDefinition.TotalSize);
|
|
|
|
writeLE32(i.Aux.FunctionDefinition.PointerToLinenumber);
|
|
|
|
writeLE32(i.Aux.FunctionDefinition.PointerToNextFunction);
|
|
|
|
WriteZeros(sizeof(i.Aux.FunctionDefinition.unused));
|
2014-09-16 03:42:42 +08:00
|
|
|
if (UseBigObj)
|
|
|
|
WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
|
2010-07-26 10:17:32 +08:00
|
|
|
break;
|
|
|
|
case ATbfAndefSymbol:
|
2016-06-26 22:49:00 +08:00
|
|
|
WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused1));
|
|
|
|
writeLE16(i.Aux.bfAndefSymbol.Linenumber);
|
|
|
|
WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused2));
|
|
|
|
writeLE32(i.Aux.bfAndefSymbol.PointerToNextFunction);
|
|
|
|
WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused3));
|
2014-09-16 03:42:42 +08:00
|
|
|
if (UseBigObj)
|
|
|
|
WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
|
2010-07-26 10:17:32 +08:00
|
|
|
break;
|
|
|
|
case ATWeakExternal:
|
2016-06-26 22:49:00 +08:00
|
|
|
writeLE32(i.Aux.WeakExternal.TagIndex);
|
|
|
|
writeLE32(i.Aux.WeakExternal.Characteristics);
|
|
|
|
WriteZeros(sizeof(i.Aux.WeakExternal.unused));
|
2014-09-16 03:42:42 +08:00
|
|
|
if (UseBigObj)
|
|
|
|
WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
|
2010-07-26 10:17:32 +08:00
|
|
|
break;
|
|
|
|
case ATFile:
|
2015-06-05 06:24:41 +08:00
|
|
|
writeBytes(
|
2016-06-26 22:49:00 +08:00
|
|
|
StringRef(reinterpret_cast<const char *>(&i.Aux),
|
2014-09-16 03:42:42 +08:00
|
|
|
UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
|
2010-07-26 10:17:32 +08:00
|
|
|
break;
|
|
|
|
case ATSectionDefinition:
|
2016-06-26 22:49:00 +08:00
|
|
|
writeLE32(i.Aux.SectionDefinition.Length);
|
|
|
|
writeLE16(i.Aux.SectionDefinition.NumberOfRelocations);
|
|
|
|
writeLE16(i.Aux.SectionDefinition.NumberOfLinenumbers);
|
|
|
|
writeLE32(i.Aux.SectionDefinition.CheckSum);
|
|
|
|
writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
|
|
|
|
write8(i.Aux.SectionDefinition.Selection);
|
|
|
|
WriteZeros(sizeof(i.Aux.SectionDefinition.unused));
|
|
|
|
writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
|
2014-09-16 03:42:42 +08:00
|
|
|
if (UseBigObj)
|
|
|
|
WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
|
2010-07-26 10:17:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 01:32:54 +08:00
|
|
|
// Write the section header.
|
|
|
|
void WinCOFFObjectWriter::writeSectionHeaders() {
|
|
|
|
// Section numbers must be monotonically increasing in the section
|
|
|
|
// header, but our Sections array is not sorted by section number,
|
|
|
|
// so make a copy of Sections and sort it.
|
|
|
|
std::vector<COFFSection *> Arr;
|
|
|
|
for (auto &Section : Sections)
|
|
|
|
Arr.push_back(Section.get());
|
|
|
|
std::sort(Arr.begin(), Arr.end(),
|
|
|
|
[](const COFFSection *A, const COFFSection *B) {
|
|
|
|
return A->Number < B->Number;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto &Section : Arr) {
|
|
|
|
if (Section->Number == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
COFF::section &S = Section->Header;
|
|
|
|
if (Section->Relocations.size() >= 0xffff)
|
|
|
|
S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
|
|
|
|
writeBytes(StringRef(S.Name, COFF::NameSize));
|
|
|
|
writeLE32(S.VirtualSize);
|
|
|
|
writeLE32(S.VirtualAddress);
|
|
|
|
writeLE32(S.SizeOfRawData);
|
|
|
|
writeLE32(S.PointerToRawData);
|
|
|
|
writeLE32(S.PointerToRelocations);
|
|
|
|
writeLE32(S.PointerToLineNumbers);
|
|
|
|
writeLE16(S.NumberOfRelocations);
|
|
|
|
writeLE16(S.NumberOfLineNumbers);
|
|
|
|
writeLE32(S.Characteristics);
|
|
|
|
}
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
|
2015-06-05 06:24:41 +08:00
|
|
|
writeLE32(R.VirtualAddress);
|
|
|
|
writeLE32(R.SymbolTableIndex);
|
|
|
|
writeLE16(R.Type);
|
2010-07-12 06:07:02 +08:00
|
|
|
}
|
|
|
|
|
2017-02-16 10:56:06 +08:00
|
|
|
// Write MCSec's contents. What this function does is essentially
|
|
|
|
// "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
|
|
|
|
// because it needs to compute a CRC.
|
|
|
|
uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout,
|
|
|
|
const MCSection &MCSec) {
|
|
|
|
// Save the contents of the section to a temporary buffer, we need this
|
|
|
|
// to CRC the data before we dump it into the object file.
|
|
|
|
SmallVector<char, 128> Buf;
|
|
|
|
raw_svector_ostream VecOS(Buf);
|
|
|
|
raw_pwrite_stream &OldStream = getStream();
|
|
|
|
|
|
|
|
// Redirect the output stream to our buffer and fill our buffer with
|
|
|
|
// the section data.
|
|
|
|
setStream(VecOS);
|
|
|
|
Asm.writeSectionData(&MCSec, Layout);
|
|
|
|
|
|
|
|
// Reset the stream back to what it was before.
|
|
|
|
setStream(OldStream);
|
|
|
|
|
|
|
|
// Write the section contents to the object file.
|
|
|
|
getStream() << Buf;
|
|
|
|
|
|
|
|
// Calculate our CRC with an initial value of '0', this is not how
|
|
|
|
// JamCRC is specified but it aligns with the expected output.
|
|
|
|
JamCRC JC(/*Init=*/0);
|
|
|
|
JC.update(Buf);
|
|
|
|
return JC.getCRC();
|
|
|
|
}
|
|
|
|
|
2017-02-16 10:35:48 +08:00
|
|
|
void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout,
|
|
|
|
const COFFSection &Sec,
|
|
|
|
const MCSection &MCSec) {
|
|
|
|
if (Sec.Number == -1)
|
|
|
|
return;
|
|
|
|
|
2017-02-16 10:56:06 +08:00
|
|
|
// Write the section contents.
|
2017-02-16 10:35:48 +08:00
|
|
|
if (Sec.Header.PointerToRawData != 0) {
|
|
|
|
assert(getStream().tell() <= Sec.Header.PointerToRawData &&
|
|
|
|
"Section::PointerToRawData is insane!");
|
|
|
|
|
2017-02-16 10:56:06 +08:00
|
|
|
unsigned PaddingSize = Sec.Header.PointerToRawData - getStream().tell();
|
|
|
|
assert(PaddingSize < 4 &&
|
2017-02-16 10:35:48 +08:00
|
|
|
"Should only need at most three bytes of padding!");
|
2017-02-16 10:56:06 +08:00
|
|
|
WriteZeros(PaddingSize);
|
2017-02-16 10:35:48 +08:00
|
|
|
|
2017-02-16 10:56:06 +08:00
|
|
|
uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
|
2017-02-16 10:35:48 +08:00
|
|
|
|
|
|
|
// Update the section definition auxiliary symbol to record the CRC.
|
|
|
|
COFFSection *Sec = SectionMap[&MCSec];
|
|
|
|
COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
|
|
|
|
assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
|
|
|
|
AuxSymbol &SecDef = AuxSyms[0];
|
2017-02-16 10:56:06 +08:00
|
|
|
SecDef.Aux.SectionDefinition.CheckSum = CRC;
|
2017-02-16 10:35:48 +08:00
|
|
|
}
|
|
|
|
|
2017-02-16 10:56:06 +08:00
|
|
|
// Write relocations for this section.
|
2017-02-16 10:35:48 +08:00
|
|
|
if (Sec.Relocations.empty()) {
|
|
|
|
assert(Sec.Header.PointerToRelocations == 0 &&
|
|
|
|
"Section::PointerToRelocations is insane!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(getStream().tell() == Sec.Header.PointerToRelocations &&
|
|
|
|
"Section::PointerToRelocations is insane!");
|
|
|
|
|
|
|
|
if (Sec.Relocations.size() >= 0xffff) {
|
|
|
|
// In case of overflow, write actual relocation count as first
|
|
|
|
// relocation. Including the synthetic reloc itself (+ 1).
|
|
|
|
COFF::relocation R;
|
|
|
|
R.VirtualAddress = Sec.Relocations.size() + 1;
|
|
|
|
R.SymbolTableIndex = 0;
|
|
|
|
R.Type = 0;
|
|
|
|
WriteRelocation(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &Relocation : Sec.Relocations)
|
|
|
|
WriteRelocation(Relocation.Data);
|
|
|
|
}
|
|
|
|
|
2010-07-12 06:07:02 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// MCObjectWriter interface implementations
|
|
|
|
|
2015-06-05 07:25:54 +08:00
|
|
|
void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
|
2010-12-07 08:27:36 +08:00
|
|
|
const MCAsmLayout &Layout) {
|
2010-07-26 10:17:32 +08:00
|
|
|
// "Define" each section & symbol. This creates section & symbol
|
2010-09-27 16:58:26 +08:00
|
|
|
// entries in the staging area.
|
2014-12-04 16:30:39 +08:00
|
|
|
for (const auto &Section : Asm)
|
2015-05-27 22:45:54 +08:00
|
|
|
defineSection(static_cast<const MCSectionCOFF &>(Section));
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2015-05-16 08:35:24 +08:00
|
|
|
for (const MCSymbol &Symbol : Asm.symbols())
|
2015-11-27 07:29:27 +08:00
|
|
|
if (!Symbol.isTemporary())
|
2015-05-21 03:34:08 +08:00
|
|
|
DefineSymbol(Symbol, Asm, Layout);
|
2010-07-12 06:07:02 +08:00
|
|
|
}
|
|
|
|
|
2015-06-05 06:24:41 +08:00
|
|
|
bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
|
2015-05-16 09:01:55 +08:00
|
|
|
const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
|
2015-04-18 05:15:17 +08:00
|
|
|
bool InSet, bool IsPCRel) const {
|
2014-11-11 16:43:57 +08:00
|
|
|
// MS LINK expects to be able to replace all references to a function with a
|
|
|
|
// thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
|
|
|
|
// away any relocations to functions.
|
2015-06-09 01:17:12 +08:00
|
|
|
uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
|
2015-12-22 06:09:27 +08:00
|
|
|
if (Asm.isIncrementalLinkerCompatible() &&
|
|
|
|
(Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
|
2014-11-11 16:43:57 +08:00
|
|
|
return false;
|
2015-06-05 06:24:41 +08:00
|
|
|
return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
|
2015-04-18 05:15:17 +08:00
|
|
|
InSet, IsPCRel);
|
2014-11-11 16:43:57 +08:00
|
|
|
}
|
|
|
|
|
2017-07-12 07:56:10 +08:00
|
|
|
void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout,
|
|
|
|
const MCFragment *Fragment,
|
|
|
|
const MCFixup &Fixup, MCValue Target,
|
|
|
|
uint64_t &FixedValue) {
|
2014-04-13 12:57:38 +08:00
|
|
|
assert(Target.getSymA() && "Relocation must reference a symbol!");
|
2010-08-25 05:04:52 +08:00
|
|
|
|
2015-11-27 07:29:27 +08:00
|
|
|
const MCSymbol &A = Target.getSymA()->getSymbol();
|
2015-11-17 18:00:43 +08:00
|
|
|
if (!A.isRegistered()) {
|
|
|
|
Asm.getContext().reportError(Fixup.getLoc(),
|
2015-05-27 22:37:12 +08:00
|
|
|
Twine("symbol '") + A.getName() +
|
|
|
|
"' can not be undefined");
|
2015-11-17 18:00:43 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-09-17 00:26:29 +08:00
|
|
|
if (A.isTemporary() && A.isUndefined()) {
|
2015-11-17 18:00:43 +08:00
|
|
|
Asm.getContext().reportError(Fixup.getLoc(),
|
2015-09-17 00:26:29 +08:00
|
|
|
Twine("assembler label '") + A.getName() +
|
|
|
|
"' can not be undefined");
|
2015-11-17 18:00:43 +08:00
|
|
|
return;
|
2015-09-17 00:26:29 +08:00
|
|
|
}
|
2014-01-31 05:13:05 +08:00
|
|
|
|
2017-02-16 09:06:45 +08:00
|
|
|
MCSection *MCSec = Fragment->getParent();
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2010-08-25 05:04:52 +08:00
|
|
|
// Mark this symbol as requiring an entry in the symbol table.
|
2017-02-16 09:06:45 +08:00
|
|
|
assert(SectionMap.find(MCSec) != SectionMap.end() &&
|
2015-06-05 07:25:54 +08:00
|
|
|
"Section must already have been defined in executePostLayoutBinding!");
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2017-02-16 09:06:45 +08:00
|
|
|
COFFSection *Sec = SectionMap[MCSec];
|
2011-04-20 22:01:45 +08:00
|
|
|
const MCSymbolRefExpr *SymB = Target.getSymB();
|
2010-08-25 05:04:52 +08:00
|
|
|
|
2014-01-06 15:39:46 +08:00
|
|
|
if (SymB) {
|
|
|
|
const MCSymbol *B = &SymB->getSymbol();
|
2015-11-17 18:00:43 +08:00
|
|
|
if (!B->getFragment()) {
|
|
|
|
Asm.getContext().reportError(
|
2014-01-06 15:39:46 +08:00
|
|
|
Fixup.getLoc(),
|
|
|
|
Twine("symbol '") + B->getName() +
|
|
|
|
"' can not be undefined in a subtraction expression");
|
2015-11-17 18:00:43 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-01-06 15:39:46 +08:00
|
|
|
|
2011-04-22 02:36:50 +08:00
|
|
|
// Offset of the symbol in the section
|
2015-05-20 07:53:20 +08:00
|
|
|
int64_t OffsetOfB = Layout.getSymbolOffset(*B);
|
2010-08-25 05:04:52 +08:00
|
|
|
|
2015-02-09 14:31:31 +08:00
|
|
|
// Offset of the relocation in the section
|
|
|
|
int64_t OffsetOfRelocation =
|
|
|
|
Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
|
|
|
|
|
2015-05-14 09:10:41 +08:00
|
|
|
FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
|
2010-08-25 05:04:52 +08:00
|
|
|
} else {
|
|
|
|
FixedValue = Target.getConstant();
|
|
|
|
}
|
2010-07-26 10:17:32 +08:00
|
|
|
|
|
|
|
COFFRelocation Reloc;
|
|
|
|
|
2010-08-01 05:08:54 +08:00
|
|
|
Reloc.Data.SymbolTableIndex = 0;
|
2010-07-26 10:17:32 +08:00
|
|
|
Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
|
2010-09-27 16:58:26 +08:00
|
|
|
|
2010-10-06 03:48:12 +08:00
|
|
|
// Turn relocations for temporary symbols into section relocations.
|
2017-06-23 05:57:04 +08:00
|
|
|
if (A.isTemporary()) {
|
2015-11-27 07:29:27 +08:00
|
|
|
MCSection *TargetSection = &A.getSection();
|
|
|
|
assert(
|
|
|
|
SectionMap.find(TargetSection) != SectionMap.end() &&
|
|
|
|
"Section must already have been defined in executePostLayoutBinding!");
|
|
|
|
Reloc.Symb = SectionMap[TargetSection]->Symbol;
|
|
|
|
FixedValue += Layout.getSymbolOffset(A);
|
|
|
|
} else {
|
|
|
|
assert(
|
|
|
|
SymbolMap.find(&A) != SymbolMap.end() &&
|
|
|
|
"Symbol must already have been defined in executePostLayoutBinding!");
|
|
|
|
Reloc.Symb = SymbolMap[&A];
|
|
|
|
}
|
2010-09-27 16:58:26 +08:00
|
|
|
|
|
|
|
++Reloc.Symb->Relocations;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
|
|
|
Reloc.Data.VirtualAddress += Fixup.getOffset();
|
2017-06-23 12:07:44 +08:00
|
|
|
Reloc.Data.Type = TargetObjectWriter->getRelocType(
|
|
|
|
Asm.getContext(), Target, Fixup, SymB, Asm.getBackend());
|
2011-12-23 06:21:47 +08:00
|
|
|
|
|
|
|
// FIXME: Can anyone explain what this does other than adjust for the size
|
|
|
|
// of the offset?
|
2014-04-14 04:47:55 +08:00
|
|
|
if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
|
|
|
|
Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
|
|
|
|
(Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
|
|
|
|
Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
|
2010-08-25 05:04:52 +08:00
|
|
|
FixedValue += 4;
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2014-04-27 11:48:22 +08:00
|
|
|
if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
|
|
|
|
switch (Reloc.Data.Type) {
|
|
|
|
case COFF::IMAGE_REL_ARM_ABSOLUTE:
|
|
|
|
case COFF::IMAGE_REL_ARM_ADDR32:
|
|
|
|
case COFF::IMAGE_REL_ARM_ADDR32NB:
|
|
|
|
case COFF::IMAGE_REL_ARM_TOKEN:
|
|
|
|
case COFF::IMAGE_REL_ARM_SECTION:
|
|
|
|
case COFF::IMAGE_REL_ARM_SECREL:
|
|
|
|
break;
|
|
|
|
case COFF::IMAGE_REL_ARM_BRANCH11:
|
|
|
|
case COFF::IMAGE_REL_ARM_BLX11:
|
2015-05-27 22:37:12 +08:00
|
|
|
// IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
|
|
|
|
// pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
|
|
|
|
// for Windows CE).
|
2014-04-27 11:48:22 +08:00
|
|
|
case COFF::IMAGE_REL_ARM_BRANCH24:
|
|
|
|
case COFF::IMAGE_REL_ARM_BLX24:
|
|
|
|
case COFF::IMAGE_REL_ARM_MOV32A:
|
|
|
|
// IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
|
|
|
|
// only used for ARM mode code, which is documented as being unsupported
|
2014-05-15 09:52:21 +08:00
|
|
|
// by Windows on ARM. Empirical proof indicates that masm is able to
|
2014-04-27 11:48:22 +08:00
|
|
|
// generate the relocations however the rest of the MSVC toolchain is
|
|
|
|
// unable to handle it.
|
|
|
|
llvm_unreachable("unsupported relocation");
|
|
|
|
break;
|
|
|
|
case COFF::IMAGE_REL_ARM_MOV32T:
|
|
|
|
break;
|
|
|
|
case COFF::IMAGE_REL_ARM_BRANCH20T:
|
|
|
|
case COFF::IMAGE_REL_ARM_BRANCH24T:
|
|
|
|
case COFF::IMAGE_REL_ARM_BLX23T:
|
|
|
|
// IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
|
|
|
|
// perform a 4 byte adjustment to the relocation. Relative branches are
|
|
|
|
// offset by 4 on ARM, however, because there is no RELA relocations, all
|
|
|
|
// branches are offset by 4.
|
|
|
|
FixedValue = FixedValue + 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 21:47:59 +08:00
|
|
|
// The fixed value never makes sense for section indices, ignore it.
|
2016-02-05 09:55:49 +08:00
|
|
|
if (Fixup.getKind() == FK_SecRel_2)
|
|
|
|
FixedValue = 0;
|
|
|
|
|
2014-05-22 07:17:50 +08:00
|
|
|
if (TargetObjectWriter->recordRelocation(Fixup))
|
2017-02-16 09:06:45 +08:00
|
|
|
Sec->Relocations.push_back(Reloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::time_t getTime() {
|
|
|
|
std::time_t Now = time(nullptr);
|
|
|
|
if (Now < 0 || !isUInt<32>(Now))
|
|
|
|
return UINT32_MAX;
|
|
|
|
return Now;
|
2010-07-12 06:07:02 +08:00
|
|
|
}
|
|
|
|
|
2017-02-16 10:35:48 +08:00
|
|
|
// Create .file symbols.
|
|
|
|
void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
|
2015-05-29 02:03:20 +08:00
|
|
|
for (const std::string &Name : Asm.getFileNames()) {
|
2014-09-16 03:42:42 +08:00
|
|
|
// round up to calculate the number of auxiliary symbols required
|
|
|
|
unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
|
2015-05-29 02:03:20 +08:00
|
|
|
unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
|
2014-09-16 03:42:42 +08:00
|
|
|
|
2017-02-16 10:35:48 +08:00
|
|
|
COFFSymbol *File = createSymbol(".file");
|
|
|
|
File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
|
|
|
|
File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
|
|
|
|
File->Aux.resize(Count);
|
2014-09-16 03:42:42 +08:00
|
|
|
|
|
|
|
unsigned Offset = 0;
|
2015-05-29 02:03:20 +08:00
|
|
|
unsigned Length = Name.size();
|
2017-02-16 10:35:48 +08:00
|
|
|
for (auto &Aux : File->Aux) {
|
2014-09-16 03:42:42 +08:00
|
|
|
Aux.AuxType = ATFile;
|
|
|
|
|
|
|
|
if (Length > SymbolSize) {
|
2015-05-29 02:03:20 +08:00
|
|
|
memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
|
2014-09-16 03:42:42 +08:00
|
|
|
Length = Length - SymbolSize;
|
|
|
|
} else {
|
2015-05-29 02:03:20 +08:00
|
|
|
memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
|
2014-09-16 03:42:42 +08:00
|
|
|
memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Offset += SymbolSize;
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 10:35:48 +08:00
|
|
|
}
|
|
|
|
|
2017-02-18 01:32:54 +08:00
|
|
|
static bool isAssociative(const COFFSection &Section) {
|
|
|
|
return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
|
|
|
|
COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
|
|
|
|
}
|
|
|
|
|
2017-02-16 10:35:48 +08:00
|
|
|
void WinCOFFObjectWriter::assignSectionNumbers() {
|
|
|
|
size_t I = 1;
|
2017-02-18 01:32:54 +08:00
|
|
|
auto Assign = [&](COFFSection &Section) {
|
|
|
|
Section.Number = I;
|
|
|
|
Section.Symbol->Data.SectionNumber = I;
|
|
|
|
Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
|
2017-02-16 10:35:48 +08:00
|
|
|
++I;
|
2017-02-18 01:32:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Although it is not explicitly requested by the Microsoft COFF spec,
|
|
|
|
// we should avoid emitting forward associative section references,
|
|
|
|
// because MSVC link.exe as of 2017 cannot handle that.
|
|
|
|
for (const std::unique_ptr<COFFSection> &Section : Sections)
|
|
|
|
if (!isAssociative(*Section))
|
|
|
|
Assign(*Section);
|
|
|
|
for (const std::unique_ptr<COFFSection> &Section : Sections)
|
|
|
|
if (isAssociative(*Section))
|
|
|
|
Assign(*Section);
|
2017-02-16 10:35:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assign file offsets to COFF object file structures.
|
|
|
|
void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout) {
|
|
|
|
unsigned Offset = getInitialOffset();
|
|
|
|
|
|
|
|
Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
|
|
|
|
Offset += COFF::SectionSize * Header.NumberOfSections;
|
|
|
|
|
|
|
|
for (const auto &Section : Asm) {
|
|
|
|
COFFSection *Sec = SectionMap[&Section];
|
|
|
|
|
|
|
|
if (Sec->Number == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
|
|
|
|
|
|
|
|
if (IsPhysicalSection(Sec)) {
|
|
|
|
// Align the section data to a four byte boundary.
|
|
|
|
Offset = alignTo(Offset, 4);
|
|
|
|
Sec->Header.PointerToRawData = Offset;
|
|
|
|
|
|
|
|
Offset += Sec->Header.SizeOfRawData;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Sec->Relocations.empty()) {
|
|
|
|
bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
|
|
|
|
|
|
|
|
if (RelocationsOverflow) {
|
|
|
|
// Signal overflow by setting NumberOfRelocations to max value. Actual
|
|
|
|
// size is found in reloc #0. Microsoft tools understand this.
|
|
|
|
Sec->Header.NumberOfRelocations = 0xffff;
|
|
|
|
} else {
|
|
|
|
Sec->Header.NumberOfRelocations = Sec->Relocations.size();
|
|
|
|
}
|
|
|
|
Sec->Header.PointerToRelocations = Offset;
|
|
|
|
|
|
|
|
if (RelocationsOverflow) {
|
|
|
|
// Reloc #0 will contain actual count, so make room for it.
|
|
|
|
Offset += COFF::RelocationSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Offset += COFF::RelocationSize * Sec->Relocations.size();
|
|
|
|
|
|
|
|
for (auto &Relocation : Sec->Relocations) {
|
|
|
|
assert(Relocation.Symb->getIndex() != -1);
|
|
|
|
Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(Sec->Symbol->Aux.size() == 1 &&
|
|
|
|
"Section's symbol must have one aux!");
|
|
|
|
AuxSymbol &Aux = Sec->Symbol->Aux[0];
|
|
|
|
assert(Aux.AuxType == ATSectionDefinition &&
|
|
|
|
"Section's symbol's aux symbol must be a Section Definition!");
|
|
|
|
Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
|
|
|
|
Aux.Aux.SectionDefinition.NumberOfRelocations =
|
|
|
|
Sec->Header.NumberOfRelocations;
|
|
|
|
Aux.Aux.SectionDefinition.NumberOfLinenumbers =
|
|
|
|
Sec->Header.NumberOfLineNumbers;
|
|
|
|
}
|
|
|
|
|
|
|
|
Header.PointerToSymbolTable = Offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout) {
|
|
|
|
if (Sections.size() > INT32_MAX)
|
|
|
|
report_fatal_error(
|
|
|
|
"PE COFF object files can't have more than 2147483647 sections");
|
|
|
|
|
|
|
|
UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
|
|
|
|
Header.NumberOfSections = Sections.size();
|
|
|
|
Header.NumberOfSymbols = 0;
|
|
|
|
|
|
|
|
assignSectionNumbers();
|
|
|
|
createFileSymbols(Asm);
|
2014-09-16 03:42:42 +08:00
|
|
|
|
2014-08-28 12:02:50 +08:00
|
|
|
for (auto &Symbol : Symbols) {
|
2010-07-26 10:17:32 +08:00
|
|
|
// Update section number & offset for symbols that have them.
|
2014-05-01 21:37:57 +08:00
|
|
|
if (Symbol->Section)
|
2014-04-28 11:34:48 +08:00
|
|
|
Symbol->Data.SectionNumber = Symbol->Section->Number;
|
2015-11-27 07:29:27 +08:00
|
|
|
Symbol->setIndex(Header.NumberOfSymbols++);
|
|
|
|
// Update auxiliary symbol info.
|
|
|
|
Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
|
|
|
|
Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
2014-09-30 06:43:20 +08:00
|
|
|
// Build string table.
|
|
|
|
for (const auto &S : Sections)
|
|
|
|
if (S->Name.size() > COFF::NameSize)
|
|
|
|
Strings.add(S->Name);
|
|
|
|
for (const auto &S : Symbols)
|
2015-11-27 07:29:27 +08:00
|
|
|
if (S->Name.size() > COFF::NameSize)
|
2014-09-30 06:43:20 +08:00
|
|
|
Strings.add(S->Name);
|
2015-10-24 05:48:05 +08:00
|
|
|
Strings.finalize();
|
2014-09-30 06:43:20 +08:00
|
|
|
|
|
|
|
// Set names.
|
|
|
|
for (const auto &S : Sections)
|
|
|
|
SetSectionName(*S);
|
|
|
|
for (auto &S : Symbols)
|
2015-11-27 07:29:27 +08:00
|
|
|
SetSymbolName(*S);
|
2014-09-30 06:43:20 +08:00
|
|
|
|
2010-07-26 10:17:32 +08:00
|
|
|
// Fixup weak external references.
|
2014-12-04 16:30:39 +08:00
|
|
|
for (auto &Symbol : Symbols) {
|
2014-04-28 11:34:48 +08:00
|
|
|
if (Symbol->Other) {
|
2015-05-30 12:56:02 +08:00
|
|
|
assert(Symbol->getIndex() != -1);
|
2014-04-28 11:34:48 +08:00
|
|
|
assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
|
|
|
|
assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
|
2010-07-26 10:17:32 +08:00
|
|
|
"Symbol's aux symbol must be a Weak External!");
|
2015-05-30 12:56:02 +08:00
|
|
|
Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-06 20:13:10 +08:00
|
|
|
// Fixup associative COMDAT sections.
|
2014-12-04 16:30:39 +08:00
|
|
|
for (auto &Section : Sections) {
|
2014-04-28 11:34:48 +08:00
|
|
|
if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
|
2013-07-06 20:13:10 +08:00
|
|
|
COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
|
|
|
|
continue;
|
|
|
|
|
2015-05-27 22:45:54 +08:00
|
|
|
const MCSectionCOFF &MCSec = *Section->MCSection;
|
2013-07-06 20:13:10 +08:00
|
|
|
|
2014-06-07 03:26:12 +08:00
|
|
|
const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
|
|
|
|
assert(COMDAT);
|
|
|
|
COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
|
|
|
|
assert(COMDATSymbol);
|
|
|
|
COFFSection *Assoc = COMDATSymbol->Section;
|
2014-04-28 11:34:48 +08:00
|
|
|
if (!Assoc)
|
2014-06-07 03:26:12 +08:00
|
|
|
report_fatal_error(
|
|
|
|
Twine("Missing associated COMDAT section for section ") +
|
|
|
|
MCSec.getSectionName());
|
2013-07-06 20:13:10 +08:00
|
|
|
|
|
|
|
// Skip this section if the associated section is unused.
|
|
|
|
if (Assoc->Number == -1)
|
|
|
|
continue;
|
|
|
|
|
2015-05-30 12:56:02 +08:00
|
|
|
Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
|
2013-07-06 20:13:10 +08:00
|
|
|
}
|
|
|
|
|
2017-02-16 10:35:48 +08:00
|
|
|
assignFileOffsets(Asm, Layout);
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2015-09-02 07:46:11 +08:00
|
|
|
// MS LINK expects to be able to use this timestamp to implement their
|
|
|
|
// /INCREMENTAL feature.
|
2015-12-22 06:09:27 +08:00
|
|
|
if (Asm.isIncrementalLinkerCompatible()) {
|
2017-02-16 09:06:45 +08:00
|
|
|
Header.TimeDateStamp = getTime();
|
2015-12-22 06:09:27 +08:00
|
|
|
} else {
|
2016-01-07 03:05:19 +08:00
|
|
|
// Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
|
2015-12-22 06:09:27 +08:00
|
|
|
Header.TimeDateStamp = 0;
|
|
|
|
}
|
2010-08-03 12:43:33 +08:00
|
|
|
|
2010-07-26 10:17:32 +08:00
|
|
|
// Write it all to disk...
|
|
|
|
WriteFileHeader(Header);
|
2017-02-18 01:32:54 +08:00
|
|
|
writeSectionHeaders();
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2017-02-16 10:35:48 +08:00
|
|
|
// Write section contents.
|
|
|
|
sections::iterator I = Sections.begin();
|
|
|
|
sections::iterator IE = Sections.end();
|
|
|
|
MCAssembler::iterator J = Asm.begin();
|
|
|
|
MCAssembler::iterator JE = Asm.end();
|
|
|
|
for (; I != IE && J != JE; ++I, ++J)
|
|
|
|
writeSection(Asm, Layout, **I, *J);
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2015-09-02 00:19:03 +08:00
|
|
|
assert(getStream().tell() == Header.PointerToSymbolTable &&
|
2010-07-26 10:17:32 +08:00
|
|
|
"Header::PointerToSymbolTable is insane!");
|
|
|
|
|
2017-02-16 10:35:48 +08:00
|
|
|
// Write a symbol table.
|
2014-12-04 16:30:39 +08:00
|
|
|
for (auto &Symbol : Symbols)
|
2015-05-30 12:56:02 +08:00
|
|
|
if (Symbol->getIndex() != -1)
|
2014-04-28 11:34:48 +08:00
|
|
|
WriteSymbol(*Symbol);
|
2010-07-26 10:17:32 +08:00
|
|
|
|
2017-02-16 10:35:48 +08:00
|
|
|
// Write a string table, which completes the entire COFF file.
|
2016-10-05 06:43:25 +08:00
|
|
|
Strings.write(getStream());
|
2010-07-12 06:07:02 +08:00
|
|
|
}
|
|
|
|
|
2015-05-27 22:37:12 +08:00
|
|
|
MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
|
|
|
|
: Machine(Machine_) {}
|
2011-12-24 10:14:02 +08:00
|
|
|
|
2013-11-19 08:57:56 +08:00
|
|
|
// Pin the vtable to this file.
|
|
|
|
void MCWinCOFFObjectTargetWriter::anchor() {}
|
|
|
|
|
2010-07-12 06:07:02 +08:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// WinCOFFObjectWriter factory function
|
|
|
|
|
2015-04-10 02:08:15 +08:00
|
|
|
MCObjectWriter *
|
|
|
|
llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
|
2015-04-15 06:14:34 +08:00
|
|
|
raw_pwrite_stream &OS) {
|
2015-04-10 02:08:15 +08:00
|
|
|
return new WinCOFFObjectWriter(MOTW, OS);
|
2010-07-26 11:01:28 +08:00
|
|
|
}
|