2017-02-08 07:02:00 +08:00
|
|
|
//===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
|
2010-07-12 06:07:02 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-07-12 06:07:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
2020-03-11 05:44:03 +08:00
|
|
|
#include "llvm/ADT/DenseSet.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"
|
2019-10-09 17:06:30 +08:00
|
|
|
#include "llvm/Support/CRC.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2020-04-17 02:31:45 +08:00
|
|
|
#include "llvm/Support/EndianStream.h"
|
2010-07-26 10:17:32 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2018-08-23 07:58:16 +08:00
|
|
|
#include "llvm/Support/LEB128.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 {
|
|
|
|
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
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
COFFSection(StringRef Name) : Name(std::string(Name)) {}
|
2010-07-26 10:17:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class WinCOFFObjectWriter : public MCObjectWriter {
|
|
|
|
public:
|
2018-05-22 02:17:42 +08:00
|
|
|
support::endian::Writer W;
|
|
|
|
|
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
|
|
|
|
2020-03-11 05:44:03 +08:00
|
|
|
using symbol_list = DenseSet<COFFSymbol *>;
|
|
|
|
|
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
|
|
|
|
2020-03-11 05:44:03 +08:00
|
|
|
symbol_list WeakDefaults;
|
|
|
|
|
2014-09-16 03:42:42 +08:00
|
|
|
bool UseBigObj;
|
|
|
|
|
2018-08-23 07:58:16 +08:00
|
|
|
bool EmitAddrsigSection = false;
|
|
|
|
MCSectionCOFF *AddrsigSection;
|
|
|
|
std::vector<const MCSymbol *> AddrsigSyms;
|
|
|
|
|
2017-10-10 08:50:29 +08:00
|
|
|
WinCOFFObjectWriter(std::unique_ptr<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);
|
2020-03-11 05:44:03 +08:00
|
|
|
void setWeakDefaultNames();
|
2017-02-16 10:35:48 +08:00
|
|
|
void assignSectionNumbers();
|
|
|
|
void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
|
|
|
|
|
2018-08-23 07:58:16 +08:00
|
|
|
void emitAddrsigSection() override { EmitAddrsigSection = true; }
|
|
|
|
void addAddrsigSymbol(const MCSymbol *Sym) override {
|
|
|
|
AddrsigSyms.push_back(Sym);
|
|
|
|
}
|
|
|
|
|
2018-05-22 02:23:50 +08:00
|
|
|
uint64_t 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
|
|
|
|
|
2017-10-10 08:50:29 +08:00
|
|
|
WinCOFFObjectWriter::WinCOFFObjectWriter(
|
|
|
|
std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
|
2018-05-22 02:28:57 +08:00
|
|
|
: W(OS, support::little), TargetObjectWriter(std::move(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) {
|
2019-08-15 23:54:37 +08:00
|
|
|
Symbols.push_back(std::make_unique<COFFSymbol>(Name));
|
2017-02-15 07:58:19 +08:00
|
|
|
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) {
|
2019-08-15 23:54:37 +08:00
|
|
|
Sections.emplace_back(std::make_unique<COFFSection>(Name));
|
2017-02-15 07:58:19 +08:00
|
|
|
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) {
|
2020-04-16 06:49:05 +08:00
|
|
|
COFFSection *Section = createSection(MCSec.getName());
|
|
|
|
COFFSymbol *Symbol = createSymbol(MCSec.getName());
|
2017-02-15 08:28:48 +08:00
|
|
|
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.
|
2019-12-29 05:38:41 +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;
|
2019-12-29 05:38:41 +08:00
|
|
|
if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
|
2017-02-15 09:09:01 +08:00
|
|
|
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;
|
2020-03-11 05:44:03 +08:00
|
|
|
WeakDefaults.insert(WeakDefault);
|
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 =
|
2019-06-26 09:09:52 +08:00
|
|
|
COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS;
|
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;
|
2019-12-29 05:38:41 +08:00
|
|
|
}
|
2016-05-27 04:31:00 +08:00
|
|
|
|
2019-12-29 05:38:41 +08:00
|
|
|
if (Local) {
|
|
|
|
Local->Data.Value = getSymbolValue(MCSym, Layout);
|
|
|
|
|
|
|
|
const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
|
|
|
|
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) {
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
|
|
|
|
W.write<uint16_t>(0xFFFF);
|
|
|
|
W.write<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion);
|
|
|
|
W.write<uint16_t>(Header.Machine);
|
|
|
|
W.write<uint32_t>(Header.TimeDateStamp);
|
|
|
|
W.OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
|
|
|
|
W.write<uint32_t>(0);
|
|
|
|
W.write<uint32_t>(0);
|
|
|
|
W.write<uint32_t>(0);
|
|
|
|
W.write<uint32_t>(0);
|
|
|
|
W.write<uint32_t>(Header.NumberOfSections);
|
|
|
|
W.write<uint32_t>(Header.PointerToSymbolTable);
|
|
|
|
W.write<uint32_t>(Header.NumberOfSymbols);
|
2014-09-16 03:42:42 +08:00
|
|
|
} else {
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint16_t>(Header.Machine);
|
|
|
|
W.write<uint16_t>(static_cast<int16_t>(Header.NumberOfSections));
|
|
|
|
W.write<uint32_t>(Header.TimeDateStamp);
|
|
|
|
W.write<uint32_t>(Header.PointerToSymbolTable);
|
|
|
|
W.write<uint32_t>(Header.NumberOfSymbols);
|
|
|
|
W.write<uint16_t>(Header.SizeOfOptionalHeader);
|
|
|
|
W.write<uint16_t>(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) {
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS.write(S.Data.Name, COFF::NameSize);
|
|
|
|
W.write<uint32_t>(S.Data.Value);
|
2014-09-16 03:42:42 +08:00
|
|
|
if (UseBigObj)
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint32_t>(S.Data.SectionNumber);
|
2014-09-16 03:42:42 +08:00
|
|
|
else
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint16_t>(static_cast<int16_t>(S.Data.SectionNumber));
|
|
|
|
W.write<uint16_t>(S.Data.Type);
|
|
|
|
W.OS << char(S.Data.StorageClass);
|
|
|
|
W.OS << char(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 ATWeakExternal:
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint32_t>(i.Aux.WeakExternal.TagIndex);
|
|
|
|
W.write<uint32_t>(i.Aux.WeakExternal.Characteristics);
|
|
|
|
W.OS.write_zeros(sizeof(i.Aux.WeakExternal.unused));
|
2014-09-16 03:42:42 +08:00
|
|
|
if (UseBigObj)
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
|
2010-07-26 10:17:32 +08:00
|
|
|
break;
|
|
|
|
case ATFile:
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS.write(reinterpret_cast<const char *>(&i.Aux),
|
|
|
|
UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size);
|
2010-07-26 10:17:32 +08:00
|
|
|
break;
|
|
|
|
case ATSectionDefinition:
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint32_t>(i.Aux.SectionDefinition.Length);
|
|
|
|
W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfRelocations);
|
|
|
|
W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfLinenumbers);
|
|
|
|
W.write<uint32_t>(i.Aux.SectionDefinition.CheckSum);
|
|
|
|
W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
|
|
|
|
W.OS << char(i.Aux.SectionDefinition.Selection);
|
|
|
|
W.OS.write_zeros(sizeof(i.Aux.SectionDefinition.unused));
|
|
|
|
W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
|
2014-09-16 03:42:42 +08:00
|
|
|
if (UseBigObj)
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS.write_zeros(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());
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 10:13:45 +08:00
|
|
|
llvm::sort(Arr, [](const COFFSection *A, const COFFSection *B) {
|
|
|
|
return A->Number < B->Number;
|
|
|
|
});
|
2017-02-18 01:32:54 +08:00
|
|
|
|
|
|
|
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;
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS.write(S.Name, COFF::NameSize);
|
|
|
|
W.write<uint32_t>(S.VirtualSize);
|
|
|
|
W.write<uint32_t>(S.VirtualAddress);
|
|
|
|
W.write<uint32_t>(S.SizeOfRawData);
|
|
|
|
W.write<uint32_t>(S.PointerToRawData);
|
|
|
|
W.write<uint32_t>(S.PointerToRelocations);
|
|
|
|
W.write<uint32_t>(S.PointerToLineNumbers);
|
|
|
|
W.write<uint16_t>(S.NumberOfRelocations);
|
|
|
|
W.write<uint16_t>(S.NumberOfLineNumbers);
|
|
|
|
W.write<uint32_t>(S.Characteristics);
|
2017-02-18 01:32:54 +08:00
|
|
|
}
|
2010-07-26 10:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint32_t>(R.VirtualAddress);
|
|
|
|
W.write<uint32_t>(R.SymbolTableIndex);
|
|
|
|
W.write<uint16_t>(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);
|
2018-05-22 02:11:35 +08:00
|
|
|
Asm.writeSectionData(VecOS, &MCSec, Layout);
|
2017-02-16 10:56:06 +08:00
|
|
|
|
|
|
|
// Write the section contents to the object file.
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS << Buf;
|
2017-02-16 10:56:06 +08:00
|
|
|
|
|
|
|
// 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);
|
2019-10-09 17:06:30 +08:00
|
|
|
JC.update(makeArrayRef(reinterpret_cast<uint8_t*>(Buf.data()), Buf.size()));
|
2017-02-16 10:56:06 +08:00
|
|
|
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) {
|
2018-08-23 13:39:36 +08:00
|
|
|
assert(W.OS.tell() == Sec.Header.PointerToRawData &&
|
2017-02-16 10:35:48 +08:00
|
|
|
"Section::PointerToRawData is insane!");
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
assert(W.OS.tell() == Sec.Header.PointerToRelocations &&
|
2017-02-16 10:35:48 +08:00
|
|
|
"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) {
|
2018-08-23 07:58:16 +08:00
|
|
|
if (EmitAddrsigSection) {
|
|
|
|
AddrsigSection = Asm.getContext().getCOFFSection(
|
|
|
|
".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE,
|
|
|
|
SectionKind::getMetadata());
|
|
|
|
Asm.registerSection(*AddrsigSection);
|
|
|
|
}
|
|
|
|
|
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 {
|
[MC] Always emit relocations for same-section function references
Summary:
We already emit relocations in this case when the "incremental linker
compatible" flag is set, but it turns out these relocations are also
required for /guard:cf. Now that we have two use cases for this
behavior, let's make it unconditional to try to keep things simple.
We never hit this problem in Clang because it always sets the
"incremental linker compatible" flag when targeting MSVC. However, LLD
LTO doesn't set this flag, so we'd get CFG failures at runtime when
using ThinLTO and /guard:cf. We probably don't want LLD LTO to set the
"incremental linker compatible" assembler flag, since this has nothing
to do with incremental linking, and we don't need to timestamp LTO
temporary objects.
Fixes PR36624.
Reviewers: inglorion, espindola, majnemer
Subscribers: mehdi_amini, llvm-commits, hiraditya
Differential Revision: https://reviews.llvm.org/D44485
llvm-svn: 327557
2018-03-15 03:24:32 +08:00
|
|
|
// Don't drop relocations between functions, even if they are in the same text
|
|
|
|
// section. Multiple Visual C++ linker features depend on having the
|
|
|
|
// relocations present. The /INCREMENTAL flag will cause these relocations to
|
|
|
|
// point to thunks, and the /GUARD:CF flag assumes that it can use relocations
|
|
|
|
// to approximate the set of all address taken functions. LLD's implementation
|
|
|
|
// of /GUARD:CF also relies on the existance of these relocations.
|
2015-06-09 01:17:12 +08:00
|
|
|
uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
|
[MC] Always emit relocations for same-section function references
Summary:
We already emit relocations in this case when the "incremental linker
compatible" flag is set, but it turns out these relocations are also
required for /guard:cf. Now that we have two use cases for this
behavior, let's make it unconditional to try to keep things simple.
We never hit this problem in Clang because it always sets the
"incremental linker compatible" flag when targeting MSVC. However, LLD
LTO doesn't set this flag, so we'd get CFG failures at runtime when
using ThinLTO and /guard:cf. We probably don't want LLD LTO to set the
"incremental linker compatible" assembler flag, since this has nothing
to do with incremental linking, and we don't need to timestamp LTO
temporary objects.
Fixes PR36624.
Reviewers: inglorion, espindola, majnemer
Subscribers: mehdi_amini, llvm-commits, hiraditya
Differential Revision: https://reviews.llvm.org/D44485
llvm-svn: 327557
2018-03-15 03:24:32 +08:00
|
|
|
if ((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
|
|
|
}
|
|
|
|
|
2020-03-11 05:44:03 +08:00
|
|
|
void WinCOFFObjectWriter::setWeakDefaultNames() {
|
|
|
|
if (WeakDefaults.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If multiple object files use a weak symbol (either with a regular
|
|
|
|
// defined default, or an absolute zero symbol as default), the defaults
|
|
|
|
// cause duplicate definitions unless their names are made unique. Look
|
|
|
|
// for a defined extern symbol, that isn't comdat - that should be unique
|
|
|
|
// unless there are other duplicate definitions. And if none is found,
|
|
|
|
// allow picking a comdat symbol, as that's still better than nothing.
|
|
|
|
|
|
|
|
COFFSymbol *Unique = nullptr;
|
|
|
|
for (bool AllowComdat : {false, true}) {
|
|
|
|
for (auto &Sym : Symbols) {
|
|
|
|
// Don't include the names of the defaults themselves
|
|
|
|
if (WeakDefaults.count(Sym.get()))
|
|
|
|
continue;
|
|
|
|
// Only consider external symbols
|
|
|
|
if (Sym->Data.StorageClass != COFF::IMAGE_SYM_CLASS_EXTERNAL)
|
|
|
|
continue;
|
|
|
|
// Only consider symbols defined in a section or that are absolute
|
|
|
|
if (!Sym->Section && Sym->Data.SectionNumber != COFF::IMAGE_SYM_ABSOLUTE)
|
|
|
|
continue;
|
|
|
|
if (!AllowComdat && Sym->Section &&
|
|
|
|
Sym->Section->Header.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
|
|
|
|
continue;
|
|
|
|
Unique = Sym.get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Unique)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// If we didn't find any unique symbol to use for the names, just skip this.
|
|
|
|
if (!Unique)
|
|
|
|
return;
|
|
|
|
for (auto *Sym : WeakDefaults) {
|
|
|
|
Sym->Name.append(".");
|
|
|
|
Sym->Name.append(Unique->Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-05-22 02:17:42 +08:00
|
|
|
unsigned Offset = W.OS.tell();
|
2017-02-16 10:35:48 +08:00
|
|
|
|
|
|
|
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)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-22 02:23:50 +08:00
|
|
|
uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout) {
|
|
|
|
uint64_t StartOffset = W.OS.tell();
|
|
|
|
|
2017-02-16 10:35:48 +08:00
|
|
|
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;
|
|
|
|
|
2020-03-11 05:44:03 +08:00
|
|
|
setWeakDefaultNames();
|
2017-02-16 10:35:48 +08:00
|
|
|
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;
|
2018-08-17 05:34:41 +08:00
|
|
|
const MCSymbol *AssocMCSym = MCSec.getCOMDATSymbol();
|
|
|
|
assert(AssocMCSym);
|
2013-07-06 20:13:10 +08:00
|
|
|
|
2018-08-17 05:34:41 +08:00
|
|
|
// It's an error to try to associate with an undefined symbol or a symbol
|
|
|
|
// without a section.
|
|
|
|
if (!AssocMCSym->isInSection()) {
|
|
|
|
Asm.getContext().reportError(
|
2020-04-16 06:49:05 +08:00
|
|
|
SMLoc(), Twine("cannot make section ") + MCSec.getName() +
|
2018-08-17 05:34:41 +08:00
|
|
|
Twine(" associative with sectionless symbol ") +
|
|
|
|
AssocMCSym->getName());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto *AssocMCSec = cast<MCSectionCOFF>(&AssocMCSym->getSection());
|
|
|
|
assert(SectionMap.count(AssocMCSec));
|
|
|
|
COFFSection *AssocSec = SectionMap[AssocMCSec];
|
2013-07-06 20:13:10 +08:00
|
|
|
|
|
|
|
// Skip this section if the associated section is unused.
|
2018-08-17 05:34:41 +08:00
|
|
|
if (AssocSec->Number == -1)
|
2013-07-06 20:13:10 +08:00
|
|
|
continue;
|
|
|
|
|
2018-08-17 05:34:41 +08:00
|
|
|
Section->Symbol->Aux[0].Aux.SectionDefinition.Number = AssocSec->Number;
|
2013-07-06 20:13:10 +08:00
|
|
|
}
|
|
|
|
|
2018-08-23 07:58:16 +08:00
|
|
|
// Create the contents of the .llvm_addrsig section.
|
|
|
|
if (EmitAddrsigSection) {
|
|
|
|
auto Frag = new MCDataFragment(AddrsigSection);
|
2018-08-23 14:57:49 +08:00
|
|
|
Frag->setLayoutOrder(0);
|
2018-08-23 07:58:16 +08:00
|
|
|
raw_svector_ostream OS(Frag->getContents());
|
|
|
|
for (const MCSymbol *S : AddrsigSyms) {
|
|
|
|
if (!S->isTemporary()) {
|
|
|
|
encodeULEB128(S->getIndex(), OS);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MCSection *TargetSection = &S->getSection();
|
|
|
|
assert(SectionMap.find(TargetSection) != SectionMap.end() &&
|
|
|
|
"Section must already have been defined in "
|
|
|
|
"executePostLayoutBinding!");
|
|
|
|
encodeULEB128(SectionMap[TargetSection]->Symbol->getIndex(), OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
assert(W.OS.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.
|
2018-05-22 02:17:42 +08:00
|
|
|
Strings.write(W.OS);
|
2018-05-22 02:23:50 +08:00
|
|
|
|
|
|
|
return W.OS.tell() - StartOffset;
|
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
|
|
|
|
|
2017-10-11 00:28:07 +08:00
|
|
|
std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter(
|
2017-10-10 08:50:29 +08:00
|
|
|
std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) {
|
2019-08-15 23:54:37 +08:00
|
|
|
return std::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS);
|
2010-07-26 11:01:28 +08:00
|
|
|
}
|