2013-03-09 09:32:16 +00:00
|
|
|
//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===//
|
2010-08-16 18:57:57 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00: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-16 18:57:57 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements ELF object file writer information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-11 00:27:28 +00:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-06-06 11:49:48 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2011-12-22 03:38:00 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-02-11 00:27:28 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-07 03:48:56 +00:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2017-07-11 23:56:10 +00:00
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
2014-04-10 21:53:53 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-08-16 18:57:57 +00:00
|
|
|
#include "llvm/MC/MCAsmLayout.h"
|
2011-12-22 03:38:00 +00:00
|
|
|
#include "llvm/MC/MCAssembler.h"
|
2010-08-16 18:57:57 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2017-02-11 00:27:28 +00:00
|
|
|
#include "llvm/MC/MCELFObjectWriter.h"
|
2010-08-16 18:57:57 +00:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2017-02-11 00:27:28 +00:00
|
|
|
#include "llvm/MC/MCFixup.h"
|
2017-07-11 23:56:10 +00:00
|
|
|
#include "llvm/MC/MCFixupKindInfo.h"
|
2017-02-11 00:27:28 +00:00
|
|
|
#include "llvm/MC/MCFragment.h"
|
2018-09-06 22:09:31 +00:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2012-03-26 06:58:25 +00:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
2017-02-11 00:27:28 +00:00
|
|
|
#include "llvm/MC/MCSection.h"
|
2010-08-16 18:57:57 +00:00
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
2017-02-11 00:27:28 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2015-06-02 00:25:12 +00:00
|
|
|
#include "llvm/MC/MCSymbolELF.h"
|
2010-08-16 18:57:57 +00:00
|
|
|
#include "llvm/MC/MCValue.h"
|
2014-07-03 02:01:39 +00:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2017-02-11 00:27:28 +00:00
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2014-04-10 21:53:53 +00:00
|
|
|
#include "llvm/Support/Compression.h"
|
2015-01-14 11:23:27 +00:00
|
|
|
#include "llvm/Support/Endian.h"
|
2017-01-17 15:45:07 +00:00
|
|
|
#include "llvm/Support/Error.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2017-02-11 00:27:28 +00:00
|
|
|
#include "llvm/Support/Host.h"
|
2018-07-17 22:17:18 +00:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2017-02-11 00:27:28 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Support/SMLoc.h"
|
2015-10-22 18:32:06 +00:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2017-02-11 00:27:28 +00: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-16 18:57:57 +00:00
|
|
|
#include <vector>
|
2016-02-02 18:20:45 +00:00
|
|
|
|
2010-08-16 18:57:57 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-05-11 22:53:06 +00:00
|
|
|
#undef DEBUG_TYPE
|
|
|
|
#define DEBUG_TYPE "reloc-info"
|
|
|
|
|
2011-12-22 03:38:00 +00:00
|
|
|
namespace {
|
2017-02-11 00:27:28 +00:00
|
|
|
|
2017-04-26 22:31:39 +00:00
|
|
|
using SectionIndexMapTy = DenseMap<const MCSectionELF *, uint32_t>;
|
2014-03-25 23:44:25 +00:00
|
|
|
|
2015-04-29 21:09:32 +00:00
|
|
|
class ELFObjectWriter;
|
2018-05-21 19:18:28 +00:00
|
|
|
struct ELFWriter;
|
2015-04-29 21:09:32 +00:00
|
|
|
|
2018-05-21 19:44:54 +00:00
|
|
|
bool isDwoSection(const MCSectionELF &Sec) {
|
|
|
|
return Sec.getSectionName().endswith(".dwo");
|
|
|
|
}
|
|
|
|
|
2014-03-25 23:44:25 +00:00
|
|
|
class SymbolTableWriter {
|
2018-05-21 19:18:28 +00:00
|
|
|
ELFWriter &EWriter;
|
2014-03-25 23:44:25 +00:00
|
|
|
bool Is64Bit;
|
|
|
|
|
2015-04-29 21:09:32 +00:00
|
|
|
// indexes we are going to write to .symtab_shndx.
|
|
|
|
std::vector<uint32_t> ShndxIndexes;
|
2014-03-25 23:44:25 +00:00
|
|
|
|
|
|
|
// The numbel of symbols written so far.
|
|
|
|
unsigned NumWritten;
|
|
|
|
|
|
|
|
void createSymtabShndx();
|
|
|
|
|
2015-04-29 21:09:32 +00:00
|
|
|
template <typename T> void write(T Value);
|
2014-03-25 23:44:25 +00:00
|
|
|
|
|
|
|
public:
|
2018-05-21 19:18:28 +00:00
|
|
|
SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit);
|
2014-03-25 23:44:25 +00: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-29 21:09:32 +00:00
|
|
|
|
|
|
|
ArrayRef<uint32_t> getShndxIndexes() const { return ShndxIndexes; }
|
2014-03-25 23:44:25 +00:00
|
|
|
};
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
struct ELFWriter {
|
|
|
|
ELFObjectWriter &OWriter;
|
|
|
|
support::endian::Writer W;
|
|
|
|
|
2018-05-21 19:44:54 +00:00
|
|
|
enum DwoMode {
|
|
|
|
AllSections,
|
|
|
|
NonDwoOnly,
|
|
|
|
DwoOnly,
|
|
|
|
} Mode;
|
|
|
|
|
2016-04-12 21:45:53 +00: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 03:38:00 +00:00
|
|
|
}
|
2016-04-12 21:45:53 +00:00
|
|
|
};
|
2011-12-22 03:38:00 +00:00
|
|
|
|
2016-04-12 21:45:53 +00:00
|
|
|
/// @}
|
|
|
|
/// @name Symbol Table Data
|
|
|
|
/// @{
|
2011-12-22 03:38:00 +00:00
|
|
|
|
2016-04-12 21:45:53 +00:00
|
|
|
StringTableBuilder StrTabBuilder{StringTableBuilder::ELF};
|
2011-12-22 03:38:00 +00:00
|
|
|
|
2016-04-12 21:45:53 +00:00
|
|
|
/// @}
|
2015-04-30 21:51:58 +00:00
|
|
|
|
2016-04-12 21:45:53 +00: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 03:38:00 +00:00
|
|
|
|
2016-04-12 21:45:53 +00: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 03:38:00 +00:00
|
|
|
|
2016-04-12 21:45:53 +00:00
|
|
|
// TargetObjectWriter wrappers.
|
2018-05-21 19:18:28 +00:00
|
|
|
bool is64Bit() const;
|
|
|
|
bool hasRelocationAddend() const;
|
2011-12-22 03:38:00 +00:00
|
|
|
|
2016-04-12 21:45:53 +00:00
|
|
|
void align(unsigned Alignment);
|
2015-03-25 13:16:53 +00:00
|
|
|
|
2016-05-27 12:27:32 +00:00
|
|
|
bool maybeWriteCompression(uint64_t Size,
|
|
|
|
SmallVectorImpl<char> &CompressedContents,
|
|
|
|
bool ZLibStyle, unsigned Alignment);
|
|
|
|
|
2016-04-12 21:45:53 +00:00
|
|
|
public:
|
2018-05-21 19:18:28 +00:00
|
|
|
ELFWriter(ELFObjectWriter &OWriter, raw_pwrite_stream &OS,
|
2018-05-21 19:44:54 +00:00
|
|
|
bool IsLittleEndian, DwoMode Mode)
|
2018-05-21 19:18:28 +00:00
|
|
|
: OWriter(OWriter),
|
2018-05-21 19:44:54 +00:00
|
|
|
W(OS, IsLittleEndian ? support::little : support::big), Mode(Mode) {}
|
2016-04-12 21:45:53 +00:00
|
|
|
|
2018-05-21 18:17:42 +00:00
|
|
|
void WriteWord(uint64_t Word) {
|
2016-04-12 21:45:53 +00:00
|
|
|
if (is64Bit())
|
2018-05-21 18:17:42 +00:00
|
|
|
W.write<uint64_t>(Word);
|
2016-04-12 21:45:53 +00:00
|
|
|
else
|
2018-05-21 18:17:42 +00:00
|
|
|
W.write<uint32_t>(Word);
|
2016-04-12 21:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> void write(T Val) {
|
2018-05-21 18:17:42 +00:00
|
|
|
W.write(Val);
|
2016-04-12 21:45:53 +00: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-26 22:31:39 +00:00
|
|
|
using SectionOffsetsTy =
|
|
|
|
std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>>;
|
2016-04-12 21:45:53 +00:00
|
|
|
|
|
|
|
// Map from a signature symbol to the group section index
|
2017-04-26 22:31:39 +00:00
|
|
|
using RevGroupMapTy = DenseMap<const MCSymbol *, unsigned>;
|
2016-04-12 21:45:53 +00: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-17 22:17:18 +00:00
|
|
|
void writeAddrsigSection();
|
|
|
|
|
2016-04-12 21:45:53 +00: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-21 19:18:28 +00: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-17 22:17:18 +00:00
|
|
|
bool EmitAddrsigSection = false;
|
|
|
|
std::vector<const MCSymbol *> AddrsigSyms;
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
bool hasRelocationAddend() const;
|
|
|
|
|
|
|
|
bool shouldRelocateWithSymbol(const MCAssembler &Asm,
|
|
|
|
const MCSymbolRefExpr *RefA,
|
|
|
|
const MCSymbolELF *Sym, uint64_t C,
|
|
|
|
unsigned Type) const;
|
|
|
|
|
|
|
|
public:
|
2018-05-21 19:30:59 +00:00
|
|
|
ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW)
|
|
|
|
: TargetObjectWriter(std::move(MOTW)) {}
|
2018-05-21 19:18:28 +00:00
|
|
|
|
|
|
|
void reset() override {
|
|
|
|
Relocations.clear();
|
|
|
|
Renames.clear();
|
|
|
|
MCObjectWriter::reset();
|
|
|
|
}
|
|
|
|
|
2016-04-12 21:45:53 +00:00
|
|
|
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
|
|
|
|
const MCSymbol &SymA,
|
|
|
|
const MCFragment &FB, bool InSet,
|
|
|
|
bool IsPCRel) const override;
|
|
|
|
|
2018-05-21 19:44:54 +00:00
|
|
|
virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
|
|
|
|
const MCSectionELF *From,
|
|
|
|
const MCSectionELF *To) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00: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-17 22:17:18 +00:00
|
|
|
void emitAddrsigSection() override { EmitAddrsigSection = true; }
|
|
|
|
void addAddrsigSymbol(const MCSymbol *Sym) override {
|
|
|
|
AddrsigSyms.push_back(Sym);
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
friend struct ELFWriter;
|
2016-04-12 21:45:53 +00:00
|
|
|
};
|
2017-02-11 00:27:28 +00:00
|
|
|
|
2018-05-21 19:30:59 +00: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-21 19:44:54 +00: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-21 19:30:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-02 18:20:45 +00:00
|
|
|
} // end anonymous namespace
|
2011-12-22 03:38:00 +00:00
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
void ELFWriter::align(unsigned Alignment) {
|
2018-05-21 18:17:42 +00:00
|
|
|
uint64_t Padding = OffsetToAlignment(W.OS.tell(), Alignment);
|
|
|
|
W.OS.write_zeros(Padding);
|
2015-06-05 18:21:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
unsigned ELFWriter::addToSectionTable(const MCSectionELF *Sec) {
|
2015-04-30 20:53:27 +00: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-22 23:58:30 +00:00
|
|
|
StrTabBuilder.add(Sec->getSectionName());
|
2015-04-30 20:53:27 +00:00
|
|
|
return SectionTable.size();
|
|
|
|
}
|
|
|
|
|
2014-03-25 23:44:25 +00:00
|
|
|
void SymbolTableWriter::createSymtabShndx() {
|
2015-04-29 21:09:32 +00:00
|
|
|
if (!ShndxIndexes.empty())
|
2014-03-25 23:44:25 +00:00
|
|
|
return;
|
|
|
|
|
2015-04-29 21:09:32 +00:00
|
|
|
ShndxIndexes.resize(NumWritten);
|
2014-03-25 23:44:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:09:32 +00:00
|
|
|
template <typename T> void SymbolTableWriter::write(T Value) {
|
|
|
|
EWriter.write(Value);
|
2014-03-25 23:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
SymbolTableWriter::SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit)
|
2015-04-29 21:09:32 +00:00
|
|
|
: EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {}
|
2014-03-25 23:44:25 +00: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-29 21:09:32 +00:00
|
|
|
if (!ShndxIndexes.empty()) {
|
2014-03-25 23:44:25 +00:00
|
|
|
if (LargeIndex)
|
2015-04-29 21:09:32 +00:00
|
|
|
ShndxIndexes.push_back(shndx);
|
2014-03-25 23:44:25 +00:00
|
|
|
else
|
2015-04-29 21:09:32 +00:00
|
|
|
ShndxIndexes.push_back(0);
|
2014-03-25 23:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
|
|
|
|
|
|
|
|
if (Is64Bit) {
|
2015-04-29 21:09:32 +00: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-25 23:44:25 +00:00
|
|
|
} else {
|
2015-04-29 21:09:32 +00: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-25 23:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++NumWritten;
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
bool ELFWriter::is64Bit() const {
|
|
|
|
return OWriter.TargetObjectWriter->is64Bit();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ELFWriter::hasRelocationAddend() const {
|
|
|
|
return OWriter.hasRelocationAddend();
|
|
|
|
}
|
|
|
|
|
2010-08-16 18:57:57 +00:00
|
|
|
// Emit the ELF header.
|
2018-05-21 19:18:28 +00:00
|
|
|
void ELFWriter::writeHeader(const MCAssembler &Asm) {
|
2010-08-16 18:57:57 +00: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-21 18:17:42 +00:00
|
|
|
W.OS << ELF::ElfMagic; // e_ident[EI_MAG0] to e_ident[EI_MAG3]
|
2010-08-16 18:57:57 +00:00
|
|
|
|
2018-05-21 18:17:42 +00:00
|
|
|
W.OS << char(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
|
2010-08-16 18:57:57 +00:00
|
|
|
|
|
|
|
// e_ident[EI_DATA]
|
2018-05-21 18:17:42 +00:00
|
|
|
W.OS << char(W.Endian == support::little ? ELF::ELFDATA2LSB
|
|
|
|
: ELF::ELFDATA2MSB);
|
2010-08-16 18:57:57 +00:00
|
|
|
|
2018-05-21 18:17:42 +00:00
|
|
|
W.OS << char(ELF::EV_CURRENT); // e_ident[EI_VERSION]
|
2010-09-09 17:57:50 +00:00
|
|
|
// e_ident[EI_OSABI]
|
2018-05-21 19:18:28 +00:00
|
|
|
W.OS << char(OWriter.TargetObjectWriter->getOSABI());
|
2019-02-14 22:42:09 +00:00
|
|
|
// e_ident[EI_ABIVERSION]
|
|
|
|
W.OS << char(OWriter.TargetObjectWriter->getABIVersion());
|
2010-08-16 18:57:57 +00:00
|
|
|
|
2018-05-21 18:17:42 +00:00
|
|
|
W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD);
|
2010-08-16 18:57:57 +00:00
|
|
|
|
2018-05-21 18:17:42 +00:00
|
|
|
W.write<uint16_t>(ELF::ET_REL); // e_type
|
2010-08-16 18:57:57 +00:00
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
W.write<uint16_t>(OWriter.TargetObjectWriter->getEMachine()); // e_machine = target
|
2010-08-16 18:57:57 +00:00
|
|
|
|
2018-05-21 18:17:42 +00:00
|
|
|
W.write<uint32_t>(ELF::EV_CURRENT); // e_version
|
2010-08-16 18:57:57 +00:00
|
|
|
WriteWord(0); // e_entry, no entry point in .o file
|
|
|
|
WriteWord(0); // e_phoff, no program header for .o
|
2015-04-14 22:54:16 +00:00
|
|
|
WriteWord(0); // e_shoff = sec hdr table off in bytes
|
2010-08-16 18:57:57 +00:00
|
|
|
|
2011-02-04 21:41:11 +00:00
|
|
|
// e_flags = whatever the target wants
|
2018-05-21 18:17:42 +00:00
|
|
|
W.write<uint32_t>(Asm.getELFHeaderEFlags());
|
2010-08-16 18:57:57 +00:00
|
|
|
|
|
|
|
// e_ehsize = ELF header size
|
2018-05-21 18:17:42 +00:00
|
|
|
W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr)
|
|
|
|
: sizeof(ELF::Elf32_Ehdr));
|
2010-08-16 18:57:57 +00:00
|
|
|
|
2018-05-21 18:17:42 +00: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-16 18:57:57 +00:00
|
|
|
|
|
|
|
// e_shentsize = Section header entry size
|
2018-05-21 18:17:42 +00:00
|
|
|
W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr)
|
|
|
|
: sizeof(ELF::Elf32_Shdr));
|
2010-08-16 18:57:57 +00:00
|
|
|
|
|
|
|
// e_shnum = # of section header ents
|
2018-05-21 18:17:42 +00:00
|
|
|
W.write<uint16_t>(0);
|
2010-08-16 18:57:57 +00: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-22 23:58:30 +00:00
|
|
|
assert(StringTableIndex < ELF::SHN_LORESERVE);
|
2018-05-21 18:17:42 +00:00
|
|
|
W.write<uint16_t>(StringTableIndex);
|
2010-08-16 18:57:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
uint64_t ELFWriter::SymbolValue(const MCSymbol &Sym,
|
|
|
|
const MCAsmLayout &Layout) {
|
2015-05-29 21:45:01 +00:00
|
|
|
if (Sym.isCommon() && Sym.isExternal())
|
2015-05-29 17:48:04 +00:00
|
|
|
return Sym.getCommonAlignment();
|
2010-12-20 21:14:39 +00:00
|
|
|
|
2014-04-30 21:51:13 +00:00
|
|
|
uint64_t Res;
|
2015-05-20 04:39:01 +00:00
|
|
|
if (!Layout.getSymbolOffset(Sym, Res))
|
2014-04-30 16:59:35 +00:00
|
|
|
return 0;
|
2010-09-27 21:23:02 +00:00
|
|
|
|
2015-05-20 04:39:01 +00:00
|
|
|
if (Layout.getAssembler().isThumbFunc(&Sym))
|
2014-04-30 16:59:35 +00:00
|
|
|
Res |= 1;
|
2010-09-27 21:23:02 +00:00
|
|
|
|
2014-03-21 22:00:29 +00:00
|
|
|
return Res;
|
2010-09-27 21:23:02 +00:00
|
|
|
}
|
|
|
|
|
2014-01-07 20:17:03 +00: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;
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
|
|
|
|
ELFSymbolData &MSD, const MCAsmLayout &Layout) {
|
2015-06-02 20:38:46 +00:00
|
|
|
const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol);
|
2015-06-02 00:25:12 +00:00
|
|
|
const MCSymbolELF *Base =
|
|
|
|
cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol));
|
2014-03-26 00:16:43 +00:00
|
|
|
|
|
|
|
// This has to be in sync with when computeSymbolTable uses SHN_ABS or
|
|
|
|
// SHN_COMMON.
|
2015-05-29 21:45:01 +00:00
|
|
|
bool IsReserved = !Base || Symbol.isCommon();
|
2010-10-31 00:16:26 +00:00
|
|
|
|
2013-02-19 21:57:35 +00:00
|
|
|
// Binding and Type share the same byte as upper and lower nibbles
|
2015-06-02 20:38:46 +00:00
|
|
|
uint8_t Binding = Symbol.getBinding();
|
|
|
|
uint8_t Type = Symbol.getType();
|
2014-03-23 03:33:20 +00:00
|
|
|
if (Base) {
|
2015-06-02 20:38:46 +00:00
|
|
|
Type = mergeTypeForSet(Type, Base->getType());
|
2014-03-23 03:33:20 +00:00
|
|
|
}
|
2015-06-04 00:47:43 +00:00
|
|
|
uint8_t Info = (Binding << 4) | Type;
|
2013-02-19 21:57:35 +00:00
|
|
|
|
2013-10-29 01:06:17 +00:00
|
|
|
// Other and Visibility share the same byte with Visibility using the lower
|
2013-02-19 21:57:35 +00:00
|
|
|
// 2 bits
|
2015-06-02 20:38:46 +00:00
|
|
|
uint8_t Visibility = Symbol.getVisibility();
|
2015-06-04 05:59:23 +00:00
|
|
|
uint8_t Other = Symbol.getOther() | Visibility;
|
2010-10-06 21:02:29 +00:00
|
|
|
|
2015-05-20 04:39:01 +00:00
|
|
|
uint64_t Value = SymbolValue(*MSD.Symbol, Layout);
|
2010-08-16 18:57:57 +00:00
|
|
|
uint64_t Size = 0;
|
|
|
|
|
2015-05-29 17:24:52 +00:00
|
|
|
const MCExpr *ESize = MSD.Symbol->getSize();
|
2014-03-27 00:28:24 +00:00
|
|
|
if (!ESize && Base)
|
2015-05-29 17:24:52 +00:00
|
|
|
ESize = Base->getSize();
|
2010-09-21 00:24:38 +00:00
|
|
|
|
2010-12-22 16:03:00 +00:00
|
|
|
if (ESize) {
|
|
|
|
int64_t Res;
|
2015-04-06 15:27:57 +00:00
|
|
|
if (!ESize->evaluateKnownAbsolute(Res, Layout))
|
2010-12-22 16:03:00 +00:00
|
|
|
report_fatal_error("Size expression must be absolute.");
|
|
|
|
Size = Res;
|
2010-08-16 18:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the symbol table entry
|
2015-05-28 20:25:29 +00:00
|
|
|
Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex,
|
|
|
|
IsReserved);
|
2010-08-16 18:57:57 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 20:05:17 +00: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-02 20:38:46 +00:00
|
|
|
static bool isWeak(const MCSymbolELF &Sym) {
|
|
|
|
if (Sym.getType() == ELF::STT_GNU_IFUNC)
|
2015-04-17 08:46:11 +00:00
|
|
|
return true;
|
|
|
|
|
2015-06-02 20:38:46 +00:00
|
|
|
switch (Sym.getBinding()) {
|
2015-04-17 08:46:11 +00:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown binding");
|
|
|
|
case ELF::STB_LOCAL:
|
|
|
|
return false;
|
|
|
|
case ELF::STB_GLOBAL:
|
2015-04-17 20:05:17 +00:00
|
|
|
return false;
|
2015-04-17 08:46:11 +00:00
|
|
|
case ELF::STB_WEAK:
|
|
|
|
case ELF::STB_GNU_UNIQUE:
|
2018-05-21 19:18:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-12-06 21:57:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
|
|
|
|
bool Used, bool Renamed) {
|
2014-03-20 02:12:01 +00:00
|
|
|
if (Symbol.isVariable()) {
|
|
|
|
const MCExpr *Expr = Symbol.getVariableValue();
|
2019-03-04 19:12:56 +00: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 02:12:01 +00:00
|
|
|
if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
|
|
|
|
if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-11-01 14:28:48 +00:00
|
|
|
|
2010-10-19 19:31:37 +00:00
|
|
|
if (Used)
|
|
|
|
return true;
|
|
|
|
|
2010-10-27 15:18:17 +00:00
|
|
|
if (Renamed)
|
|
|
|
return false;
|
|
|
|
|
2015-06-03 21:23:21 +00: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 13:39:57 +00:00
|
|
|
}
|
2011-02-23 20:22:07 +00:00
|
|
|
|
2015-06-03 21:23:21 +00:00
|
|
|
if (Symbol.isUndefined() && !Symbol.isBindingSet())
|
2010-10-27 14:44:52 +00:00
|
|
|
return false;
|
|
|
|
|
2010-10-19 19:31:37 +00:00
|
|
|
if (Symbol.isTemporary())
|
2010-10-05 18:01:23 +00:00
|
|
|
return false;
|
|
|
|
|
2015-06-04 15:33:30 +00:00
|
|
|
if (Symbol.getType() == ELF::STT_SECTION)
|
|
|
|
return false;
|
|
|
|
|
2010-10-05 18:01:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
void ELFWriter::computeSymbolTable(
|
2015-04-07 19:00:17 +00:00
|
|
|
MCAssembler &Asm, const MCAsmLayout &Layout,
|
2015-05-28 17:54:01 +00: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-22 23:58:30 +00:00
|
|
|
MCContext &Ctx = Asm.getContext();
|
2015-05-28 17:54:01 +00: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-22 23:58:30 +00:00
|
|
|
// Symbol table
|
|
|
|
unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
|
|
|
|
MCSectionELF *SymtabSection =
|
|
|
|
Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, "");
|
|
|
|
SymtabSection->setAlignment(is64Bit() ? 8 : 4);
|
|
|
|
SymbolTableIndex = addToSectionTable(SymtabSection);
|
|
|
|
|
2015-06-05 18:21:00 +00:00
|
|
|
align(SymtabSection->getAlignment());
|
2018-05-21 18:17:42 +00:00
|
|
|
uint64_t SecStart = W.OS.tell();
|
2015-05-28 17:54:01 +00:00
|
|
|
|
|
|
|
// The first entry is the undefined symbol entry.
|
|
|
|
Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
|
|
|
|
|
2015-05-28 20:11:34 +00:00
|
|
|
std::vector<ELFSymbolData> LocalSymbolData;
|
|
|
|
std::vector<ELFSymbolData> ExternalSymbolData;
|
|
|
|
|
2010-10-14 16:34:44 +00: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-22 23:58:30 +00:00
|
|
|
bool HasLargeSectionIndex = false;
|
2015-06-02 20:38:46 +00:00
|
|
|
for (const MCSymbol &S : Asm.symbols()) {
|
|
|
|
const auto &Symbol = cast<MCSymbolELF>(S);
|
2015-06-03 21:30:10 +00:00
|
|
|
bool Used = Symbol.isUsedInReloc();
|
2015-06-03 21:52:06 +00:00
|
|
|
bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
|
2015-06-03 21:41:59 +00:00
|
|
|
bool isSignature = Symbol.isSignature();
|
2010-11-14 04:17:37 +00:00
|
|
|
|
2015-05-20 04:39:01 +00:00
|
|
|
if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
|
2018-05-21 19:18:28 +00:00
|
|
|
OWriter.Renames.count(&Symbol)))
|
2010-08-16 18:57:57 +00:00
|
|
|
continue;
|
|
|
|
|
2015-11-17 10:00:43 +00:00
|
|
|
if (Symbol.isTemporary() && Symbol.isUndefined()) {
|
|
|
|
Ctx.reportError(SMLoc(), "Undefined temporary symbol");
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-25 20:10:45 +00:00
|
|
|
|
2010-08-16 18:57:57 +00:00
|
|
|
ELFSymbolData MSD;
|
2015-06-02 00:25:12 +00:00
|
|
|
MSD.Symbol = cast<MCSymbolELF>(&Symbol);
|
2010-08-16 18:57:57 +00:00
|
|
|
|
2015-06-03 21:41:59 +00:00
|
|
|
bool Local = Symbol.getBinding() == ELF::STB_LOCAL;
|
2015-06-25 20:10:45 +00:00
|
|
|
assert(Local || !Symbol.isTemporary());
|
|
|
|
|
2015-05-29 15:07:27 +00:00
|
|
|
if (Symbol.isAbsolute()) {
|
2014-03-24 03:43:21 +00:00
|
|
|
MSD.SectionIndex = ELF::SHN_ABS;
|
2015-05-29 17:48:04 +00:00
|
|
|
} else if (Symbol.isCommon()) {
|
2010-10-14 16:34:44 +00:00
|
|
|
assert(!Local);
|
2010-09-21 00:24:38 +00:00
|
|
|
MSD.SectionIndex = ELF::SHN_COMMON;
|
2015-05-29 15:07:27 +00: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-22 23:58:30 +00:00
|
|
|
if (isSignature && !Used) {
|
2015-04-28 22:59:58 +00: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-22 23:58:30 +00:00
|
|
|
if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
|
|
|
|
HasLargeSectionIndex = true;
|
|
|
|
} else {
|
2010-11-14 04:17:37 +00: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-22 23:58:30 +00:00
|
|
|
}
|
2010-08-16 18:57:57 +00:00
|
|
|
} else {
|
2010-11-10 21:51:05 +00:00
|
|
|
const MCSectionELF &Section =
|
2015-05-29 15:07:27 +00:00
|
|
|
static_cast<const MCSectionELF &>(Symbol.getSection());
|
2018-12-05 10:43:58 +00: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-21 19:44:54 +00:00
|
|
|
if (Mode == NonDwoOnly && isDwoSection(Section))
|
|
|
|
continue;
|
2010-11-10 21:51:05 +00:00
|
|
|
MSD.SectionIndex = SectionIndexMap.lookup(&Section);
|
2010-08-16 18:57:57 +00: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-22 23:58:30 +00:00
|
|
|
if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
|
|
|
|
HasLargeSectionIndex = true;
|
2010-10-15 15:39:06 +00:00
|
|
|
}
|
|
|
|
|
2010-10-27 15:18:17 +00:00
|
|
|
StringRef Name = Symbol.getName();
|
2014-10-17 01:48:58 +00:00
|
|
|
|
|
|
|
// Sections have their own string table
|
2015-10-22 18:32:06 +00:00
|
|
|
if (Symbol.getType() != ELF::STT_SECTION) {
|
|
|
|
MSD.Name = Name;
|
|
|
|
StrTabBuilder.add(Name);
|
|
|
|
}
|
2010-10-27 15:18:17 +00:00
|
|
|
|
2015-05-29 14:20:40 +00:00
|
|
|
if (Local)
|
2010-10-27 14:44:52 +00:00
|
|
|
LocalSymbolData.push_back(MSD);
|
|
|
|
else
|
|
|
|
ExternalSymbolData.push_back(MSD);
|
2010-08-16 18:57:57 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 16:59:57 +00: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-22 23:58:30 +00:00
|
|
|
if (HasLargeSectionIndex) {
|
|
|
|
MCSectionELF *SymtabShndxSection =
|
|
|
|
Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");
|
|
|
|
SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);
|
|
|
|
SymtabShndxSection->setAlignment(4);
|
|
|
|
}
|
|
|
|
|
2015-05-28 19:29:15 +00:00
|
|
|
ArrayRef<std::string> FileNames = Asm.getFileNames();
|
|
|
|
for (const std::string &Name : FileNames)
|
2015-05-28 18:03:20 +00:00
|
|
|
StrTabBuilder.add(Name);
|
2014-04-30 16:25:02 +00:00
|
|
|
|
2015-10-23 21:48:05 +00:00
|
|
|
StrTabBuilder.finalize();
|
2014-04-30 16:25:02 +00:00
|
|
|
|
2017-03-03 21:22:06 +00: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-28 20:00:13 +00: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-16 18:57:57 +00: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-28 19:29:15 +00:00
|
|
|
unsigned Index = FileNames.size() + 1;
|
2010-11-14 03:12:24 +00:00
|
|
|
|
2015-05-28 17:54:01 +00:00
|
|
|
for (ELFSymbolData &MSD : LocalSymbolData) {
|
2015-06-23 11:31:32 +00:00
|
|
|
unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION
|
|
|
|
? 0
|
|
|
|
: StrTabBuilder.getOffset(MSD.Name);
|
2015-05-28 17:54:01 +00:00
|
|
|
MSD.Symbol->setIndex(Index++);
|
2015-05-28 20:25:29 +00:00
|
|
|
writeSymbol(Writer, StringIndex, MSD, Layout);
|
2015-05-28 17:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the symbol table entries.
|
2015-05-28 20:00:13 +00:00
|
|
|
LastLocalSymbolIndex = Index;
|
2015-05-28 17:54:01 +00:00
|
|
|
|
2015-05-28 19:43:20 +00:00
|
|
|
for (ELFSymbolData &MSD : ExternalSymbolData) {
|
2015-05-28 20:25:29 +00:00
|
|
|
unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
|
2015-05-28 20:00:13 +00:00
|
|
|
MSD.Symbol->setIndex(Index++);
|
2015-05-28 20:25:29 +00:00
|
|
|
writeSymbol(Writer, StringIndex, MSD, Layout);
|
2015-06-02 20:38:46 +00:00
|
|
|
assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
|
2015-05-28 17:54:01 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 18:17:42 +00:00
|
|
|
uint64_t SecEnd = W.OS.tell();
|
2015-05-28 17:54:01 +00: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-21 18:17:42 +00:00
|
|
|
SecStart = W.OS.tell();
|
2015-05-28 17:54:01 +00:00
|
|
|
const MCSectionELF *SymtabShndxSection =
|
|
|
|
SectionTable[SymtabShndxSectionIndex - 1];
|
|
|
|
for (uint32_t Index : ShndxIndexes)
|
|
|
|
write(Index);
|
2018-05-21 18:17:42 +00:00
|
|
|
SecEnd = W.OS.tell();
|
2015-05-28 17:54:01 +00:00
|
|
|
SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
|
2010-08-16 18:57:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 22:17:18 +00:00
|
|
|
void ELFWriter::writeAddrsigSection() {
|
|
|
|
for (const MCSymbol *Sym : OWriter.AddrsigSyms)
|
|
|
|
encodeULEB128(Sym->getIndex(), W.OS);
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
|
|
|
|
const MCSectionELF &Sec) {
|
|
|
|
if (OWriter.Relocations[&Sec].empty())
|
2015-04-30 14:21:49 +00:00
|
|
|
return nullptr;
|
2010-08-17 17:56:13 +00:00
|
|
|
|
2015-04-30 00:45:46 +00:00
|
|
|
const StringRef SectionName = Sec.getSectionName();
|
2015-02-17 20:31:13 +00:00
|
|
|
std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
|
|
|
|
RelaSectionName += SectionName;
|
2013-07-10 20:58:17 +00:00
|
|
|
|
2015-02-17 20:31:13 +00: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-11 23:17:48 +00:00
|
|
|
|
2015-02-17 20:31:13 +00:00
|
|
|
unsigned Flags = 0;
|
2015-04-30 00:45:46 +00:00
|
|
|
if (Sec.getFlags() & ELF::SHF_GROUP)
|
2015-02-17 20:31:13 +00:00
|
|
|
Flags = ELF::SHF_GROUP;
|
|
|
|
|
2015-05-21 19:20:38 +00:00
|
|
|
MCSectionELF *RelaSection = Ctx.createELFRelSection(
|
2015-02-17 20:31:13 +00:00
|
|
|
RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
|
2015-04-30 00:45:46 +00:00
|
|
|
Flags, EntrySize, Sec.getGroup(), &Sec);
|
2015-05-21 20:43:13 +00:00
|
|
|
RelaSection->setAlignment(is64Bit() ? 8 : 4);
|
2015-04-30 14:21:49 +00:00
|
|
|
return RelaSection;
|
2011-03-20 18:44:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 12:27:32 +00:00
|
|
|
// Include the debug info compression header.
|
2018-05-21 19:18:28 +00:00
|
|
|
bool ELFWriter::maybeWriteCompression(
|
2016-05-27 12:27:32 +00: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 00:14:12 +00:00
|
|
|
const StringRef Magic = "ZLIB";
|
|
|
|
if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
|
|
|
|
return false;
|
2018-05-21 18:17:42 +00:00
|
|
|
W.OS << Magic;
|
|
|
|
support::endian::write(W.OS, Size, support::big);
|
2016-05-25 00:14:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
|
|
|
|
const MCAsmLayout &Layout) {
|
2015-05-27 13:30:50 +00:00
|
|
|
MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
|
2015-04-30 21:51:58 +00:00
|
|
|
StringRef SectionName = Section.getSectionName();
|
2014-04-10 21:53:53 +00:00
|
|
|
|
2017-06-09 00:40:19 +00:00
|
|
|
auto &MC = Asm.getContext();
|
|
|
|
const auto &MAI = MC.getAsmInfo();
|
|
|
|
|
2015-04-30 21:51:58 +00: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 12:27:32 +00:00
|
|
|
bool CompressionEnabled =
|
2017-06-09 00:40:19 +00:00
|
|
|
MAI->compressDebugSections() != DebugCompressionType::None;
|
2016-05-27 12:27:32 +00:00
|
|
|
if (!CompressionEnabled || !SectionName.startswith(".debug_") ||
|
|
|
|
SectionName == ".debug_frame") {
|
2018-05-21 18:17:42 +00:00
|
|
|
Asm.writeSectionData(W.OS, &Section, Layout);
|
2015-04-30 21:51:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-09 00:40:19 +00:00
|
|
|
assert((MAI->compressDebugSections() == DebugCompressionType::Z ||
|
|
|
|
MAI->compressDebugSections() == DebugCompressionType::GNU) &&
|
|
|
|
"expected zlib or zlib-gnu style compression");
|
|
|
|
|
2015-09-01 16:19:03 +00:00
|
|
|
SmallVector<char, 128> UncompressedData;
|
|
|
|
raw_svector_ostream VecOS(UncompressedData);
|
2018-05-21 18:11:35 +00:00
|
|
|
Asm.writeSectionData(VecOS, &Section, Layout);
|
2014-04-10 21:53:53 +00:00
|
|
|
|
2015-04-30 21:51:58 +00:00
|
|
|
SmallVector<char, 128> CompressedContents;
|
2017-01-17 15:45:07 +00:00
|
|
|
if (Error E = zlib::compress(
|
|
|
|
StringRef(UncompressedData.data(), UncompressedData.size()),
|
|
|
|
CompressedContents)) {
|
|
|
|
consumeError(std::move(E));
|
2018-05-21 18:17:42 +00:00
|
|
|
W.OS << UncompressedData;
|
2015-04-30 21:51:58 +00:00
|
|
|
return;
|
2014-04-18 21:24:12 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 00:40:19 +00:00
|
|
|
bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z;
|
2016-05-27 12:27:32 +00:00
|
|
|
if (!maybeWriteCompression(UncompressedData.size(), CompressedContents,
|
|
|
|
ZlibStyle, Sec.getAlignment())) {
|
2018-05-21 18:17:42 +00:00
|
|
|
W.OS << UncompressedData;
|
2014-04-10 21:53:53 +00:00
|
|
|
return;
|
2015-04-30 21:51:58 +00:00
|
|
|
}
|
2016-05-27 12:27:32 +00:00
|
|
|
|
|
|
|
if (ZlibStyle)
|
|
|
|
// Set the compressed flag. That is zlib style.
|
|
|
|
Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED);
|
|
|
|
else
|
|
|
|
// Add "z" prefix to section name. This is zlib-gnu style.
|
2017-06-09 00:40:19 +00:00
|
|
|
MC.renameELFSection(&Section, (".z" + SectionName.drop_front(1)).str());
|
2018-05-21 18:17:42 +00:00
|
|
|
W.OS << CompressedContents;
|
2014-04-10 21:53:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00: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-21 18:17:42 +00:00
|
|
|
W.write<uint32_t>(Name); // sh_name: index into string table
|
|
|
|
W.write<uint32_t>(Type); // sh_type
|
2010-08-16 18:57:57 +00:00
|
|
|
WriteWord(Flags); // sh_flags
|
|
|
|
WriteWord(Address); // sh_addr
|
|
|
|
WriteWord(Offset); // sh_offset
|
|
|
|
WriteWord(Size); // sh_size
|
2018-05-21 18:17:42 +00:00
|
|
|
W.write<uint32_t>(Link); // sh_link
|
|
|
|
W.write<uint32_t>(Info); // sh_info
|
2010-08-16 18:57:57 +00:00
|
|
|
WriteWord(Alignment); // sh_addralign
|
|
|
|
WriteWord(EntrySize); // sh_entsize
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
void ELFWriter::writeRelocations(const MCAssembler &Asm,
|
2015-04-30 00:45:46 +00:00
|
|
|
const MCSectionELF &Sec) {
|
2018-05-21 19:18:28 +00:00
|
|
|
std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec];
|
2012-03-23 23:06:45 +00:00
|
|
|
|
2015-12-17 16:22:06 +00: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 15:08:24 +00:00
|
|
|
|
2015-12-17 16:22:06 +00:00
|
|
|
// Sort the relocation entries. MIPS needs this.
|
2018-05-21 19:18:28 +00:00
|
|
|
OWriter.TargetObjectWriter->sortRelocs(Asm, Relocs);
|
2010-08-16 18:57:57 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
|
2014-03-29 06:26:49 +00:00
|
|
|
const ELFRelocationEntry &Entry = Relocs[e - i - 1];
|
2015-05-28 20:53:09 +00:00
|
|
|
unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
|
2010-08-16 18:57:57 +00:00
|
|
|
|
2010-12-18 03:27:34 +00:00
|
|
|
if (is64Bit()) {
|
2015-04-30 00:30:40 +00:00
|
|
|
write(Entry.Offset);
|
2018-05-21 19:18:28 +00:00
|
|
|
if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
|
2015-04-30 00:30:40 +00:00
|
|
|
write(uint32_t(Index));
|
2010-09-09 18:01:29 +00:00
|
|
|
|
2018-05-21 19:18:28 +00: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 06:26:49 +00:00
|
|
|
} else {
|
2012-06-27 22:28:30 +00:00
|
|
|
struct ELF::Elf64_Rela ERE64;
|
2014-03-29 06:26:49 +00:00
|
|
|
ERE64.setSymbolAndType(Index, Entry.Type);
|
2015-04-30 00:30:40 +00:00
|
|
|
write(ERE64.r_info);
|
2012-06-27 22:28:30 +00:00
|
|
|
}
|
2010-12-18 03:27:34 +00:00
|
|
|
if (hasRelocationAddend())
|
2015-04-30 00:30:40 +00:00
|
|
|
write(Entry.Addend);
|
2010-09-09 18:01:29 +00:00
|
|
|
} else {
|
2015-04-30 00:30:40 +00:00
|
|
|
write(uint32_t(Entry.Offset));
|
2010-09-09 18:01:29 +00:00
|
|
|
|
2010-10-05 15:11:03 +00:00
|
|
|
struct ELF::Elf32_Rela ERE32;
|
2014-03-29 06:26:49 +00:00
|
|
|
ERE32.setSymbolAndType(Index, Entry.Type);
|
2015-04-30 00:30:40 +00:00
|
|
|
write(ERE32.r_info);
|
2010-09-09 18:01:29 +00:00
|
|
|
|
2010-12-18 03:27:34 +00:00
|
|
|
if (hasRelocationAddend())
|
2015-04-30 00:30:40 +00:00
|
|
|
write(uint32_t(Entry.Addend));
|
2017-09-21 14:04:53 +00:00
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
|
|
|
|
if (uint32_t RType =
|
|
|
|
OWriter.TargetObjectWriter->getRType2(Entry.Type)) {
|
2017-09-21 14:04:53 +00:00
|
|
|
write(uint32_t(Entry.Offset));
|
|
|
|
|
|
|
|
ERE32.setSymbolAndType(0, RType);
|
|
|
|
write(ERE32.r_info);
|
|
|
|
write(uint32_t(0));
|
|
|
|
}
|
2018-05-21 19:18:28 +00:00
|
|
|
if (uint32_t RType =
|
|
|
|
OWriter.TargetObjectWriter->getRType3(Entry.Type)) {
|
2017-09-21 14:04:53 +00:00
|
|
|
write(uint32_t(Entry.Offset));
|
|
|
|
|
|
|
|
ERE32.setSymbolAndType(0, RType);
|
|
|
|
write(ERE32.r_info);
|
|
|
|
write(uint32_t(0));
|
|
|
|
}
|
|
|
|
}
|
2010-09-09 18:01:29 +00:00
|
|
|
}
|
2010-08-16 18:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
const MCSectionELF *ELFWriter::createStringTable(MCContext &Ctx) {
|
2015-05-25 21:56:55 +00:00
|
|
|
const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1];
|
2018-05-21 18:17:42 +00:00
|
|
|
StrTabBuilder.write(W.OS);
|
2015-04-30 14:21:49 +00:00
|
|
|
return StrtabSection;
|
2010-11-11 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
|
|
|
|
uint32_t GroupSymbolIndex, uint64_t Offset,
|
|
|
|
uint64_t Size, const MCSectionELF &Section) {
|
2010-11-10 23:36:59 +00:00
|
|
|
uint64_t sh_link = 0;
|
|
|
|
uint64_t sh_info = 0;
|
|
|
|
|
|
|
|
switch(Section.getType()) {
|
2015-04-06 03:16:51 +00:00
|
|
|
default:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
2010-11-10 23:36:59 +00:00
|
|
|
case ELF::SHT_DYNAMIC:
|
2015-04-30 21:20:06 +00:00
|
|
|
llvm_unreachable("SHT_DYNAMIC in a relocatable object");
|
2010-11-10 23:36:59 +00:00
|
|
|
|
|
|
|
case ELF::SHT_REL:
|
|
|
|
case ELF::SHT_RELA: {
|
2015-02-17 20:37:50 +00:00
|
|
|
sh_link = SymbolTableIndex;
|
2010-11-10 23:36:59 +00:00
|
|
|
assert(sh_link && ".symtab not found");
|
2017-03-14 19:28:51 +00:00
|
|
|
const MCSection *InfoSection = Section.getAssociatedSection();
|
|
|
|
sh_info = SectionIndexMap.lookup(cast<MCSectionELF>(InfoSection));
|
2010-11-10 23:36:59 +00: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-02 16:33:01 +00:00
|
|
|
case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
|
2018-07-17 22:17:18 +00:00
|
|
|
case ELF::SHT_LLVM_ADDRSIG:
|
2010-11-10 23:36:59 +00:00
|
|
|
sh_link = SymbolTableIndex;
|
|
|
|
break;
|
|
|
|
|
2011-10-07 23:28:32 +00:00
|
|
|
case ELF::SHT_GROUP:
|
2010-11-11 18:13:52 +00:00
|
|
|
sh_link = SymbolTableIndex;
|
|
|
|
sh_info = GroupSymbolIndex;
|
|
|
|
break;
|
2010-11-10 23:36:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 19:28:51 +00: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 14:18:59 +00: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-22 23:58:30 +00:00
|
|
|
WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()),
|
2015-05-21 19:36:43 +00:00
|
|
|
Section.getType(), Section.getFlags(), 0, Offset, Size,
|
|
|
|
sh_link, sh_info, Section.getAlignment(),
|
|
|
|
Section.getEntrySize());
|
2010-11-10 23:36:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
void ELFWriter::writeSectionHeader(
|
2015-06-04 20:55:49 +00:00
|
|
|
const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
|
2015-04-28 15:26:21 +00:00
|
|
|
const SectionOffsetsTy &SectionOffsets) {
|
2015-04-30 21:10:06 +00:00
|
|
|
const unsigned NumSections = SectionTable.size();
|
2011-03-20 18:44:20 +00:00
|
|
|
|
|
|
|
// Null section first.
|
|
|
|
uint64_t FirstSectionSize =
|
2015-04-15 13:07:47 +00:00
|
|
|
(NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
|
2015-04-29 20:25:24 +00:00
|
|
|
WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
|
2011-03-20 18:44:20 +00:00
|
|
|
|
2015-05-25 21:56:55 +00:00
|
|
|
for (const MCSectionELF *Section : SectionTable) {
|
2011-03-20 18:44:20 +00:00
|
|
|
uint32_t GroupSymbolIndex;
|
2015-04-30 20:53:27 +00:00
|
|
|
unsigned Type = Section->getType();
|
|
|
|
if (Type != ELF::SHT_GROUP)
|
2011-03-20 18:44:20 +00:00
|
|
|
GroupSymbolIndex = 0;
|
|
|
|
else
|
2015-05-28 20:53:09 +00:00
|
|
|
GroupSymbolIndex = Section->getGroup()->getIndex();
|
2011-03-20 18:44:20 +00:00
|
|
|
|
2015-04-30 14:21:49 +00:00
|
|
|
const std::pair<uint64_t, uint64_t> &Offsets =
|
2015-04-30 20:53:27 +00:00
|
|
|
SectionOffsets.find(Section)->second;
|
2015-05-21 19:46:39 +00:00
|
|
|
uint64_t Size;
|
2015-05-26 02:00:36 +00:00
|
|
|
if (Type == ELF::SHT_NOBITS)
|
|
|
|
Size = Layout.getSectionAddressSize(Section);
|
|
|
|
else
|
2015-05-21 19:46:39 +00:00
|
|
|
Size = Offsets.second - Offsets.first;
|
2011-03-20 18:44:20 +00:00
|
|
|
|
2015-05-21 19:42:35 +00:00
|
|
|
writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
|
2015-05-21 19:36:43 +00:00
|
|
|
*Section);
|
2011-03-20 18:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
|
2018-05-21 18:23:50 +00:00
|
|
|
uint64_t StartOffset = W.OS.tell();
|
|
|
|
|
2015-04-30 14:21:49 +00: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-22 23:58:30 +00:00
|
|
|
MCSectionELF *StrtabSection =
|
|
|
|
Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
|
|
|
|
StringTableIndex = addToSectionTable(StrtabSection);
|
2011-03-20 18:44:20 +00:00
|
|
|
|
2015-04-30 14:21:49 +00:00
|
|
|
RevGroupMapTy RevGroupMap;
|
|
|
|
SectionIndexMapTy SectionIndexMap;
|
2011-03-20 18:44:20 +00:00
|
|
|
|
2015-04-30 14:21:49 +00:00
|
|
|
std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
|
2015-04-08 11:41:24 +00:00
|
|
|
|
2015-04-14 22:54:16 +00:00
|
|
|
// Write out the ELF header ...
|
2015-04-29 20:39:37 +00:00
|
|
|
writeHeader(Asm);
|
2015-04-14 22:54:16 +00:00
|
|
|
|
|
|
|
// ... then the sections ...
|
2015-04-30 14:21:49 +00:00
|
|
|
SectionOffsetsTy SectionOffsets;
|
2015-05-21 20:43:13 +00:00
|
|
|
std::vector<MCSectionELF *> Groups;
|
|
|
|
std::vector<MCSectionELF *> Relocations;
|
2015-05-27 13:30:50 +00:00
|
|
|
for (MCSection &Sec : Asm) {
|
|
|
|
MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
|
2018-05-21 19:44:54 +00:00
|
|
|
if (Mode == NonDwoOnly && isDwoSection(Section))
|
|
|
|
continue;
|
|
|
|
if (Mode == DwoOnly && !isDwoSection(Section))
|
|
|
|
continue;
|
2015-04-30 14:21:49 +00:00
|
|
|
|
2015-06-05 18:21:00 +00:00
|
|
|
align(Section.getAlignment());
|
2010-09-06 16:11:52 +00:00
|
|
|
|
2011-03-20 18:44:20 +00:00
|
|
|
// Remember the offset into the file for this section.
|
2018-05-21 18:17:42 +00:00
|
|
|
uint64_t SecStart = W.OS.tell();
|
2015-04-30 00:30:40 +00:00
|
|
|
|
2015-06-02 21:30:13 +00:00
|
|
|
const MCSymbolELF *SignatureSymbol = Section.getGroup();
|
2015-05-27 13:30:50 +00:00
|
|
|
writeSectionData(Asm, Section, Layout);
|
2015-04-30 00:30:40 +00:00
|
|
|
|
2018-05-21 18:17:42 +00:00
|
|
|
uint64_t SecEnd = W.OS.tell();
|
2015-04-30 14:21:49 +00:00
|
|
|
SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
|
|
|
|
|
2015-05-21 20:43:13 +00:00
|
|
|
MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
|
2015-04-30 14:21:49 +00:00
|
|
|
|
|
|
|
if (SignatureSymbol) {
|
2015-05-29 20:21:02 +00:00
|
|
|
Asm.registerSymbol(*SignatureSymbol);
|
2015-04-30 14:21:49 +00:00
|
|
|
unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
|
|
|
|
if (!GroupIdx) {
|
2015-05-21 19:20:38 +00:00
|
|
|
MCSectionELF *Group = Ctx.createELFGroupSection(SignatureSymbol);
|
2015-04-30 20:53:27 +00:00
|
|
|
GroupIdx = addToSectionTable(Group);
|
2015-05-21 20:43:13 +00:00
|
|
|
Group->setAlignment(4);
|
|
|
|
Groups.push_back(Group);
|
2015-04-30 14:21:49 +00:00
|
|
|
}
|
2015-06-05 17:54:25 +00:00
|
|
|
std::vector<const MCSectionELF *> &Members =
|
|
|
|
GroupMembers[SignatureSymbol];
|
|
|
|
Members.push_back(&Section);
|
2015-04-30 14:21:49 +00:00
|
|
|
if (RelSection)
|
2015-06-05 17:54:25 +00:00
|
|
|
Members.push_back(RelSection);
|
2015-04-30 14:21:49 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 20:53:27 +00:00
|
|
|
SectionIndexMap[&Section] = addToSectionTable(&Section);
|
2015-05-21 20:43:13 +00:00
|
|
|
if (RelSection) {
|
2015-04-30 20:53:27 +00:00
|
|
|
SectionIndexMap[RelSection] = addToSectionTable(RelSection);
|
2015-05-21 20:43:13 +00:00
|
|
|
Relocations.push_back(RelSection);
|
|
|
|
}
|
2018-09-06 22:09:31 +00:00
|
|
|
|
|
|
|
OWriter.TargetObjectWriter->addTargetSectionFlags(Ctx, Section);
|
2015-05-21 20:43:13 +00: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-02 16:33:01 +00: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-21 20:43:13 +00:00
|
|
|
for (MCSectionELF *Group : Groups) {
|
2015-06-05 18:21:00 +00:00
|
|
|
align(Group->getAlignment());
|
2015-05-21 20:43:13 +00:00
|
|
|
|
|
|
|
// Remember the offset into the file for this section.
|
2018-05-21 18:17:42 +00:00
|
|
|
uint64_t SecStart = W.OS.tell();
|
2015-05-21 20:43:13 +00: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-21 18:17:42 +00:00
|
|
|
uint64_t SecEnd = W.OS.tell();
|
2015-05-21 20:43:13 +00:00
|
|
|
SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
|
2015-04-30 14:21:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:44:54 +00:00
|
|
|
if (Mode == DwoOnly) {
|
|
|
|
// dwo files don't have symbol tables or relocations, but they do have
|
|
|
|
// string tables.
|
|
|
|
StrTabBuilder.finalize();
|
|
|
|
} else {
|
2018-07-17 22:17:18 +00:00
|
|
|
MCSectionELF *AddrsigSection;
|
|
|
|
if (OWriter.EmitAddrsigSection) {
|
|
|
|
AddrsigSection = Ctx.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG,
|
|
|
|
ELF::SHF_EXCLUDE);
|
|
|
|
addToSectionTable(AddrsigSection);
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:44:54 +00:00
|
|
|
// Compute symbol table information.
|
|
|
|
computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
|
|
|
|
SectionOffsets);
|
2015-05-21 20:43:13 +00:00
|
|
|
|
2018-05-21 19:44:54 +00:00
|
|
|
for (MCSectionELF *RelSection : Relocations) {
|
|
|
|
align(RelSection->getAlignment());
|
2015-05-21 20:43:13 +00:00
|
|
|
|
2018-05-21 19:44:54 +00:00
|
|
|
// Remember the offset into the file for this section.
|
|
|
|
uint64_t SecStart = W.OS.tell();
|
2015-05-21 20:43:13 +00:00
|
|
|
|
2018-05-21 19:44:54 +00:00
|
|
|
writeRelocations(Asm,
|
|
|
|
cast<MCSectionELF>(*RelSection->getAssociatedSection()));
|
2015-05-21 20:43:13 +00:00
|
|
|
|
2018-05-21 19:44:54 +00:00
|
|
|
uint64_t SecEnd = W.OS.tell();
|
|
|
|
SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
|
|
|
|
}
|
2018-07-17 22:17:18 +00: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-16 18:57:57 +00: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-02 16:33:01 +00: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-29 20:34:31 +00:00
|
|
|
{
|
2018-05-21 18:17:42 +00:00
|
|
|
uint64_t SecStart = W.OS.tell();
|
2015-04-30 21:10:06 +00:00
|
|
|
const MCSectionELF *Sec = createStringTable(Ctx);
|
2018-05-21 18:17:42 +00:00
|
|
|
uint64_t SecEnd = W.OS.tell();
|
2015-04-30 14:21:49 +00:00
|
|
|
SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);
|
2015-04-29 20:34:31 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 22:54:16 +00:00
|
|
|
uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
|
2015-06-05 18:21:00 +00:00
|
|
|
align(NaturalAlignment);
|
2010-09-06 16:11:52 +00:00
|
|
|
|
2018-05-21 18:17:42 +00:00
|
|
|
const uint64_t SectionHeaderOffset = W.OS.tell();
|
2015-04-14 22:54:16 +00:00
|
|
|
|
2011-03-20 18:44:20 +00:00
|
|
|
// ... then the section header table ...
|
2015-06-04 20:55:49 +00:00
|
|
|
writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
|
2015-04-14 22:54:16 +00:00
|
|
|
|
2018-05-21 18:17:42 +00: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-29 20:39:37 +00:00
|
|
|
unsigned NumSectionsOffset;
|
|
|
|
|
2018-05-21 18:17:42 +00:00
|
|
|
auto &Stream = static_cast<raw_pwrite_stream &>(W.OS);
|
2015-04-14 22:54:16 +00:00
|
|
|
if (is64Bit()) {
|
2018-05-21 18:17:42 +00: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-29 20:39:37 +00:00
|
|
|
NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
|
2015-04-14 22:54:16 +00:00
|
|
|
} else {
|
2018-05-21 18:17:42 +00: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-29 20:39:37 +00:00
|
|
|
NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
|
2015-04-14 22:54:16 +00:00
|
|
|
}
|
2018-05-21 18:17:42 +00:00
|
|
|
Stream.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections),
|
|
|
|
NumSectionsOffset);
|
2018-05-21 18:23:50 +00:00
|
|
|
|
|
|
|
return W.OS.tell() - StartOffset;
|
2010-08-16 18:57:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 19:18:28 +00: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());
|
|
|
|
|
|
|
|
if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
|
|
|
|
continue;
|
|
|
|
|
2019-02-13 01:39:32 +00:00
|
|
|
// FIXME: Get source locations for these errors or diagnose them earlier.
|
2018-05-21 19:18:28 +00:00
|
|
|
if (Symbol.isUndefined() && Rest.startswith("@@") &&
|
2019-02-13 01:39:32 +00:00
|
|
|
!Rest.startswith("@@@")) {
|
|
|
|
Asm.getContext().reportError(SMLoc(), "versioned symbol " + AliasName +
|
|
|
|
" must be defined");
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-21 19:18:28 +00:00
|
|
|
|
2019-02-13 01:39:32 +00:00
|
|
|
if (Renames.count(&Symbol) && Renames[&Symbol] != Alias) {
|
|
|
|
Asm.getContext().reportError(
|
|
|
|
SMLoc(), llvm::Twine("multiple symbol versions defined for ") +
|
|
|
|
Symbol.getName());
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-21 19:18:28 +00:00
|
|
|
|
|
|
|
Renames.insert(std::make_pair(&Symbol, Alias));
|
|
|
|
}
|
2018-07-17 22:17:18 +00:00
|
|
|
|
|
|
|
for (const MCSymbol *&Sym : AddrsigSyms) {
|
|
|
|
if (const MCSymbol *R = Renames.lookup(cast<MCSymbolELF>(Sym)))
|
|
|
|
Sym = R;
|
2018-08-06 21:59:58 +00:00
|
|
|
if (Sym->isInSection() && Sym->getName().startswith(".L"))
|
|
|
|
Sym = Sym->getSection().getBeginSymbol();
|
2018-07-17 22:17:18 +00:00
|
|
|
Sym->setUsedInReloc();
|
|
|
|
}
|
2018-05-21 19:18:28 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()) {
|
|
|
|
// Let A, B and C being the components of Target and R be the location of
|
|
|
|
// the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
|
|
|
|
// If it is pcrel, we want to compute (A - B + C - R).
|
|
|
|
|
|
|
|
// In general, ELF has no relocations for -B. It can only represent (A + C)
|
|
|
|
// or (A + C - R). If B = R + K and the relocation is not pcrel, we can
|
|
|
|
// replace B to implement it: (A - R - K + C)
|
|
|
|
if (IsPCRel) {
|
|
|
|
Ctx.reportError(
|
|
|
|
Fixup.getLoc(),
|
|
|
|
"No relocation available to represent this relative expression");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
|
|
|
|
uint64_t K = SymBOffset - FixupOffset;
|
|
|
|
IsPCRel = true;
|
|
|
|
C -= K;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
|
|
|
|
uint64_t OriginalC = C;
|
|
|
|
bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type);
|
|
|
|
if (!RelocateWithSymbol && SymA && !SymA->isUndefined())
|
|
|
|
C += Layout.getSymbolOffset(*SymA);
|
|
|
|
|
|
|
|
uint64_t Addend = 0;
|
|
|
|
if (hasRelocationAddend()) {
|
|
|
|
Addend = C;
|
|
|
|
C = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FixedValue = C;
|
|
|
|
|
2018-05-21 19:44:54 +00:00
|
|
|
const MCSectionELF *SecA = (SymA && SymA->isInSection())
|
|
|
|
? cast<MCSectionELF>(&SymA->getSection())
|
|
|
|
: nullptr;
|
|
|
|
if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
|
|
|
|
return;
|
|
|
|
|
2018-05-21 19:18:28 +00:00
|
|
|
if (!RelocateWithSymbol) {
|
|
|
|
const auto *SectionSymbol =
|
|
|
|
SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol()) : nullptr;
|
|
|
|
if (SectionSymbol)
|
|
|
|
SectionSymbol->setUsedInReloc();
|
|
|
|
ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA,
|
|
|
|
OriginalC);
|
|
|
|
Relocations[&FixupSection].push_back(Rec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto *RenamedSymA = SymA;
|
|
|
|
if (SymA) {
|
|
|
|
if (const MCSymbolELF *R = Renames.lookup(SymA))
|
|
|
|
RenamedSymA = R;
|
|
|
|
|
|
|
|
if (ViaWeakRef)
|
|
|
|
RenamedSymA->setIsWeakrefUsedInReloc();
|
|
|
|
else
|
|
|
|
RenamedSymA->setUsedInReloc();
|
|
|
|
}
|
|
|
|
ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA,
|
|
|
|
OriginalC);
|
|
|
|
Relocations[&FixupSection].push_back(Rec);
|
|
|
|
}
|
|
|
|
|
2015-06-04 22:24:41 +00:00
|
|
|
bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
|
2015-06-02 20:38:46 +00:00
|
|
|
const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
|
2015-04-17 21:15:17 +00:00
|
|
|
bool InSet, bool IsPCRel) const {
|
2015-06-02 20:38:46 +00:00
|
|
|
const auto &SymA = cast<MCSymbolELF>(SA);
|
2015-04-17 21:15:17 +00:00
|
|
|
if (IsPCRel) {
|
|
|
|
assert(!InSet);
|
2017-04-08 23:35:49 +00:00
|
|
|
if (isWeak(SymA))
|
2015-04-17 21:15:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-04 22:24:41 +00:00
|
|
|
return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
|
2015-04-17 21:15:17 +00:00
|
|
|
InSet, IsPCRel);
|
2011-03-03 07:24:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 16:28:07 +00:00
|
|
|
std::unique_ptr<MCObjectWriter>
|
2017-10-09 23:53:15 +00:00
|
|
|
llvm::createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
|
|
|
|
raw_pwrite_stream &OS, bool IsLittleEndian) {
|
2018-05-21 19:30:59 +00:00
|
|
|
return llvm::make_unique<ELFSingleObjectWriter>(std::move(MOTW), OS,
|
|
|
|
IsLittleEndian);
|
2011-12-21 17:30:17 +00:00
|
|
|
}
|
2018-05-21 19:44:54 +00:00
|
|
|
|
|
|
|
std::unique_ptr<MCObjectWriter>
|
|
|
|
llvm::createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
|
|
|
|
raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
|
|
|
|
bool IsLittleEndian) {
|
|
|
|
return llvm::make_unique<ELFDwoObjectWriter>(std::move(MOTW), OS, DwoOS,
|
|
|
|
IsLittleEndian);
|
|
|
|
}
|