2013-03-09 17:32:16 +08:00
|
|
|
//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===//
|
2010-08-17 02:57:57 +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-08-17 02:57:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements ELF object file writer information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2011-12-22 11:38:00 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2017-07-12 07:56:10 +08:00
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
2014-04-11 05:53:53 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-08-17 02:57:57 +08:00
|
|
|
#include "llvm/MC/MCAsmLayout.h"
|
2011-12-22 11:38:00 +08:00
|
|
|
#include "llvm/MC/MCAssembler.h"
|
2010-08-17 02:57:57 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/MC/MCELFObjectWriter.h"
|
2010-08-17 02:57:57 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/MC/MCFixup.h"
|
2017-07-12 07:56:10 +08:00
|
|
|
#include "llvm/MC/MCFixupKindInfo.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/MC/MCFragment.h"
|
2018-09-07 06:09:31 +08:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2012-03-26 14:58:25 +08:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/MC/MCSection.h"
|
2010-08-17 02:57:57 +08:00
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2015-06-02 08:25:12 +08:00
|
|
|
#include "llvm/MC/MCSymbolELF.h"
|
2010-08-17 02:57:57 +08:00
|
|
|
#include "llvm/MC/MCValue.h"
|
2014-07-03 10:01:39 +08:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
[Alignment] Move OffsetToAlignment to Alignment.h
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet, JDevlieghere, alexshap, rupprecht, jhenderson
Subscribers: sdardis, nemanjai, hiraditya, kbarton, jakehehrlich, jrtc27, MaskRay, atanasyan, jsji, seiya, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D67499
llvm-svn: 371742
2019-09-12 23:20:36 +08:00
|
|
|
#include "llvm/Support/Alignment.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2014-04-11 05:53:53 +08:00
|
|
|
#include "llvm/Support/Compression.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2017-01-17 23:45:07 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2018-07-18 06:17:18 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Support/SMLoc.h"
|
2015-10-23 02:32:06 +08:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/Support/SwapByteOrder.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2010-08-17 02:57:57 +08:00
|
|
|
#include <vector>
|
2016-02-03 02:20:45 +08:00
|
|
|
|
2010-08-17 02:57:57 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-05-12 06:53:06 +08:00
|
|
|
#undef DEBUG_TYPE
|
|
|
|
#define DEBUG_TYPE "reloc-info"
|
|
|
|
|
2011-12-22 11:38:00 +08:00
|
|
|
namespace {
|
2017-02-11 08:27:28 +08:00
|
|
|
|
2017-04-27 06:31:39 +08:00
|
|
|
using SectionIndexMapTy = DenseMap<const MCSectionELF *, uint32_t>;
|
2014-03-26 07:44:25 +08:00
|
|
|
|
2015-04-30 05:09:32 +08:00
|
|
|
class ELFObjectWriter;
|
2018-05-22 03:18:28 +08:00
|
|
|
struct ELFWriter;
|
2015-04-30 05:09:32 +08:00
|
|
|
|
2018-05-22 03:44:54 +08:00
|
|
|
bool isDwoSection(const MCSectionELF &Sec) {
|
|
|
|
return Sec.getSectionName().endswith(".dwo");
|
|
|
|
}
|
|
|
|
|
2014-03-26 07:44:25 +08:00
|
|
|
class SymbolTableWriter {
|
2018-05-22 03:18:28 +08:00
|
|
|
ELFWriter &EWriter;
|
2014-03-26 07:44:25 +08:00
|
|
|
bool Is64Bit;
|
|
|
|
|
2015-04-30 05:09:32 +08:00
|
|
|
// indexes we are going to write to .symtab_shndx.
|
|
|
|
std::vector<uint32_t> ShndxIndexes;
|
2014-03-26 07:44:25 +08:00
|
|
|
|
|
|
|
// The numbel of symbols written so far.
|
|
|
|
unsigned NumWritten;
|
|
|
|
|
|
|
|
void createSymtabShndx();
|
|
|
|
|
2015-04-30 05:09:32 +08:00
|
|
|
template <typename T> void write(T Value);
|
2014-03-26 07:44:25 +08:00
|
|
|
|
|
|
|
public:
|
2018-05-22 03:18:28 +08:00
|
|
|
SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit);
|
2014-03-26 07:44:25 +08:00
|
|
|
|
|
|
|
void writeSymbol(uint32_t name, uint8_t info, uint64_t value, uint64_t size,
|
|
|
|
uint8_t other, uint32_t shndx, bool Reserved);
|
2015-04-30 05:09:32 +08:00
|
|
|
|
|
|
|
ArrayRef<uint32_t> getShndxIndexes() const { return ShndxIndexes; }
|
2014-03-26 07:44:25 +08:00
|
|
|
};
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
struct ELFWriter {
|
|
|
|
ELFObjectWriter &OWriter;
|
|
|
|
support::endian::Writer W;
|
|
|
|
|
2018-05-22 03:44:54 +08:00
|
|
|
enum DwoMode {
|
|
|
|
AllSections,
|
|
|
|
NonDwoOnly,
|
|
|
|
DwoOnly,
|
|
|
|
} Mode;
|
|
|
|
|
2016-04-13 05:45:53 +08:00
|
|
|
static uint64_t SymbolValue(const MCSymbol &Sym, const MCAsmLayout &Layout);
|
|
|
|
static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
|
|
|
|
bool Used, bool Renamed);
|
|
|
|
|
|
|
|
/// Helper struct for containing some precomputed information on symbols.
|
|
|
|
struct ELFSymbolData {
|
|
|
|
const MCSymbolELF *Symbol;
|
|
|
|
uint32_t SectionIndex;
|
|
|
|
StringRef Name;
|
|
|
|
|
|
|
|
// Support lexicographic sorting.
|
|
|
|
bool operator<(const ELFSymbolData &RHS) const {
|
|
|
|
unsigned LHSType = Symbol->getType();
|
|
|
|
unsigned RHSType = RHS.Symbol->getType();
|
|
|
|
if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION)
|
|
|
|
return false;
|
|
|
|
if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
|
|
|
|
return true;
|
|
|
|
if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
|
|
|
|
return SectionIndex < RHS.SectionIndex;
|
|
|
|
return Name < RHS.Name;
|
2011-12-22 11:38:00 +08:00
|
|
|
}
|
2016-04-13 05:45:53 +08:00
|
|
|
};
|
2011-12-22 11:38:00 +08:00
|
|
|
|
2016-04-13 05:45:53 +08:00
|
|
|
/// @}
|
|
|
|
/// @name Symbol Table Data
|
|
|
|
/// @{
|
2011-12-22 11:38:00 +08:00
|
|
|
|
2016-04-13 05:45:53 +08:00
|
|
|
StringTableBuilder StrTabBuilder{StringTableBuilder::ELF};
|
2011-12-22 11:38:00 +08:00
|
|
|
|
2016-04-13 05:45:53 +08:00
|
|
|
/// @}
|
2015-05-01 05:51:58 +08:00
|
|
|
|
2016-04-13 05:45:53 +08:00
|
|
|
// This holds the symbol table index of the last local symbol.
|
|
|
|
unsigned LastLocalSymbolIndex;
|
|
|
|
// This holds the .strtab section index.
|
|
|
|
unsigned StringTableIndex;
|
|
|
|
// This holds the .symtab section index.
|
|
|
|
unsigned SymbolTableIndex;
|
2011-12-22 11:38:00 +08:00
|
|
|
|
2016-04-13 05:45:53 +08:00
|
|
|
// Sections in the order they are to be output in the section table.
|
|
|
|
std::vector<const MCSectionELF *> SectionTable;
|
|
|
|
unsigned addToSectionTable(const MCSectionELF *Sec);
|
2011-12-22 11:38:00 +08:00
|
|
|
|
2016-04-13 05:45:53 +08:00
|
|
|
// TargetObjectWriter wrappers.
|
2018-05-22 03:18:28 +08:00
|
|
|
bool is64Bit() const;
|
|
|
|
bool hasRelocationAddend() const;
|
2011-12-22 11:38:00 +08:00
|
|
|
|
2016-04-13 05:45:53 +08:00
|
|
|
void align(unsigned Alignment);
|
2015-03-25 21:16:53 +08:00
|
|
|
|
2016-05-27 20:27:32 +08:00
|
|
|
bool maybeWriteCompression(uint64_t Size,
|
|
|
|
SmallVectorImpl<char> &CompressedContents,
|
|
|
|
bool ZLibStyle, unsigned Alignment);
|
|
|
|
|
2016-04-13 05:45:53 +08:00
|
|
|
public:
|
2018-05-22 03:18:28 +08:00
|
|
|
ELFWriter(ELFObjectWriter &OWriter, raw_pwrite_stream &OS,
|
2018-05-22 03:44:54 +08:00
|
|
|
bool IsLittleEndian, DwoMode Mode)
|
2018-05-22 03:18:28 +08:00
|
|
|
: OWriter(OWriter),
|
2018-05-22 03:44:54 +08:00
|
|
|
W(OS, IsLittleEndian ? support::little : support::big), Mode(Mode) {}
|
2016-04-13 05:45:53 +08:00
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
void WriteWord(uint64_t Word) {
|
2016-04-13 05:45:53 +08:00
|
|
|
if (is64Bit())
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint64_t>(Word);
|
2016-04-13 05:45:53 +08:00
|
|
|
else
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint32_t>(Word);
|
2016-04-13 05:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> void write(T Val) {
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write(Val);
|
2016-04-13 05:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeHeader(const MCAssembler &Asm);
|
|
|
|
|
|
|
|
void writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
|
|
|
|
ELFSymbolData &MSD, const MCAsmLayout &Layout);
|
|
|
|
|
|
|
|
// Start and end offset of each section
|
2017-04-27 06:31:39 +08:00
|
|
|
using SectionOffsetsTy =
|
|
|
|
std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>>;
|
2016-04-13 05:45:53 +08:00
|
|
|
|
|
|
|
// Map from a signature symbol to the group section index
|
2017-04-27 06:31:39 +08:00
|
|
|
using RevGroupMapTy = DenseMap<const MCSymbol *, unsigned>;
|
2016-04-13 05:45:53 +08:00
|
|
|
|
|
|
|
/// Compute the symbol table data
|
|
|
|
///
|
|
|
|
/// \param Asm - The assembler.
|
|
|
|
/// \param SectionIndexMap - Maps a section to its index.
|
|
|
|
/// \param RevGroupMap - Maps a signature symbol to the group section.
|
|
|
|
void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
|
|
|
|
const SectionIndexMapTy &SectionIndexMap,
|
|
|
|
const RevGroupMapTy &RevGroupMap,
|
|
|
|
SectionOffsetsTy &SectionOffsets);
|
|
|
|
|
2018-07-18 06:17:18 +08:00
|
|
|
void writeAddrsigSection();
|
|
|
|
|
2016-04-13 05:45:53 +08:00
|
|
|
MCSectionELF *createRelocationSection(MCContext &Ctx,
|
|
|
|
const MCSectionELF &Sec);
|
|
|
|
|
|
|
|
const MCSectionELF *createStringTable(MCContext &Ctx);
|
|
|
|
|
|
|
|
void writeSectionHeader(const MCAsmLayout &Layout,
|
|
|
|
const SectionIndexMapTy &SectionIndexMap,
|
|
|
|
const SectionOffsetsTy &SectionOffsets);
|
|
|
|
|
|
|
|
void writeSectionData(const MCAssembler &Asm, MCSection &Sec,
|
|
|
|
const MCAsmLayout &Layout);
|
|
|
|
|
|
|
|
void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
|
|
|
|
uint64_t Address, uint64_t Offset, uint64_t Size,
|
|
|
|
uint32_t Link, uint32_t Info, uint64_t Alignment,
|
|
|
|
uint64_t EntrySize);
|
|
|
|
|
|
|
|
void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout);
|
|
|
|
void writeSection(const SectionIndexMapTy &SectionIndexMap,
|
|
|
|
uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size,
|
|
|
|
const MCSectionELF &Section);
|
|
|
|
};
|
|
|
|
|
|
|
|
class ELFObjectWriter : public MCObjectWriter {
|
|
|
|
/// The target specific ELF writer instance.
|
|
|
|
std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
|
|
|
|
|
|
|
|
DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>> Relocations;
|
|
|
|
|
|
|
|
DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
|
|
|
|
|
2018-07-18 06:17:18 +08:00
|
|
|
bool EmitAddrsigSection = false;
|
|
|
|
std::vector<const MCSymbol *> AddrsigSyms;
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
bool hasRelocationAddend() const;
|
|
|
|
|
|
|
|
bool shouldRelocateWithSymbol(const MCAssembler &Asm,
|
|
|
|
const MCSymbolRefExpr *RefA,
|
|
|
|
const MCSymbolELF *Sym, uint64_t C,
|
|
|
|
unsigned Type) const;
|
|
|
|
|
|
|
|
public:
|
2018-05-22 03:30:59 +08:00
|
|
|
ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW)
|
|
|
|
: TargetObjectWriter(std::move(MOTW)) {}
|
2018-05-22 03:18:28 +08:00
|
|
|
|
|
|
|
void reset() override {
|
|
|
|
Relocations.clear();
|
|
|
|
Renames.clear();
|
|
|
|
MCObjectWriter::reset();
|
|
|
|
}
|
|
|
|
|
2016-04-13 05:45:53 +08:00
|
|
|
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
|
|
|
|
const MCSymbol &SymA,
|
|
|
|
const MCFragment &FB, bool InSet,
|
|
|
|
bool IsPCRel) const override;
|
|
|
|
|
2018-05-22 03:44:54 +08:00
|
|
|
virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
|
|
|
|
const MCSectionELF *From,
|
|
|
|
const MCSectionELF *To) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
|
|
|
|
const MCFragment *Fragment, const MCFixup &Fixup,
|
|
|
|
MCValue Target, uint64_t &FixedValue) override;
|
|
|
|
|
|
|
|
void executePostLayoutBinding(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout) override;
|
|
|
|
|
2018-07-18 06:17:18 +08:00
|
|
|
void emitAddrsigSection() override { EmitAddrsigSection = true; }
|
|
|
|
void addAddrsigSymbol(const MCSymbol *Sym) override {
|
|
|
|
AddrsigSyms.push_back(Sym);
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
friend struct ELFWriter;
|
2016-04-13 05:45:53 +08:00
|
|
|
};
|
2017-02-11 08:27:28 +08:00
|
|
|
|
2018-05-22 03:30:59 +08:00
|
|
|
class ELFSingleObjectWriter : public ELFObjectWriter {
|
|
|
|
raw_pwrite_stream &OS;
|
|
|
|
bool IsLittleEndian;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ELFSingleObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
|
|
|
|
raw_pwrite_stream &OS, bool IsLittleEndian)
|
|
|
|
: ELFObjectWriter(std::move(MOTW)), OS(OS),
|
|
|
|
IsLittleEndian(IsLittleEndian) {}
|
|
|
|
|
|
|
|
uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
|
2018-05-22 03:44:54 +08:00
|
|
|
return ELFWriter(*this, OS, IsLittleEndian, ELFWriter::AllSections)
|
|
|
|
.writeObject(Asm, Layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
friend struct ELFWriter;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ELFDwoObjectWriter : public ELFObjectWriter {
|
|
|
|
raw_pwrite_stream &OS, &DwoOS;
|
|
|
|
bool IsLittleEndian;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
|
|
|
|
raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
|
|
|
|
bool IsLittleEndian)
|
|
|
|
: ELFObjectWriter(std::move(MOTW)), OS(OS), DwoOS(DwoOS),
|
|
|
|
IsLittleEndian(IsLittleEndian) {}
|
|
|
|
|
|
|
|
virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
|
|
|
|
const MCSectionELF *From,
|
|
|
|
const MCSectionELF *To) override {
|
|
|
|
if (isDwoSection(*From)) {
|
|
|
|
Ctx.reportError(Loc, "A dwo section may not contain relocations");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (To && isDwoSection(*To)) {
|
|
|
|
Ctx.reportError(Loc, "A relocation may not refer to a dwo section");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
|
|
|
|
uint64_t Size = ELFWriter(*this, OS, IsLittleEndian, ELFWriter::NonDwoOnly)
|
|
|
|
.writeObject(Asm, Layout);
|
|
|
|
Size += ELFWriter(*this, DwoOS, IsLittleEndian, ELFWriter::DwoOnly)
|
|
|
|
.writeObject(Asm, Layout);
|
|
|
|
return Size;
|
2018-05-22 03:30:59 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-03 02:20:45 +08:00
|
|
|
} // end anonymous namespace
|
2011-12-22 11:38:00 +08:00
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
void ELFWriter::align(unsigned Alignment) {
|
[Alignment] Move OffsetToAlignment to Alignment.h
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet, JDevlieghere, alexshap, rupprecht, jhenderson
Subscribers: sdardis, nemanjai, hiraditya, kbarton, jakehehrlich, jrtc27, MaskRay, atanasyan, jsji, seiya, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D67499
llvm-svn: 371742
2019-09-12 23:20:36 +08:00
|
|
|
uint64_t Padding = offsetToAlignment(W.OS.tell(), llvm::Align(Alignment));
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS.write_zeros(Padding);
|
2015-06-06 02:21:00 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
unsigned ELFWriter::addToSectionTable(const MCSectionELF *Sec) {
|
2015-05-01 04:53:27 +08:00
|
|
|
SectionTable.push_back(Sec);
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
StrTabBuilder.add(Sec->getSectionName());
|
2015-05-01 04:53:27 +08:00
|
|
|
return SectionTable.size();
|
|
|
|
}
|
|
|
|
|
2014-03-26 07:44:25 +08:00
|
|
|
void SymbolTableWriter::createSymtabShndx() {
|
2015-04-30 05:09:32 +08:00
|
|
|
if (!ShndxIndexes.empty())
|
2014-03-26 07:44:25 +08:00
|
|
|
return;
|
|
|
|
|
2015-04-30 05:09:32 +08:00
|
|
|
ShndxIndexes.resize(NumWritten);
|
2014-03-26 07:44:25 +08:00
|
|
|
}
|
|
|
|
|
2015-04-30 05:09:32 +08:00
|
|
|
template <typename T> void SymbolTableWriter::write(T Value) {
|
|
|
|
EWriter.write(Value);
|
2014-03-26 07:44:25 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
SymbolTableWriter::SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit)
|
2015-04-30 05:09:32 +08:00
|
|
|
: EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {}
|
2014-03-26 07:44:25 +08:00
|
|
|
|
|
|
|
void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
|
|
|
|
uint64_t size, uint8_t other,
|
|
|
|
uint32_t shndx, bool Reserved) {
|
|
|
|
bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
|
|
|
|
|
|
|
|
if (LargeIndex)
|
|
|
|
createSymtabShndx();
|
|
|
|
|
2015-04-30 05:09:32 +08:00
|
|
|
if (!ShndxIndexes.empty()) {
|
2014-03-26 07:44:25 +08:00
|
|
|
if (LargeIndex)
|
2015-04-30 05:09:32 +08:00
|
|
|
ShndxIndexes.push_back(shndx);
|
2014-03-26 07:44:25 +08:00
|
|
|
else
|
2015-04-30 05:09:32 +08:00
|
|
|
ShndxIndexes.push_back(0);
|
2014-03-26 07:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
|
|
|
|
|
|
|
|
if (Is64Bit) {
|
2015-04-30 05:09:32 +08:00
|
|
|
write(name); // st_name
|
|
|
|
write(info); // st_info
|
|
|
|
write(other); // st_other
|
|
|
|
write(Index); // st_shndx
|
|
|
|
write(value); // st_value
|
|
|
|
write(size); // st_size
|
2014-03-26 07:44:25 +08:00
|
|
|
} else {
|
2015-04-30 05:09:32 +08:00
|
|
|
write(name); // st_name
|
|
|
|
write(uint32_t(value)); // st_value
|
|
|
|
write(uint32_t(size)); // st_size
|
|
|
|
write(info); // st_info
|
|
|
|
write(other); // st_other
|
|
|
|
write(Index); // st_shndx
|
2014-03-26 07:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
++NumWritten;
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
bool ELFWriter::is64Bit() const {
|
|
|
|
return OWriter.TargetObjectWriter->is64Bit();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ELFWriter::hasRelocationAddend() const {
|
|
|
|
return OWriter.hasRelocationAddend();
|
|
|
|
}
|
|
|
|
|
2010-08-17 02:57:57 +08:00
|
|
|
// Emit the ELF header.
|
2018-05-22 03:18:28 +08:00
|
|
|
void ELFWriter::writeHeader(const MCAssembler &Asm) {
|
2010-08-17 02:57:57 +08:00
|
|
|
// ELF Header
|
|
|
|
// ----------
|
|
|
|
//
|
|
|
|
// Note
|
|
|
|
// ----
|
|
|
|
// emitWord method behaves differently for ELF32 and ELF64, writing
|
|
|
|
// 4 bytes in the former and 8 in the latter.
|
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS << ELF::ElfMagic; // e_ident[EI_MAG0] to e_ident[EI_MAG3]
|
2010-08-17 02:57:57 +08:00
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS << char(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
|
2010-08-17 02:57:57 +08:00
|
|
|
|
|
|
|
// e_ident[EI_DATA]
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS << char(W.Endian == support::little ? ELF::ELFDATA2LSB
|
|
|
|
: ELF::ELFDATA2MSB);
|
2010-08-17 02:57:57 +08:00
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS << char(ELF::EV_CURRENT); // e_ident[EI_VERSION]
|
2010-09-10 01:57:50 +08:00
|
|
|
// e_ident[EI_OSABI]
|
2018-05-22 03:18:28 +08:00
|
|
|
W.OS << char(OWriter.TargetObjectWriter->getOSABI());
|
2019-02-15 06:42:09 +08:00
|
|
|
// e_ident[EI_ABIVERSION]
|
|
|
|
W.OS << char(OWriter.TargetObjectWriter->getABIVersion());
|
2010-08-17 02:57:57 +08:00
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD);
|
2010-08-17 02:57:57 +08:00
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint16_t>(ELF::ET_REL); // e_type
|
2010-08-17 02:57:57 +08:00
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
W.write<uint16_t>(OWriter.TargetObjectWriter->getEMachine()); // e_machine = target
|
2010-08-17 02:57:57 +08:00
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint32_t>(ELF::EV_CURRENT); // e_version
|
2010-08-17 02:57:57 +08:00
|
|
|
WriteWord(0); // e_entry, no entry point in .o file
|
|
|
|
WriteWord(0); // e_phoff, no program header for .o
|
2015-04-15 06:54:16 +08:00
|
|
|
WriteWord(0); // e_shoff = sec hdr table off in bytes
|
2010-08-17 02:57:57 +08:00
|
|
|
|
2011-02-05 05:41:11 +08:00
|
|
|
// e_flags = whatever the target wants
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint32_t>(Asm.getELFHeaderEFlags());
|
2010-08-17 02:57:57 +08:00
|
|
|
|
|
|
|
// e_ehsize = ELF header size
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr)
|
|
|
|
: sizeof(ELF::Elf32_Ehdr));
|
2010-08-17 02:57:57 +08:00
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint16_t>(0); // e_phentsize = prog header entry size
|
|
|
|
W.write<uint16_t>(0); // e_phnum = # prog header entries = 0
|
2010-08-17 02:57:57 +08:00
|
|
|
|
|
|
|
// e_shentsize = Section header entry size
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr)
|
|
|
|
: sizeof(ELF::Elf32_Shdr));
|
2010-08-17 02:57:57 +08:00
|
|
|
|
|
|
|
// e_shnum = # of section header ents
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint16_t>(0);
|
2010-08-17 02:57:57 +08:00
|
|
|
|
|
|
|
// e_shstrndx = Section # of '.shstrtab'
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
assert(StringTableIndex < ELF::SHN_LORESERVE);
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint16_t>(StringTableIndex);
|
2010-08-17 02:57:57 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
uint64_t ELFWriter::SymbolValue(const MCSymbol &Sym,
|
|
|
|
const MCAsmLayout &Layout) {
|
AMDGPU/MC: Add .amdgpu_lds directive
Summary:
The directive defines a symbol as an group/local memory (LDS) symbol.
LDS symbols behave similar to common symbols for the purposes of ELF,
using the processor-specific SHN_AMDGPU_LDS as section index.
It is the linker and/or runtime loader's job to "instantiate" LDS symbols
and resolve relocations that reference them.
It is not possible to initialize LDS memory (not even zero-initialize
as for .bss).
We want to be able to link together objects -- starting with relocatable
objects, but possible expanding to shared objects in the future -- that
access LDS memory in a flexible way.
LDS memory is in an address space that is entirely separate from the
address space that contains the program image (code and normal data),
so having program segments for it doesn't really make sense.
Furthermore, we want to be able to compile multiple kernels in a
compilation unit which have disjoint use of LDS memory. In that case,
we may want to place LDS symbols differently for different kernels
to save memory (LDS memory is very limited and physically private to
each kernel invocation), so we can't simply place LDS symbols in a
.lds section.
Hence this solution where LDS symbols always stay undefined.
Change-Id: I08cbc37a7c0c32f53f7b6123aa0afc91dbc1748f
Reviewers: arsenm, rampitec, t-tye, b-sumner, jsjodin
Subscribers: kzhuravl, jvesely, wdng, yaxunl, dstuttard, tpr, rupprecht, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61493
llvm-svn: 364296
2019-06-25 19:51:35 +08:00
|
|
|
if (Sym.isCommon() && (Sym.isTargetCommon() || Sym.isExternal()))
|
2015-05-30 01:48:04 +08:00
|
|
|
return Sym.getCommonAlignment();
|
2010-12-21 05:14:39 +08:00
|
|
|
|
2014-05-01 05:51:13 +08:00
|
|
|
uint64_t Res;
|
2015-05-20 12:39:01 +08:00
|
|
|
if (!Layout.getSymbolOffset(Sym, Res))
|
2014-05-01 00:59:35 +08:00
|
|
|
return 0;
|
2010-09-28 05:23:02 +08:00
|
|
|
|
2015-05-20 12:39:01 +08:00
|
|
|
if (Layout.getAssembler().isThumbFunc(&Sym))
|
2014-05-01 00:59:35 +08:00
|
|
|
Res |= 1;
|
2010-09-28 05:23:02 +08:00
|
|
|
|
2014-03-22 06:00:29 +08:00
|
|
|
return Res;
|
2010-09-28 05:23:02 +08:00
|
|
|
}
|
|
|
|
|
2014-01-08 04:17:03 +08:00
|
|
|
static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
|
|
|
|
uint8_t Type = newType;
|
|
|
|
|
|
|
|
// Propagation rules:
|
|
|
|
// IFUNC > FUNC > OBJECT > NOTYPE
|
|
|
|
// TLS_OBJECT > OBJECT > NOTYPE
|
|
|
|
//
|
|
|
|
// dont let the new type degrade the old type
|
|
|
|
switch (origType) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case ELF::STT_GNU_IFUNC:
|
|
|
|
if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
|
|
|
|
Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
|
|
|
|
Type = ELF::STT_GNU_IFUNC;
|
|
|
|
break;
|
|
|
|
case ELF::STT_FUNC:
|
|
|
|
if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
|
|
|
|
Type == ELF::STT_TLS)
|
|
|
|
Type = ELF::STT_FUNC;
|
|
|
|
break;
|
|
|
|
case ELF::STT_OBJECT:
|
|
|
|
if (Type == ELF::STT_NOTYPE)
|
|
|
|
Type = ELF::STT_OBJECT;
|
|
|
|
break;
|
|
|
|
case ELF::STT_TLS:
|
|
|
|
if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
|
|
|
|
Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
|
|
|
|
Type = ELF::STT_TLS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Type;
|
|
|
|
}
|
|
|
|
|
2019-09-07 22:58:47 +08:00
|
|
|
static bool isIFunc(const MCSymbolELF *Symbol) {
|
|
|
|
while (Symbol->getType() != ELF::STT_GNU_IFUNC) {
|
|
|
|
const MCSymbolRefExpr *Value;
|
|
|
|
if (!Symbol->isVariable() ||
|
|
|
|
!(Value = dyn_cast<MCSymbolRefExpr>(Symbol->getVariableValue())) ||
|
|
|
|
Value->getKind() != MCSymbolRefExpr::VK_None ||
|
|
|
|
mergeTypeForSet(Symbol->getType(), ELF::STT_GNU_IFUNC) != ELF::STT_GNU_IFUNC)
|
|
|
|
return false;
|
|
|
|
Symbol = &cast<MCSymbolELF>(Value->getSymbol());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
|
|
|
|
ELFSymbolData &MSD, const MCAsmLayout &Layout) {
|
2015-06-03 04:38:46 +08:00
|
|
|
const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol);
|
2015-06-02 08:25:12 +08:00
|
|
|
const MCSymbolELF *Base =
|
|
|
|
cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol));
|
2014-03-26 08:16:43 +08:00
|
|
|
|
|
|
|
// This has to be in sync with when computeSymbolTable uses SHN_ABS or
|
|
|
|
// SHN_COMMON.
|
2015-05-30 05:45:01 +08:00
|
|
|
bool IsReserved = !Base || Symbol.isCommon();
|
2010-10-31 08:16:26 +08:00
|
|
|
|
2013-02-20 05:57:35 +08:00
|
|
|
// Binding and Type share the same byte as upper and lower nibbles
|
2015-06-03 04:38:46 +08:00
|
|
|
uint8_t Binding = Symbol.getBinding();
|
|
|
|
uint8_t Type = Symbol.getType();
|
2019-09-07 22:58:47 +08:00
|
|
|
if (isIFunc(&Symbol))
|
|
|
|
Type = ELF::STT_GNU_IFUNC;
|
2014-03-23 11:33:20 +08:00
|
|
|
if (Base) {
|
2015-06-03 04:38:46 +08:00
|
|
|
Type = mergeTypeForSet(Type, Base->getType());
|
2014-03-23 11:33:20 +08:00
|
|
|
}
|
2015-06-04 08:47:43 +08:00
|
|
|
uint8_t Info = (Binding << 4) | Type;
|
2013-02-20 05:57:35 +08:00
|
|
|
|
2013-10-29 09:06:17 +08:00
|
|
|
// Other and Visibility share the same byte with Visibility using the lower
|
2013-02-20 05:57:35 +08:00
|
|
|
// 2 bits
|
2015-06-03 04:38:46 +08:00
|
|
|
uint8_t Visibility = Symbol.getVisibility();
|
2015-06-04 13:59:23 +08:00
|
|
|
uint8_t Other = Symbol.getOther() | Visibility;
|
2010-10-07 05:02:29 +08:00
|
|
|
|
2015-05-20 12:39:01 +08:00
|
|
|
uint64_t Value = SymbolValue(*MSD.Symbol, Layout);
|
2010-08-17 02:57:57 +08:00
|
|
|
uint64_t Size = 0;
|
|
|
|
|
2015-05-30 01:24:52 +08:00
|
|
|
const MCExpr *ESize = MSD.Symbol->getSize();
|
2014-03-27 08:28:24 +08:00
|
|
|
if (!ESize && Base)
|
2015-05-30 01:24:52 +08:00
|
|
|
ESize = Base->getSize();
|
2010-09-21 08:24:38 +08:00
|
|
|
|
2010-12-23 00:03:00 +08:00
|
|
|
if (ESize) {
|
|
|
|
int64_t Res;
|
2015-04-06 23:27:57 +08:00
|
|
|
if (!ESize->evaluateKnownAbsolute(Res, Layout))
|
2010-12-23 00:03:00 +08:00
|
|
|
report_fatal_error("Size expression must be absolute.");
|
|
|
|
Size = Res;
|
2010-08-17 02:57:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the symbol table entry
|
2015-05-29 04:25:29 +08:00
|
|
|
Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex,
|
|
|
|
IsReserved);
|
2010-08-17 02:57:57 +08:00
|
|
|
}
|
|
|
|
|
2015-04-18 04:05:17 +08:00
|
|
|
// True if the assembler knows nothing about the final value of the symbol.
|
|
|
|
// This doesn't cover the comdat issues, since in those cases the assembler
|
|
|
|
// can at least know that all symbols in the section will move together.
|
2015-06-03 04:38:46 +08:00
|
|
|
static bool isWeak(const MCSymbolELF &Sym) {
|
|
|
|
if (Sym.getType() == ELF::STT_GNU_IFUNC)
|
2015-04-17 16:46:11 +08:00
|
|
|
return true;
|
|
|
|
|
2015-06-03 04:38:46 +08:00
|
|
|
switch (Sym.getBinding()) {
|
2015-04-17 16:46:11 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown binding");
|
|
|
|
case ELF::STB_LOCAL:
|
|
|
|
return false;
|
|
|
|
case ELF::STB_GLOBAL:
|
2015-04-18 04:05:17 +08:00
|
|
|
return false;
|
2015-04-17 16:46:11 +08:00
|
|
|
case ELF::STB_WEAK:
|
|
|
|
case ELF::STB_GNU_UNIQUE:
|
2018-05-22 03:18:28 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-12-07 05:57:34 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
|
|
|
|
bool Used, bool Renamed) {
|
2014-03-20 10:12:01 +08:00
|
|
|
if (Symbol.isVariable()) {
|
|
|
|
const MCExpr *Expr = Symbol.getVariableValue();
|
2019-03-05 03:12:56 +08:00
|
|
|
// Target Expressions that are always inlined do not appear in the symtab
|
|
|
|
if (const auto *T = dyn_cast<MCTargetExpr>(Expr))
|
|
|
|
if (T->inlineAssignedExpr())
|
|
|
|
return false;
|
2014-03-20 10:12:01 +08:00
|
|
|
if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
|
|
|
|
if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-11-01 22:28:48 +08:00
|
|
|
|
2010-10-20 03:31:37 +08:00
|
|
|
if (Used)
|
|
|
|
return true;
|
|
|
|
|
2010-10-27 23:18:17 +08:00
|
|
|
if (Renamed)
|
|
|
|
return false;
|
|
|
|
|
2015-06-04 05:23:21 +08:00
|
|
|
if (Symbol.isVariable() && Symbol.isUndefined()) {
|
|
|
|
// FIXME: this is here just to diagnose the case of a var = commmon_sym.
|
|
|
|
Layout.getBaseSymbol(Symbol);
|
|
|
|
return false;
|
2014-04-28 21:39:57 +08:00
|
|
|
}
|
2011-02-24 04:22:07 +08:00
|
|
|
|
2015-06-04 05:23:21 +08:00
|
|
|
if (Symbol.isUndefined() && !Symbol.isBindingSet())
|
2010-10-27 22:44:52 +08:00
|
|
|
return false;
|
|
|
|
|
2010-10-20 03:31:37 +08:00
|
|
|
if (Symbol.isTemporary())
|
2010-10-06 02:01:23 +08:00
|
|
|
return false;
|
|
|
|
|
2015-06-04 23:33:30 +08:00
|
|
|
if (Symbol.getType() == ELF::STT_SECTION)
|
|
|
|
return false;
|
|
|
|
|
2010-10-06 02:01:23 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
void ELFWriter::computeSymbolTable(
|
2015-04-08 03:00:17 +08:00
|
|
|
MCAssembler &Asm, const MCAsmLayout &Layout,
|
2015-05-29 01:54:01 +08:00
|
|
|
const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
|
|
|
|
SectionOffsetsTy &SectionOffsets) {
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
MCContext &Ctx = Asm.getContext();
|
2015-05-29 01:54:01 +08:00
|
|
|
SymbolTableWriter Writer(*this, is64Bit());
|
|
|
|
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
// Symbol table
|
|
|
|
unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
|
|
|
|
MCSectionELF *SymtabSection =
|
|
|
|
Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, "");
|
[Alignment] Introduce llvm::Align to MCSection
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet, JDevlieghere
Subscribers: arsenm, sdardis, jvesely, nhaehnle, sbc100, hiraditya, aheejin, jrtc27, atanasyan, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67486
llvm-svn: 371831
2019-09-13 17:29:59 +08:00
|
|
|
SymtabSection->setAlignment(is64Bit() ? llvm::Align(8) : llvm::Align(4));
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
SymbolTableIndex = addToSectionTable(SymtabSection);
|
|
|
|
|
2015-06-06 02:21:00 +08:00
|
|
|
align(SymtabSection->getAlignment());
|
2018-05-22 02:17:42 +08:00
|
|
|
uint64_t SecStart = W.OS.tell();
|
2015-05-29 01:54:01 +08:00
|
|
|
|
|
|
|
// The first entry is the undefined symbol entry.
|
|
|
|
Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
|
|
|
|
|
2015-05-29 04:11:34 +08:00
|
|
|
std::vector<ELFSymbolData> LocalSymbolData;
|
|
|
|
std::vector<ELFSymbolData> ExternalSymbolData;
|
|
|
|
|
2010-10-15 00:34:44 +08:00
|
|
|
// Add the data for the symbols.
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
bool HasLargeSectionIndex = false;
|
2015-06-03 04:38:46 +08:00
|
|
|
for (const MCSymbol &S : Asm.symbols()) {
|
|
|
|
const auto &Symbol = cast<MCSymbolELF>(S);
|
2015-06-04 05:30:10 +08:00
|
|
|
bool Used = Symbol.isUsedInReloc();
|
2015-06-04 05:52:06 +08:00
|
|
|
bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
|
2015-06-04 05:41:59 +08:00
|
|
|
bool isSignature = Symbol.isSignature();
|
2010-11-14 12:17:37 +08:00
|
|
|
|
2015-05-20 12:39:01 +08:00
|
|
|
if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
|
2018-05-22 03:18:28 +08:00
|
|
|
OWriter.Renames.count(&Symbol)))
|
2010-08-17 02:57:57 +08:00
|
|
|
continue;
|
|
|
|
|
2015-11-17 18:00:43 +08:00
|
|
|
if (Symbol.isTemporary() && Symbol.isUndefined()) {
|
|
|
|
Ctx.reportError(SMLoc(), "Undefined temporary symbol");
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-26 04:10:45 +08:00
|
|
|
|
2010-08-17 02:57:57 +08:00
|
|
|
ELFSymbolData MSD;
|
2015-06-02 08:25:12 +08:00
|
|
|
MSD.Symbol = cast<MCSymbolELF>(&Symbol);
|
2010-08-17 02:57:57 +08:00
|
|
|
|
2015-06-04 05:41:59 +08:00
|
|
|
bool Local = Symbol.getBinding() == ELF::STB_LOCAL;
|
2015-06-26 04:10:45 +08:00
|
|
|
assert(Local || !Symbol.isTemporary());
|
|
|
|
|
2015-05-29 23:07:27 +08:00
|
|
|
if (Symbol.isAbsolute()) {
|
2014-03-24 11:43:21 +08:00
|
|
|
MSD.SectionIndex = ELF::SHN_ABS;
|
2015-05-30 01:48:04 +08:00
|
|
|
} else if (Symbol.isCommon()) {
|
AMDGPU/MC: Add .amdgpu_lds directive
Summary:
The directive defines a symbol as an group/local memory (LDS) symbol.
LDS symbols behave similar to common symbols for the purposes of ELF,
using the processor-specific SHN_AMDGPU_LDS as section index.
It is the linker and/or runtime loader's job to "instantiate" LDS symbols
and resolve relocations that reference them.
It is not possible to initialize LDS memory (not even zero-initialize
as for .bss).
We want to be able to link together objects -- starting with relocatable
objects, but possible expanding to shared objects in the future -- that
access LDS memory in a flexible way.
LDS memory is in an address space that is entirely separate from the
address space that contains the program image (code and normal data),
so having program segments for it doesn't really make sense.
Furthermore, we want to be able to compile multiple kernels in a
compilation unit which have disjoint use of LDS memory. In that case,
we may want to place LDS symbols differently for different kernels
to save memory (LDS memory is very limited and physically private to
each kernel invocation), so we can't simply place LDS symbols in a
.lds section.
Hence this solution where LDS symbols always stay undefined.
Change-Id: I08cbc37a7c0c32f53f7b6123aa0afc91dbc1748f
Reviewers: arsenm, rampitec, t-tye, b-sumner, jsjodin
Subscribers: kzhuravl, jvesely, wdng, yaxunl, dstuttard, tpr, rupprecht, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61493
llvm-svn: 364296
2019-06-25 19:51:35 +08:00
|
|
|
if (Symbol.isTargetCommon()) {
|
|
|
|
MSD.SectionIndex = Symbol.getIndex();
|
|
|
|
} else {
|
|
|
|
assert(!Local);
|
|
|
|
MSD.SectionIndex = ELF::SHN_COMMON;
|
|
|
|
}
|
2015-05-29 23:07:27 +08:00
|
|
|
} else if (Symbol.isUndefined()) {
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
if (isSignature && !Used) {
|
2015-04-29 06:59:58 +08:00
|
|
|
MSD.SectionIndex = RevGroupMap.lookup(&Symbol);
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
|
|
|
|
HasLargeSectionIndex = true;
|
|
|
|
} else {
|
2010-11-14 12:17:37 +08:00
|
|
|
MSD.SectionIndex = ELF::SHN_UNDEF;
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
}
|
2010-08-17 02:57:57 +08:00
|
|
|
} else {
|
2010-11-11 05:51:05 +08:00
|
|
|
const MCSectionELF &Section =
|
2015-05-29 23:07:27 +08:00
|
|
|
static_cast<const MCSectionELF &>(Symbol.getSection());
|
2018-12-05 18:43:58 +08:00
|
|
|
|
|
|
|
// We may end up with a situation when section symbol is technically
|
|
|
|
// defined, but should not be. That happens because we explicitly
|
|
|
|
// pre-create few .debug_* sections to have accessors.
|
|
|
|
// And if these sections were not really defined in the code, but were
|
|
|
|
// referenced, we simply error out.
|
|
|
|
if (!Section.isRegistered()) {
|
|
|
|
assert(static_cast<const MCSymbolELF &>(Symbol).getType() ==
|
|
|
|
ELF::STT_SECTION);
|
|
|
|
Ctx.reportError(SMLoc(),
|
|
|
|
"Undefined section reference: " + Symbol.getName());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:44:54 +08:00
|
|
|
if (Mode == NonDwoOnly && isDwoSection(Section))
|
|
|
|
continue;
|
2010-11-11 05:51:05 +08:00
|
|
|
MSD.SectionIndex = SectionIndexMap.lookup(&Section);
|
2010-08-17 02:57:57 +08:00
|
|
|
assert(MSD.SectionIndex && "Invalid section index!");
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
|
|
|
|
HasLargeSectionIndex = true;
|
2010-10-15 23:39:06 +08:00
|
|
|
}
|
|
|
|
|
2010-10-27 23:18:17 +08:00
|
|
|
StringRef Name = Symbol.getName();
|
2014-10-17 09:48:58 +08:00
|
|
|
|
|
|
|
// Sections have their own string table
|
2015-10-23 02:32:06 +08:00
|
|
|
if (Symbol.getType() != ELF::STT_SECTION) {
|
|
|
|
MSD.Name = Name;
|
|
|
|
StrTabBuilder.add(Name);
|
|
|
|
}
|
2010-10-27 23:18:17 +08:00
|
|
|
|
2015-05-29 22:20:40 +08:00
|
|
|
if (Local)
|
2010-10-27 22:44:52 +08:00
|
|
|
LocalSymbolData.push_back(MSD);
|
|
|
|
else
|
|
|
|
ExternalSymbolData.push_back(MSD);
|
2010-08-17 02:57:57 +08:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:59:57 +08:00
|
|
|
// This holds the .symtab_shndx section index.
|
|
|
|
unsigned SymtabShndxSectionIndex = 0;
|
|
|
|
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
if (HasLargeSectionIndex) {
|
|
|
|
MCSectionELF *SymtabShndxSection =
|
2019-04-12 10:16:15 +08:00
|
|
|
Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);
|
[Alignment] Introduce llvm::Align to MCSection
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet, JDevlieghere
Subscribers: arsenm, sdardis, jvesely, nhaehnle, sbc100, hiraditya, aheejin, jrtc27, atanasyan, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67486
llvm-svn: 371831
2019-09-13 17:29:59 +08:00
|
|
|
SymtabShndxSection->setAlignment(llvm::Align(4));
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
}
|
|
|
|
|
2015-05-29 03:29:15 +08:00
|
|
|
ArrayRef<std::string> FileNames = Asm.getFileNames();
|
|
|
|
for (const std::string &Name : FileNames)
|
2015-05-29 02:03:20 +08:00
|
|
|
StrTabBuilder.add(Name);
|
2014-05-01 00:25:02 +08:00
|
|
|
|
2015-10-24 05:48:05 +08:00
|
|
|
StrTabBuilder.finalize();
|
2014-05-01 00:25:02 +08:00
|
|
|
|
2017-03-04 05:22:06 +08:00
|
|
|
// File symbols are emitted first and handled separately from normal symbols,
|
|
|
|
// i.e. a non-STT_FILE symbol with the same name may appear.
|
2015-05-29 04:00:13 +08:00
|
|
|
for (const std::string &Name : FileNames)
|
|
|
|
Writer.writeSymbol(StrTabBuilder.getOffset(Name),
|
|
|
|
ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
|
|
|
|
ELF::SHN_ABS, true);
|
|
|
|
|
2010-08-17 02:57:57 +08:00
|
|
|
// Symbols are required to be in lexicographic order.
|
|
|
|
array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
|
|
|
|
array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
|
|
|
|
|
|
|
|
// Set the symbol indices. Local symbols must come before all other
|
|
|
|
// symbols with non-local bindings.
|
2015-05-29 03:29:15 +08:00
|
|
|
unsigned Index = FileNames.size() + 1;
|
2010-11-14 11:12:24 +08:00
|
|
|
|
2015-05-29 01:54:01 +08:00
|
|
|
for (ELFSymbolData &MSD : LocalSymbolData) {
|
2015-06-23 19:31:32 +08:00
|
|
|
unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION
|
|
|
|
? 0
|
|
|
|
: StrTabBuilder.getOffset(MSD.Name);
|
2015-05-29 01:54:01 +08:00
|
|
|
MSD.Symbol->setIndex(Index++);
|
2015-05-29 04:25:29 +08:00
|
|
|
writeSymbol(Writer, StringIndex, MSD, Layout);
|
2015-05-29 01:54:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the symbol table entries.
|
2015-05-29 04:00:13 +08:00
|
|
|
LastLocalSymbolIndex = Index;
|
2015-05-29 01:54:01 +08:00
|
|
|
|
2015-05-29 03:43:20 +08:00
|
|
|
for (ELFSymbolData &MSD : ExternalSymbolData) {
|
2015-05-29 04:25:29 +08:00
|
|
|
unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
|
2015-05-29 04:00:13 +08:00
|
|
|
MSD.Symbol->setIndex(Index++);
|
2015-05-29 04:25:29 +08:00
|
|
|
writeSymbol(Writer, StringIndex, MSD, Layout);
|
2015-06-03 04:38:46 +08:00
|
|
|
assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
|
2015-05-29 01:54:01 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
uint64_t SecEnd = W.OS.tell();
|
2015-05-29 01:54:01 +08:00
|
|
|
SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);
|
|
|
|
|
|
|
|
ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();
|
|
|
|
if (ShndxIndexes.empty()) {
|
|
|
|
assert(SymtabShndxSectionIndex == 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(SymtabShndxSectionIndex != 0);
|
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
SecStart = W.OS.tell();
|
2015-05-29 01:54:01 +08:00
|
|
|
const MCSectionELF *SymtabShndxSection =
|
|
|
|
SectionTable[SymtabShndxSectionIndex - 1];
|
|
|
|
for (uint32_t Index : ShndxIndexes)
|
|
|
|
write(Index);
|
2018-05-22 02:17:42 +08:00
|
|
|
SecEnd = W.OS.tell();
|
2015-05-29 01:54:01 +08:00
|
|
|
SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
|
2010-08-17 02:57:57 +08:00
|
|
|
}
|
|
|
|
|
2018-07-18 06:17:18 +08:00
|
|
|
void ELFWriter::writeAddrsigSection() {
|
|
|
|
for (const MCSymbol *Sym : OWriter.AddrsigSyms)
|
|
|
|
encodeULEB128(Sym->getIndex(), W.OS);
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
|
|
|
|
const MCSectionELF &Sec) {
|
|
|
|
if (OWriter.Relocations[&Sec].empty())
|
2015-04-30 22:21:49 +08:00
|
|
|
return nullptr;
|
2010-08-18 01:56:13 +08:00
|
|
|
|
2015-04-30 08:45:46 +08:00
|
|
|
const StringRef SectionName = Sec.getSectionName();
|
2015-02-18 04:31:13 +08:00
|
|
|
std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
|
|
|
|
RelaSectionName += SectionName;
|
2013-07-11 04:58:17 +08:00
|
|
|
|
2015-02-18 04:31:13 +08:00
|
|
|
unsigned EntrySize;
|
|
|
|
if (hasRelocationAddend())
|
|
|
|
EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
|
|
|
|
else
|
|
|
|
EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
|
2015-02-12 07:17:48 +08:00
|
|
|
|
2015-02-18 04:31:13 +08:00
|
|
|
unsigned Flags = 0;
|
2015-04-30 08:45:46 +08:00
|
|
|
if (Sec.getFlags() & ELF::SHF_GROUP)
|
2015-02-18 04:31:13 +08:00
|
|
|
Flags = ELF::SHF_GROUP;
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionELF *RelaSection = Ctx.createELFRelSection(
|
2015-02-18 04:31:13 +08:00
|
|
|
RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
|
2015-04-30 08:45:46 +08:00
|
|
|
Flags, EntrySize, Sec.getGroup(), &Sec);
|
[Alignment] Introduce llvm::Align to MCSection
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet, JDevlieghere
Subscribers: arsenm, sdardis, jvesely, nhaehnle, sbc100, hiraditya, aheejin, jrtc27, atanasyan, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67486
llvm-svn: 371831
2019-09-13 17:29:59 +08:00
|
|
|
RelaSection->setAlignment(is64Bit() ? llvm::Align(8) : llvm::Align(4));
|
2015-04-30 22:21:49 +08:00
|
|
|
return RelaSection;
|
2011-03-21 02:44:20 +08:00
|
|
|
}
|
|
|
|
|
2016-05-27 20:27:32 +08:00
|
|
|
// Include the debug info compression header.
|
2018-05-22 03:18:28 +08:00
|
|
|
bool ELFWriter::maybeWriteCompression(
|
2016-05-27 20:27:32 +08:00
|
|
|
uint64_t Size, SmallVectorImpl<char> &CompressedContents, bool ZLibStyle,
|
|
|
|
unsigned Alignment) {
|
|
|
|
if (ZLibStyle) {
|
|
|
|
uint64_t HdrSize =
|
|
|
|
is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
|
|
|
|
if (Size <= HdrSize + CompressedContents.size())
|
|
|
|
return false;
|
|
|
|
// Platform specific header is followed by compressed data.
|
|
|
|
if (is64Bit()) {
|
|
|
|
// Write Elf64_Chdr header.
|
|
|
|
write(static_cast<ELF::Elf64_Word>(ELF::ELFCOMPRESS_ZLIB));
|
|
|
|
write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field.
|
|
|
|
write(static_cast<ELF::Elf64_Xword>(Size));
|
|
|
|
write(static_cast<ELF::Elf64_Xword>(Alignment));
|
|
|
|
} else {
|
|
|
|
// Write Elf32_Chdr header otherwise.
|
|
|
|
write(static_cast<ELF::Elf32_Word>(ELF::ELFCOMPRESS_ZLIB));
|
|
|
|
write(static_cast<ELF::Elf32_Word>(Size));
|
|
|
|
write(static_cast<ELF::Elf32_Word>(Alignment));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
|
|
|
|
// useful for consumers to preallocate a buffer to decompress into.
|
2016-05-25 08:14:12 +08:00
|
|
|
const StringRef Magic = "ZLIB";
|
|
|
|
if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
|
|
|
|
return false;
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS << Magic;
|
|
|
|
support::endian::write(W.OS, Size, support::big);
|
2016-05-25 08:14:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
|
|
|
|
const MCAsmLayout &Layout) {
|
2015-05-27 21:30:50 +08:00
|
|
|
MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
|
2015-05-01 05:51:58 +08:00
|
|
|
StringRef SectionName = Section.getSectionName();
|
2014-04-11 05:53:53 +08:00
|
|
|
|
2017-06-09 08:40:19 +08:00
|
|
|
auto &MC = Asm.getContext();
|
|
|
|
const auto &MAI = MC.getAsmInfo();
|
|
|
|
|
2015-05-01 05:51:58 +08:00
|
|
|
// Compressing debug_frame requires handling alignment fragments which is
|
|
|
|
// more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
|
|
|
|
// for writing to arbitrary buffers) for little benefit.
|
2016-05-27 20:27:32 +08:00
|
|
|
bool CompressionEnabled =
|
2017-06-09 08:40:19 +08:00
|
|
|
MAI->compressDebugSections() != DebugCompressionType::None;
|
2016-05-27 20:27:32 +08:00
|
|
|
if (!CompressionEnabled || !SectionName.startswith(".debug_") ||
|
|
|
|
SectionName == ".debug_frame") {
|
2018-05-22 02:17:42 +08:00
|
|
|
Asm.writeSectionData(W.OS, &Section, Layout);
|
2015-05-01 05:51:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-09 08:40:19 +08:00
|
|
|
assert((MAI->compressDebugSections() == DebugCompressionType::Z ||
|
|
|
|
MAI->compressDebugSections() == DebugCompressionType::GNU) &&
|
|
|
|
"expected zlib or zlib-gnu style compression");
|
|
|
|
|
2015-09-02 00:19:03 +08:00
|
|
|
SmallVector<char, 128> UncompressedData;
|
|
|
|
raw_svector_ostream VecOS(UncompressedData);
|
2018-05-22 02:11:35 +08:00
|
|
|
Asm.writeSectionData(VecOS, &Section, Layout);
|
2014-04-11 05:53:53 +08:00
|
|
|
|
2015-05-01 05:51:58 +08:00
|
|
|
SmallVector<char, 128> CompressedContents;
|
2017-01-17 23:45:07 +08:00
|
|
|
if (Error E = zlib::compress(
|
|
|
|
StringRef(UncompressedData.data(), UncompressedData.size()),
|
|
|
|
CompressedContents)) {
|
|
|
|
consumeError(std::move(E));
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS << UncompressedData;
|
2015-05-01 05:51:58 +08:00
|
|
|
return;
|
2014-04-19 05:24:12 +08:00
|
|
|
}
|
|
|
|
|
2017-06-09 08:40:19 +08:00
|
|
|
bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z;
|
2016-05-27 20:27:32 +08:00
|
|
|
if (!maybeWriteCompression(UncompressedData.size(), CompressedContents,
|
|
|
|
ZlibStyle, Sec.getAlignment())) {
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS << UncompressedData;
|
2014-04-11 05:53:53 +08:00
|
|
|
return;
|
2015-05-01 05:51:58 +08:00
|
|
|
}
|
2016-05-27 20:27:32 +08:00
|
|
|
|
2019-04-23 17:16:53 +08:00
|
|
|
if (ZlibStyle) {
|
2016-05-27 20:27:32 +08:00
|
|
|
// Set the compressed flag. That is zlib style.
|
|
|
|
Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED);
|
2019-04-23 17:16:53 +08:00
|
|
|
// Alignment field should reflect the requirements of
|
|
|
|
// the compressed section header.
|
[Alignment] Introduce llvm::Align to MCSection
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet, JDevlieghere
Subscribers: arsenm, sdardis, jvesely, nhaehnle, sbc100, hiraditya, aheejin, jrtc27, atanasyan, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67486
llvm-svn: 371831
2019-09-13 17:29:59 +08:00
|
|
|
Section.setAlignment(is64Bit() ? llvm::Align(8) : llvm::Align(4));
|
2019-04-23 17:16:53 +08:00
|
|
|
} else {
|
2016-05-27 20:27:32 +08:00
|
|
|
// Add "z" prefix to section name. This is zlib-gnu style.
|
2017-06-09 08:40:19 +08:00
|
|
|
MC.renameELFSection(&Section, (".z" + SectionName.drop_front(1)).str());
|
2019-04-23 17:16:53 +08:00
|
|
|
}
|
2018-05-22 02:17:42 +08:00
|
|
|
W.OS << CompressedContents;
|
2014-04-11 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
|
|
|
|
uint64_t Address, uint64_t Offset,
|
|
|
|
uint64_t Size, uint32_t Link, uint32_t Info,
|
|
|
|
uint64_t Alignment, uint64_t EntrySize) {
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint32_t>(Name); // sh_name: index into string table
|
|
|
|
W.write<uint32_t>(Type); // sh_type
|
2010-08-17 02:57:57 +08:00
|
|
|
WriteWord(Flags); // sh_flags
|
|
|
|
WriteWord(Address); // sh_addr
|
|
|
|
WriteWord(Offset); // sh_offset
|
|
|
|
WriteWord(Size); // sh_size
|
2018-05-22 02:17:42 +08:00
|
|
|
W.write<uint32_t>(Link); // sh_link
|
|
|
|
W.write<uint32_t>(Info); // sh_info
|
2010-08-17 02:57:57 +08:00
|
|
|
WriteWord(Alignment); // sh_addralign
|
|
|
|
WriteWord(EntrySize); // sh_entsize
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
void ELFWriter::writeRelocations(const MCAssembler &Asm,
|
2015-04-30 08:45:46 +08:00
|
|
|
const MCSectionELF &Sec) {
|
2018-05-22 03:18:28 +08:00
|
|
|
std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec];
|
2012-03-24 07:06:45 +08:00
|
|
|
|
2015-12-18 00:22:06 +08:00
|
|
|
// We record relocations by pushing to the end of a vector. Reverse the vector
|
|
|
|
// to get the relocations in the order they were created.
|
|
|
|
// In most cases that is not important, but it can be for special sections
|
|
|
|
// (.eh_frame) or specific relocations (TLS optimizations on SystemZ).
|
|
|
|
std::reverse(Relocs.begin(), Relocs.end());
|
2015-12-17 23:08:24 +08:00
|
|
|
|
2015-12-18 00:22:06 +08:00
|
|
|
// Sort the relocation entries. MIPS needs this.
|
2018-05-22 03:18:28 +08:00
|
|
|
OWriter.TargetObjectWriter->sortRelocs(Asm, Relocs);
|
2010-08-17 02:57:57 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
|
2014-03-29 14:26:49 +08:00
|
|
|
const ELFRelocationEntry &Entry = Relocs[e - i - 1];
|
2015-05-29 04:53:09 +08:00
|
|
|
unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
|
2010-08-17 02:57:57 +08:00
|
|
|
|
2010-12-18 11:27:34 +08:00
|
|
|
if (is64Bit()) {
|
2015-04-30 08:30:40 +08:00
|
|
|
write(Entry.Offset);
|
2018-05-22 03:18:28 +08:00
|
|
|
if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
|
2015-04-30 08:30:40 +08:00
|
|
|
write(uint32_t(Index));
|
2010-09-10 02:01:29 +08:00
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
write(OWriter.TargetObjectWriter->getRSsym(Entry.Type));
|
|
|
|
write(OWriter.TargetObjectWriter->getRType3(Entry.Type));
|
|
|
|
write(OWriter.TargetObjectWriter->getRType2(Entry.Type));
|
|
|
|
write(OWriter.TargetObjectWriter->getRType(Entry.Type));
|
2014-03-29 14:26:49 +08:00
|
|
|
} else {
|
2012-06-28 06:28:30 +08:00
|
|
|
struct ELF::Elf64_Rela ERE64;
|
2014-03-29 14:26:49 +08:00
|
|
|
ERE64.setSymbolAndType(Index, Entry.Type);
|
2015-04-30 08:30:40 +08:00
|
|
|
write(ERE64.r_info);
|
2012-06-28 06:28:30 +08:00
|
|
|
}
|
2010-12-18 11:27:34 +08:00
|
|
|
if (hasRelocationAddend())
|
2015-04-30 08:30:40 +08:00
|
|
|
write(Entry.Addend);
|
2010-09-10 02:01:29 +08:00
|
|
|
} else {
|
2015-04-30 08:30:40 +08:00
|
|
|
write(uint32_t(Entry.Offset));
|
2010-09-10 02:01:29 +08:00
|
|
|
|
2010-10-05 23:11:03 +08:00
|
|
|
struct ELF::Elf32_Rela ERE32;
|
2014-03-29 14:26:49 +08:00
|
|
|
ERE32.setSymbolAndType(Index, Entry.Type);
|
2015-04-30 08:30:40 +08:00
|
|
|
write(ERE32.r_info);
|
2010-09-10 02:01:29 +08:00
|
|
|
|
2010-12-18 11:27:34 +08:00
|
|
|
if (hasRelocationAddend())
|
2015-04-30 08:30:40 +08:00
|
|
|
write(uint32_t(Entry.Addend));
|
2017-09-21 22:04:53 +08:00
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
|
|
|
|
if (uint32_t RType =
|
|
|
|
OWriter.TargetObjectWriter->getRType2(Entry.Type)) {
|
2017-09-21 22:04:53 +08:00
|
|
|
write(uint32_t(Entry.Offset));
|
|
|
|
|
|
|
|
ERE32.setSymbolAndType(0, RType);
|
|
|
|
write(ERE32.r_info);
|
|
|
|
write(uint32_t(0));
|
|
|
|
}
|
2018-05-22 03:18:28 +08:00
|
|
|
if (uint32_t RType =
|
|
|
|
OWriter.TargetObjectWriter->getRType3(Entry.Type)) {
|
2017-09-21 22:04:53 +08:00
|
|
|
write(uint32_t(Entry.Offset));
|
|
|
|
|
|
|
|
ERE32.setSymbolAndType(0, RType);
|
|
|
|
write(ERE32.r_info);
|
|
|
|
write(uint32_t(0));
|
|
|
|
}
|
|
|
|
}
|
2010-09-10 02:01:29 +08:00
|
|
|
}
|
2010-08-17 02:57:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
const MCSectionELF *ELFWriter::createStringTable(MCContext &Ctx) {
|
2015-05-26 05:56:55 +08:00
|
|
|
const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1];
|
2018-05-22 02:17:42 +08:00
|
|
|
StrTabBuilder.write(W.OS);
|
2015-04-30 22:21:49 +08:00
|
|
|
return StrtabSection;
|
2010-11-12 02:13:52 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
|
|
|
|
uint32_t GroupSymbolIndex, uint64_t Offset,
|
|
|
|
uint64_t Size, const MCSectionELF &Section) {
|
2010-11-11 07:36:59 +08:00
|
|
|
uint64_t sh_link = 0;
|
|
|
|
uint64_t sh_info = 0;
|
|
|
|
|
|
|
|
switch(Section.getType()) {
|
2015-04-06 11:16:51 +08:00
|
|
|
default:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
2010-11-11 07:36:59 +08:00
|
|
|
case ELF::SHT_DYNAMIC:
|
2015-05-01 05:20:06 +08:00
|
|
|
llvm_unreachable("SHT_DYNAMIC in a relocatable object");
|
2010-11-11 07:36:59 +08:00
|
|
|
|
|
|
|
case ELF::SHT_REL:
|
|
|
|
case ELF::SHT_RELA: {
|
2015-02-18 04:37:50 +08:00
|
|
|
sh_link = SymbolTableIndex;
|
2010-11-11 07:36:59 +08:00
|
|
|
assert(sh_link && ".symtab not found");
|
2017-03-15 03:28:51 +08:00
|
|
|
const MCSection *InfoSection = Section.getAssociatedSection();
|
|
|
|
sh_info = SectionIndexMap.lookup(cast<MCSectionELF>(InfoSection));
|
2010-11-11 07:36:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ELF::SHT_SYMTAB:
|
|
|
|
sh_link = StringTableIndex;
|
|
|
|
sh_info = LastLocalSymbolIndex;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ELF::SHT_SYMTAB_SHNDX:
|
[MC] Add assembler support for .cg_profile.
Object FIle Representation
At codegen time this is emitted into the ELF file a pair of symbol indices and a weight. In assembly it looks like:
.cg_profile a, b, 32
.cg_profile freq, a, 11
.cg_profile freq, b, 20
When writing an ELF file these are put into a SHT_LLVM_CALL_GRAPH_PROFILE (0x6fff4c02) section as (uint32_t, uint32_t, uint64_t) tuples as (from symbol index, to symbol index, weight).
Differential Revision: https://reviews.llvm.org/D44965
llvm-svn: 333823
2018-06-03 00:33:01 +08:00
|
|
|
case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
|
2018-07-18 06:17:18 +08:00
|
|
|
case ELF::SHT_LLVM_ADDRSIG:
|
2010-11-11 07:36:59 +08:00
|
|
|
sh_link = SymbolTableIndex;
|
|
|
|
break;
|
|
|
|
|
2011-10-08 07:28:32 +08:00
|
|
|
case ELF::SHT_GROUP:
|
2010-11-12 02:13:52 +08:00
|
|
|
sh_link = SymbolTableIndex;
|
|
|
|
sh_info = GroupSymbolIndex;
|
|
|
|
break;
|
2010-11-11 07:36:59 +08:00
|
|
|
}
|
|
|
|
|
2017-03-15 03:28:51 +08:00
|
|
|
if (Section.getFlags() & ELF::SHF_LINK_ORDER) {
|
|
|
|
const MCSymbol *Sym = Section.getAssociatedSymbol();
|
|
|
|
const MCSectionELF *Sec = cast<MCSectionELF>(&Sym->getSection());
|
|
|
|
sh_link = SectionIndexMap.lookup(Sec);
|
|
|
|
}
|
2013-02-05 22:18:59 +08:00
|
|
|
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()),
|
2015-05-22 03:36:43 +08:00
|
|
|
Section.getType(), Section.getFlags(), 0, Offset, Size,
|
|
|
|
sh_link, sh_info, Section.getAlignment(),
|
|
|
|
Section.getEntrySize());
|
2010-11-11 07:36:59 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
void ELFWriter::writeSectionHeader(
|
2015-06-05 04:55:49 +08:00
|
|
|
const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
|
2015-04-28 23:26:21 +08:00
|
|
|
const SectionOffsetsTy &SectionOffsets) {
|
2015-05-01 05:10:06 +08:00
|
|
|
const unsigned NumSections = SectionTable.size();
|
2011-03-21 02:44:20 +08:00
|
|
|
|
|
|
|
// Null section first.
|
|
|
|
uint64_t FirstSectionSize =
|
2015-04-15 21:07:47 +08:00
|
|
|
(NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
|
2015-04-30 04:25:24 +08:00
|
|
|
WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
|
2011-03-21 02:44:20 +08:00
|
|
|
|
2015-05-26 05:56:55 +08:00
|
|
|
for (const MCSectionELF *Section : SectionTable) {
|
2011-03-21 02:44:20 +08:00
|
|
|
uint32_t GroupSymbolIndex;
|
2015-05-01 04:53:27 +08:00
|
|
|
unsigned Type = Section->getType();
|
|
|
|
if (Type != ELF::SHT_GROUP)
|
2011-03-21 02:44:20 +08:00
|
|
|
GroupSymbolIndex = 0;
|
|
|
|
else
|
2015-05-29 04:53:09 +08:00
|
|
|
GroupSymbolIndex = Section->getGroup()->getIndex();
|
2011-03-21 02:44:20 +08:00
|
|
|
|
2015-04-30 22:21:49 +08:00
|
|
|
const std::pair<uint64_t, uint64_t> &Offsets =
|
2015-05-01 04:53:27 +08:00
|
|
|
SectionOffsets.find(Section)->second;
|
2015-05-22 03:46:39 +08:00
|
|
|
uint64_t Size;
|
2015-05-26 10:00:36 +08:00
|
|
|
if (Type == ELF::SHT_NOBITS)
|
|
|
|
Size = Layout.getSectionAddressSize(Section);
|
|
|
|
else
|
2015-05-22 03:46:39 +08:00
|
|
|
Size = Offsets.second - Offsets.first;
|
2011-03-21 02:44:20 +08:00
|
|
|
|
2015-05-22 03:42:35 +08:00
|
|
|
writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
|
2015-05-22 03:36:43 +08:00
|
|
|
*Section);
|
2011-03-21 02:44:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
|
2018-05-22 02:23:50 +08:00
|
|
|
uint64_t StartOffset = W.OS.tell();
|
|
|
|
|
2015-04-30 22:21:49 +08:00
|
|
|
MCContext &Ctx = Asm.getContext();
|
Produce a single string table in a ELF .o
Normally an ELF .o has two string tables, one for symbols, one for section
names.
With the scheme of naming sections like ".text.foo" where foo is a symbol,
there is a big potential saving in using a single one.
Building llvm+clang+lld with master and with this patch the results were:
master: 193,267,008 bytes
patch: 186,107,952 bytes
master non unique section names: 183,260,192 bytes
patch non unique section names: 183,118,632 bytes
So using non usique saves 10,006,816 bytes, and the patch saves 7,159,056 while
still using distinct names for the sections.
llvm-svn: 238073
2015-05-23 07:58:30 +08:00
|
|
|
MCSectionELF *StrtabSection =
|
|
|
|
Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
|
|
|
|
StringTableIndex = addToSectionTable(StrtabSection);
|
2011-03-21 02:44:20 +08:00
|
|
|
|
2015-04-30 22:21:49 +08:00
|
|
|
RevGroupMapTy RevGroupMap;
|
|
|
|
SectionIndexMapTy SectionIndexMap;
|
2011-03-21 02:44:20 +08:00
|
|
|
|
2015-04-30 22:21:49 +08:00
|
|
|
std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
|
2015-04-08 19:41:24 +08:00
|
|
|
|
2015-04-15 06:54:16 +08:00
|
|
|
// Write out the ELF header ...
|
2015-04-30 04:39:37 +08:00
|
|
|
writeHeader(Asm);
|
2015-04-15 06:54:16 +08:00
|
|
|
|
|
|
|
// ... then the sections ...
|
2015-04-30 22:21:49 +08:00
|
|
|
SectionOffsetsTy SectionOffsets;
|
2015-05-22 04:43:13 +08:00
|
|
|
std::vector<MCSectionELF *> Groups;
|
|
|
|
std::vector<MCSectionELF *> Relocations;
|
2015-05-27 21:30:50 +08:00
|
|
|
for (MCSection &Sec : Asm) {
|
|
|
|
MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
|
2018-05-22 03:44:54 +08:00
|
|
|
if (Mode == NonDwoOnly && isDwoSection(Section))
|
|
|
|
continue;
|
|
|
|
if (Mode == DwoOnly && !isDwoSection(Section))
|
|
|
|
continue;
|
2015-04-30 22:21:49 +08:00
|
|
|
|
2015-06-06 02:21:00 +08:00
|
|
|
align(Section.getAlignment());
|
2010-09-07 00:11:52 +08:00
|
|
|
|
2011-03-21 02:44:20 +08:00
|
|
|
// Remember the offset into the file for this section.
|
2018-05-22 02:17:42 +08:00
|
|
|
uint64_t SecStart = W.OS.tell();
|
2015-04-30 08:30:40 +08:00
|
|
|
|
2015-06-03 05:30:13 +08:00
|
|
|
const MCSymbolELF *SignatureSymbol = Section.getGroup();
|
2015-05-27 21:30:50 +08:00
|
|
|
writeSectionData(Asm, Section, Layout);
|
2015-04-30 08:30:40 +08:00
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
uint64_t SecEnd = W.OS.tell();
|
2015-04-30 22:21:49 +08:00
|
|
|
SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
|
|
|
|
|
2015-05-22 04:43:13 +08:00
|
|
|
MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
|
2015-04-30 22:21:49 +08:00
|
|
|
|
|
|
|
if (SignatureSymbol) {
|
2015-05-30 04:21:02 +08:00
|
|
|
Asm.registerSymbol(*SignatureSymbol);
|
2015-04-30 22:21:49 +08:00
|
|
|
unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
|
|
|
|
if (!GroupIdx) {
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionELF *Group = Ctx.createELFGroupSection(SignatureSymbol);
|
2015-05-01 04:53:27 +08:00
|
|
|
GroupIdx = addToSectionTable(Group);
|
[Alignment] Introduce llvm::Align to MCSection
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet, JDevlieghere
Subscribers: arsenm, sdardis, jvesely, nhaehnle, sbc100, hiraditya, aheejin, jrtc27, atanasyan, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67486
llvm-svn: 371831
2019-09-13 17:29:59 +08:00
|
|
|
Group->setAlignment(llvm::Align(4));
|
2015-05-22 04:43:13 +08:00
|
|
|
Groups.push_back(Group);
|
2015-04-30 22:21:49 +08:00
|
|
|
}
|
2015-06-06 01:54:25 +08:00
|
|
|
std::vector<const MCSectionELF *> &Members =
|
|
|
|
GroupMembers[SignatureSymbol];
|
|
|
|
Members.push_back(&Section);
|
2015-04-30 22:21:49 +08:00
|
|
|
if (RelSection)
|
2015-06-06 01:54:25 +08:00
|
|
|
Members.push_back(RelSection);
|
2015-04-30 22:21:49 +08:00
|
|
|
}
|
|
|
|
|
2015-05-01 04:53:27 +08:00
|
|
|
SectionIndexMap[&Section] = addToSectionTable(&Section);
|
2015-05-22 04:43:13 +08:00
|
|
|
if (RelSection) {
|
2015-05-01 04:53:27 +08:00
|
|
|
SectionIndexMap[RelSection] = addToSectionTable(RelSection);
|
2015-05-22 04:43:13 +08:00
|
|
|
Relocations.push_back(RelSection);
|
|
|
|
}
|
2018-09-07 06:09:31 +08:00
|
|
|
|
|
|
|
OWriter.TargetObjectWriter->addTargetSectionFlags(Ctx, Section);
|
2015-05-22 04:43:13 +08:00
|
|
|
}
|
|
|
|
|
[MC] Add assembler support for .cg_profile.
Object FIle Representation
At codegen time this is emitted into the ELF file a pair of symbol indices and a weight. In assembly it looks like:
.cg_profile a, b, 32
.cg_profile freq, a, 11
.cg_profile freq, b, 20
When writing an ELF file these are put into a SHT_LLVM_CALL_GRAPH_PROFILE (0x6fff4c02) section as (uint32_t, uint32_t, uint64_t) tuples as (from symbol index, to symbol index, weight).
Differential Revision: https://reviews.llvm.org/D44965
llvm-svn: 333823
2018-06-03 00:33:01 +08:00
|
|
|
MCSectionELF *CGProfileSection = nullptr;
|
|
|
|
if (!Asm.CGProfile.empty()) {
|
|
|
|
CGProfileSection = Ctx.getELFSection(".llvm.call-graph-profile",
|
|
|
|
ELF::SHT_LLVM_CALL_GRAPH_PROFILE,
|
|
|
|
ELF::SHF_EXCLUDE, 16, "");
|
|
|
|
SectionIndexMap[CGProfileSection] = addToSectionTable(CGProfileSection);
|
|
|
|
}
|
|
|
|
|
2015-05-22 04:43:13 +08:00
|
|
|
for (MCSectionELF *Group : Groups) {
|
2015-06-06 02:21:00 +08:00
|
|
|
align(Group->getAlignment());
|
2015-05-22 04:43:13 +08:00
|
|
|
|
|
|
|
// Remember the offset into the file for this section.
|
2018-05-22 02:17:42 +08:00
|
|
|
uint64_t SecStart = W.OS.tell();
|
2015-05-22 04:43:13 +08:00
|
|
|
|
|
|
|
const MCSymbol *SignatureSymbol = Group->getGroup();
|
|
|
|
assert(SignatureSymbol);
|
|
|
|
write(uint32_t(ELF::GRP_COMDAT));
|
|
|
|
for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
|
|
|
|
uint32_t SecIndex = SectionIndexMap.lookup(Member);
|
|
|
|
write(SecIndex);
|
|
|
|
}
|
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
uint64_t SecEnd = W.OS.tell();
|
2015-05-22 04:43:13 +08:00
|
|
|
SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
|
2015-04-30 22:21:49 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 03:44:54 +08:00
|
|
|
if (Mode == DwoOnly) {
|
|
|
|
// dwo files don't have symbol tables or relocations, but they do have
|
|
|
|
// string tables.
|
|
|
|
StrTabBuilder.finalize();
|
|
|
|
} else {
|
2018-07-18 06:17:18 +08:00
|
|
|
MCSectionELF *AddrsigSection;
|
|
|
|
if (OWriter.EmitAddrsigSection) {
|
|
|
|
AddrsigSection = Ctx.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG,
|
|
|
|
ELF::SHF_EXCLUDE);
|
|
|
|
addToSectionTable(AddrsigSection);
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:44:54 +08:00
|
|
|
// Compute symbol table information.
|
|
|
|
computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
|
|
|
|
SectionOffsets);
|
2015-05-22 04:43:13 +08:00
|
|
|
|
2018-05-22 03:44:54 +08:00
|
|
|
for (MCSectionELF *RelSection : Relocations) {
|
|
|
|
align(RelSection->getAlignment());
|
2015-05-22 04:43:13 +08:00
|
|
|
|
2018-05-22 03:44:54 +08:00
|
|
|
// Remember the offset into the file for this section.
|
|
|
|
uint64_t SecStart = W.OS.tell();
|
2015-05-22 04:43:13 +08:00
|
|
|
|
2018-05-22 03:44:54 +08:00
|
|
|
writeRelocations(Asm,
|
|
|
|
cast<MCSectionELF>(*RelSection->getAssociatedSection()));
|
2015-05-22 04:43:13 +08:00
|
|
|
|
2018-05-22 03:44:54 +08:00
|
|
|
uint64_t SecEnd = W.OS.tell();
|
|
|
|
SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
|
|
|
|
}
|
2018-07-18 06:17:18 +08:00
|
|
|
|
|
|
|
if (OWriter.EmitAddrsigSection) {
|
|
|
|
uint64_t SecStart = W.OS.tell();
|
|
|
|
writeAddrsigSection();
|
|
|
|
uint64_t SecEnd = W.OS.tell();
|
|
|
|
SectionOffsets[AddrsigSection] = std::make_pair(SecStart, SecEnd);
|
|
|
|
}
|
2010-08-17 02:57:57 +08:00
|
|
|
}
|
|
|
|
|
[MC] Add assembler support for .cg_profile.
Object FIle Representation
At codegen time this is emitted into the ELF file a pair of symbol indices and a weight. In assembly it looks like:
.cg_profile a, b, 32
.cg_profile freq, a, 11
.cg_profile freq, b, 20
When writing an ELF file these are put into a SHT_LLVM_CALL_GRAPH_PROFILE (0x6fff4c02) section as (uint32_t, uint32_t, uint64_t) tuples as (from symbol index, to symbol index, weight).
Differential Revision: https://reviews.llvm.org/D44965
llvm-svn: 333823
2018-06-03 00:33:01 +08:00
|
|
|
if (CGProfileSection) {
|
|
|
|
uint64_t SecStart = W.OS.tell();
|
|
|
|
for (const MCAssembler::CGProfileEntry &CGPE : Asm.CGProfile) {
|
|
|
|
W.write<uint32_t>(CGPE.From->getSymbol().getIndex());
|
|
|
|
W.write<uint32_t>(CGPE.To->getSymbol().getIndex());
|
|
|
|
W.write<uint64_t>(CGPE.Count);
|
|
|
|
}
|
|
|
|
uint64_t SecEnd = W.OS.tell();
|
|
|
|
SectionOffsets[CGProfileSection] = std::make_pair(SecStart, SecEnd);
|
|
|
|
}
|
|
|
|
|
2015-04-30 04:34:31 +08:00
|
|
|
{
|
2018-05-22 02:17:42 +08:00
|
|
|
uint64_t SecStart = W.OS.tell();
|
2015-05-01 05:10:06 +08:00
|
|
|
const MCSectionELF *Sec = createStringTable(Ctx);
|
2018-05-22 02:17:42 +08:00
|
|
|
uint64_t SecEnd = W.OS.tell();
|
2015-04-30 22:21:49 +08:00
|
|
|
SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);
|
2015-04-30 04:34:31 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 06:54:16 +08:00
|
|
|
uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
|
2015-06-06 02:21:00 +08:00
|
|
|
align(NaturalAlignment);
|
2010-09-07 00:11:52 +08:00
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
const uint64_t SectionHeaderOffset = W.OS.tell();
|
2015-04-15 06:54:16 +08:00
|
|
|
|
2011-03-21 02:44:20 +08:00
|
|
|
// ... then the section header table ...
|
2015-06-05 04:55:49 +08:00
|
|
|
writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
|
2015-04-15 06:54:16 +08:00
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
uint16_t NumSections = support::endian::byte_swap<uint16_t>(
|
|
|
|
(SectionTable.size() + 1 >= ELF::SHN_LORESERVE) ? (uint16_t)ELF::SHN_UNDEF
|
|
|
|
: SectionTable.size() + 1,
|
|
|
|
W.Endian);
|
2015-04-30 04:39:37 +08:00
|
|
|
unsigned NumSectionsOffset;
|
|
|
|
|
2018-05-22 02:17:42 +08:00
|
|
|
auto &Stream = static_cast<raw_pwrite_stream &>(W.OS);
|
2015-04-15 06:54:16 +08:00
|
|
|
if (is64Bit()) {
|
2018-05-22 02:17:42 +08:00
|
|
|
uint64_t Val =
|
|
|
|
support::endian::byte_swap<uint64_t>(SectionHeaderOffset, W.Endian);
|
|
|
|
Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
|
|
|
|
offsetof(ELF::Elf64_Ehdr, e_shoff));
|
2015-04-30 04:39:37 +08:00
|
|
|
NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
|
2015-04-15 06:54:16 +08:00
|
|
|
} else {
|
2018-05-22 02:17:42 +08:00
|
|
|
uint32_t Val =
|
|
|
|
support::endian::byte_swap<uint32_t>(SectionHeaderOffset, W.Endian);
|
|
|
|
Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
|
|
|
|
offsetof(ELF::Elf32_Ehdr, e_shoff));
|
2015-04-30 04:39:37 +08:00
|
|
|
NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
|
2015-04-15 06:54:16 +08:00
|
|
|
}
|
2018-05-22 02:17:42 +08:00
|
|
|
Stream.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections),
|
|
|
|
NumSectionsOffset);
|
2018-05-22 02:23:50 +08:00
|
|
|
|
|
|
|
return W.OS.tell() - StartOffset;
|
2010-08-17 02:57:57 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
bool ELFObjectWriter::hasRelocationAddend() const {
|
|
|
|
return TargetObjectWriter->hasRelocationAddend();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout) {
|
|
|
|
// The presence of symbol versions causes undefined symbols and
|
|
|
|
// versions declared with @@@ to be renamed.
|
|
|
|
for (const std::pair<StringRef, const MCSymbol *> &P : Asm.Symvers) {
|
|
|
|
StringRef AliasName = P.first;
|
|
|
|
const auto &Symbol = cast<MCSymbolELF>(*P.second);
|
|
|
|
size_t Pos = AliasName.find('@');
|
|
|
|
assert(Pos != StringRef::npos);
|
|
|
|
|
|
|
|
StringRef Prefix = AliasName.substr(0, Pos);
|
|
|
|
StringRef Rest = AliasName.substr(Pos);
|
|
|
|
StringRef Tail = Rest;
|
|
|
|
if (Rest.startswith("@@@"))
|
|
|
|
Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1);
|
|
|
|
|
|
|
|
auto *Alias =
|
|
|
|
cast<MCSymbolELF>(Asm.getContext().getOrCreateSymbol(Prefix + Tail));
|
|
|
|
Asm.registerSymbol(*Alias);
|
|
|
|
const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm.getContext());
|
|
|
|
Alias->setVariableValue(Value);
|
|
|
|
|
|
|
|
// Aliases defined with .symvar copy the binding from the symbol they alias.
|
|
|
|
// This is the first place we are able to copy this information.
|
|
|
|
Alias->setExternal(Symbol.isExternal());
|
|
|
|
Alias->setBinding(Symbol.getBinding());
|
2019-05-11 01:09:25 +08:00
|
|
|
Alias->setOther(Symbol.getOther());
|
2018-05-22 03:18:28 +08:00
|
|
|
|
2019-08-24 02:01:13 +08:00
|
|
|
if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
|
2018-05-22 03:18:28 +08:00
|
|
|
continue;
|
|
|
|
|
2019-02-13 09:39:32 +08:00
|
|
|
// FIXME: Get source locations for these errors or diagnose them earlier.
|
2018-05-22 03:18:28 +08:00
|
|
|
if (Symbol.isUndefined() && Rest.startswith("@@") &&
|
2019-02-13 09:39:32 +08:00
|
|
|
!Rest.startswith("@@@")) {
|
|
|
|
Asm.getContext().reportError(SMLoc(), "versioned symbol " + AliasName +
|
|
|
|
" must be defined");
|
|
|
|
continue;
|
|
|
|
}
|
2019-08-24 02:01:13 +08:00
|
|
|
|
|
|
|
if (Renames.count(&Symbol) && Renames[&Symbol] != Alias) {
|
|
|
|
Asm.getContext().reportError(
|
|
|
|
SMLoc(), llvm::Twine("multiple symbol versions defined for ") +
|
|
|
|
Symbol.getName());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Renames.insert(std::make_pair(&Symbol, Alias));
|
2018-05-22 03:18:28 +08:00
|
|
|
}
|
2018-07-18 06:17:18 +08:00
|
|
|
|
|
|
|
for (const MCSymbol *&Sym : AddrsigSyms) {
|
|
|
|
if (const MCSymbol *R = Renames.lookup(cast<MCSymbolELF>(Sym)))
|
|
|
|
Sym = R;
|
2018-08-07 05:59:58 +08:00
|
|
|
if (Sym->isInSection() && Sym->getName().startswith(".L"))
|
|
|
|
Sym = Sym->getSection().getBeginSymbol();
|
2018-07-18 06:17:18 +08:00
|
|
|
Sym->setUsedInReloc();
|
|
|
|
}
|
2018-05-22 03:18:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// It is always valid to create a relocation with a symbol. It is preferable
|
|
|
|
// to use a relocation with a section if that is possible. Using the section
|
|
|
|
// allows us to omit some local symbols from the symbol table.
|
|
|
|
bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
|
|
|
|
const MCSymbolRefExpr *RefA,
|
|
|
|
const MCSymbolELF *Sym,
|
|
|
|
uint64_t C,
|
|
|
|
unsigned Type) const {
|
|
|
|
// A PCRel relocation to an absolute value has no symbol (or section). We
|
|
|
|
// represent that with a relocation to a null section.
|
|
|
|
if (!RefA)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
|
|
|
|
switch (Kind) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
// The .odp creation emits a relocation against the symbol ".TOC." which
|
|
|
|
// create a R_PPC64_TOC relocation. However the relocation symbol name
|
|
|
|
// in final object creation should be NULL, since the symbol does not
|
|
|
|
// really exist, it is just the reference to TOC base for the current
|
|
|
|
// object file. Since the symbol is undefined, returning false results
|
|
|
|
// in a relocation with a null section which is the desired result.
|
|
|
|
case MCSymbolRefExpr::VK_PPC_TOCBASE:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// These VariantKind cause the relocation to refer to something other than
|
|
|
|
// the symbol itself, like a linker generated table. Since the address of
|
|
|
|
// symbol is not relevant, we cannot replace the symbol with the
|
|
|
|
// section and patch the difference in the addend.
|
|
|
|
case MCSymbolRefExpr::VK_GOT:
|
|
|
|
case MCSymbolRefExpr::VK_PLT:
|
|
|
|
case MCSymbolRefExpr::VK_GOTPCREL:
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_LO:
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_HI:
|
|
|
|
case MCSymbolRefExpr::VK_PPC_GOT_HA:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// An undefined symbol is not in any section, so the relocation has to point
|
|
|
|
// to the symbol itself.
|
|
|
|
assert(Sym && "Expected a symbol");
|
|
|
|
if (Sym->isUndefined())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
unsigned Binding = Sym->getBinding();
|
|
|
|
switch(Binding) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid Binding");
|
|
|
|
case ELF::STB_LOCAL:
|
|
|
|
break;
|
|
|
|
case ELF::STB_WEAK:
|
|
|
|
// If the symbol is weak, it might be overridden by a symbol in another
|
|
|
|
// file. The relocation has to point to the symbol so that the linker
|
|
|
|
// can update it.
|
|
|
|
return true;
|
|
|
|
case ELF::STB_GLOBAL:
|
|
|
|
// Global ELF symbols can be preempted by the dynamic linker. The relocation
|
|
|
|
// has to point to the symbol for a reason analogous to the STB_WEAK case.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-07 11:47:22 +08:00
|
|
|
// Keep symbol type for a local ifunc because it may result in an IRELATIVE
|
|
|
|
// reloc that the dynamic loader will use to resolve the address at startup
|
|
|
|
// time.
|
|
|
|
if (Sym->getType() == ELF::STT_GNU_IFUNC)
|
|
|
|
return true;
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
// If a relocation points to a mergeable section, we have to be careful.
|
|
|
|
// If the offset is zero, a relocation with the section will encode the
|
|
|
|
// same information. With a non-zero offset, the situation is different.
|
|
|
|
// For example, a relocation can point 42 bytes past the end of a string.
|
|
|
|
// If we change such a relocation to use the section, the linker would think
|
|
|
|
// that it pointed to another string and subtracting 42 at runtime will
|
|
|
|
// produce the wrong value.
|
|
|
|
if (Sym->isInSection()) {
|
|
|
|
auto &Sec = cast<MCSectionELF>(Sym->getSection());
|
|
|
|
unsigned Flags = Sec.getFlags();
|
|
|
|
if (Flags & ELF::SHF_MERGE) {
|
|
|
|
if (C != 0)
|
|
|
|
return true;
|
2019-07-23 01:48:53 +08:00
|
|
|
|
|
|
|
// It looks like gold has a bug (http://sourceware.org/PR16794) and can
|
|
|
|
// only handle section relocations to mergeable sections if using RELA.
|
|
|
|
if (!hasRelocationAddend())
|
|
|
|
return true;
|
2018-05-22 03:18:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Most TLS relocations use a got, so they need the symbol. Even those that
|
|
|
|
// are just an offset (@tpoff), require a symbol in gold versions before
|
|
|
|
// 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
|
|
|
|
// http://sourceware.org/PR16773.
|
|
|
|
if (Flags & ELF::SHF_TLS)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the symbol is a thumb function the final relocation must set the lowest
|
|
|
|
// bit. With a symbol that is done by just having the symbol have that bit
|
|
|
|
// set, so we would lose the bit if we relocated with the section.
|
|
|
|
// FIXME: We could use the section but add the bit to the relocation value.
|
|
|
|
if (Asm.isThumbFunc(Sym))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout,
|
|
|
|
const MCFragment *Fragment,
|
|
|
|
const MCFixup &Fixup, MCValue Target,
|
|
|
|
uint64_t &FixedValue) {
|
|
|
|
MCAsmBackend &Backend = Asm.getBackend();
|
|
|
|
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
|
|
|
|
MCFixupKindInfo::FKF_IsPCRel;
|
|
|
|
const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
|
|
|
|
uint64_t C = Target.getConstant();
|
|
|
|
uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
|
|
|
|
MCContext &Ctx = Asm.getContext();
|
|
|
|
|
|
|
|
if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
|
|
|
|
const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol());
|
|
|
|
if (SymB.isUndefined()) {
|
|
|
|
Ctx.reportError(Fixup.getLoc(),
|
|
|
|
Twine("symbol '") + SymB.getName() +
|
|
|
|
"' can not be undefined in a subtraction expression");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!SymB.isAbsolute() && "Should have been folded");
|
|
|
|
const MCSection &SecB = SymB.getSection();
|
|
|
|
if (&SecB != &FixupSection) {
|
|
|
|
Ctx.reportError(Fixup.getLoc(),
|
|
|
|
"Cannot represent a difference across sections");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:59:35 +08:00
|
|
|
assert(!IsPCRel && "should have been folded");
|
2018-05-22 03:18:28 +08:00
|
|
|
IsPCRel = true;
|
2019-08-19 18:05:59 +08:00
|
|
|
C += FixupOffset - Layout.getSymbolOffset(SymB);
|
2018-05-22 03:18:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We either rejected the fixup or folded B into C at this point.
|
|
|
|
const MCSymbolRefExpr *RefA = Target.getSymA();
|
|
|
|
const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
|
|
|
|
|
|
|
|
bool ViaWeakRef = false;
|
|
|
|
if (SymA && SymA->isVariable()) {
|
|
|
|
const MCExpr *Expr = SymA->getVariableValue();
|
|
|
|
if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
|
|
|
|
if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
|
|
|
|
SymA = cast<MCSymbolELF>(&Inner->getSymbol());
|
|
|
|
ViaWeakRef = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:44:54 +08:00
|
|
|
const MCSectionELF *SecA = (SymA && SymA->isInSection())
|
|
|
|
? cast<MCSectionELF>(&SymA->getSection())
|
|
|
|
: nullptr;
|
|
|
|
if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
|
|
|
|
return;
|
|
|
|
|
2019-08-19 18:05:59 +08:00
|
|
|
unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
|
|
|
|
bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type);
|
|
|
|
uint64_t Addend = 0;
|
|
|
|
|
|
|
|
FixedValue = !RelocateWithSymbol && SymA && !SymA->isUndefined()
|
|
|
|
? C + Layout.getSymbolOffset(*SymA)
|
|
|
|
: C;
|
|
|
|
if (hasRelocationAddend()) {
|
|
|
|
Addend = FixedValue;
|
|
|
|
FixedValue = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-22 03:18:28 +08:00
|
|
|
if (!RelocateWithSymbol) {
|
|
|
|
const auto *SectionSymbol =
|
|
|
|
SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol()) : nullptr;
|
|
|
|
if (SectionSymbol)
|
|
|
|
SectionSymbol->setUsedInReloc();
|
2019-08-19 18:05:59 +08:00
|
|
|
ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA, C);
|
2018-05-22 03:18:28 +08:00
|
|
|
Relocations[&FixupSection].push_back(Rec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-19 18:05:59 +08:00
|
|
|
const MCSymbolELF *RenamedSymA = SymA;
|
2018-05-22 03:18:28 +08:00
|
|
|
if (SymA) {
|
|
|
|
if (const MCSymbolELF *R = Renames.lookup(SymA))
|
|
|
|
RenamedSymA = R;
|
|
|
|
|
|
|
|
if (ViaWeakRef)
|
|
|
|
RenamedSymA->setIsWeakrefUsedInReloc();
|
|
|
|
else
|
|
|
|
RenamedSymA->setUsedInReloc();
|
|
|
|
}
|
2019-08-19 18:05:59 +08:00
|
|
|
ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA, C);
|
2018-05-22 03:18:28 +08:00
|
|
|
Relocations[&FixupSection].push_back(Rec);
|
|
|
|
}
|
|
|
|
|
2015-06-05 06:24:41 +08:00
|
|
|
bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
|
2015-06-03 04:38:46 +08:00
|
|
|
const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
|
2015-04-18 05:15:17 +08:00
|
|
|
bool InSet, bool IsPCRel) const {
|
2015-06-03 04:38:46 +08:00
|
|
|
const auto &SymA = cast<MCSymbolELF>(SA);
|
2015-04-18 05:15:17 +08:00
|
|
|
if (IsPCRel) {
|
|
|
|
assert(!InSet);
|
2017-04-09 07:35:49 +08:00
|
|
|
if (isWeak(SymA))
|
2015-04-18 05:15:17 +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);
|
2011-03-03 15:24:36 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 00:28:07 +08:00
|
|
|
std::unique_ptr<MCObjectWriter>
|
2017-10-10 07:53:15 +08:00
|
|
|
llvm::createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
|
|
|
|
raw_pwrite_stream &OS, bool IsLittleEndian) {
|
2019-08-15 23:54:37 +08:00
|
|
|
return std::make_unique<ELFSingleObjectWriter>(std::move(MOTW), OS,
|
2018-05-22 03:30:59 +08:00
|
|
|
IsLittleEndian);
|
2011-12-22 01:30:17 +08:00
|
|
|
}
|
2018-05-22 03:44:54 +08:00
|
|
|
|
|
|
|
std::unique_ptr<MCObjectWriter>
|
|
|
|
llvm::createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
|
|
|
|
raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
|
|
|
|
bool IsLittleEndian) {
|
2019-08-15 23:54:37 +08:00
|
|
|
return std::make_unique<ELFDwoObjectWriter>(std::move(MOTW), OS, DwoOS,
|
2018-05-22 03:44:54 +08:00
|
|
|
IsLittleEndian);
|
|
|
|
}
|