2015-09-22 05:38:08 +08:00
|
|
|
//===- OutputSections.h -----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_ELF_OUTPUT_SECTIONS_H
|
|
|
|
#define LLD_ELF_OUTPUT_SECTIONS_H
|
|
|
|
|
2016-03-14 04:28:29 +08:00
|
|
|
#include "Config.h"
|
2017-03-09 06:36:28 +08:00
|
|
|
#include "InputSection.h"
|
2017-07-28 03:22:43 +08:00
|
|
|
#include "LinkerScript.h"
|
2016-06-20 05:39:37 +08:00
|
|
|
#include "Relocations.h"
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2015-09-22 05:38:08 +08:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
|
|
|
|
namespace lld {
|
2016-02-28 08:25:54 +08:00
|
|
|
namespace elf {
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2016-12-20 01:01:01 +08:00
|
|
|
struct PhdrEntry;
|
2015-09-22 05:38:08 +08:00
|
|
|
class SymbolBody;
|
2016-07-22 04:18:30 +08:00
|
|
|
struct EhSectionPiece;
|
2017-03-07 05:17:18 +08:00
|
|
|
class EhInputSection;
|
2017-02-24 00:49:07 +08:00
|
|
|
class InputSection;
|
2017-02-23 10:28:28 +08:00
|
|
|
class InputSectionBase;
|
2017-03-07 04:23:56 +08:00
|
|
|
class MergeInputSection;
|
2017-02-24 23:07:30 +08:00
|
|
|
class OutputSection;
|
2017-07-27 06:13:32 +08:00
|
|
|
template <class ELFT> class ObjFile;
|
2016-04-28 04:22:31 +08:00
|
|
|
template <class ELFT> class SharedFile;
|
2017-02-27 07:35:34 +08:00
|
|
|
class SharedSymbol;
|
2017-03-01 03:29:55 +08:00
|
|
|
class DefinedRegular;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
// This represents a section in an output file.
|
2017-02-24 23:07:30 +08:00
|
|
|
// It is composed of multiple InputSections.
|
2015-09-22 08:16:19 +08:00
|
|
|
// The writer creates multiple OutputSections and assign them unique,
|
2015-09-22 05:38:08 +08:00
|
|
|
// non-overlapping file offsets and VAs.
|
2017-07-28 03:22:43 +08:00
|
|
|
class OutputSection final : public BaseCommand, public SectionBase {
|
2015-09-22 05:38:08 +08:00
|
|
|
public:
|
2017-02-24 23:07:30 +08:00
|
|
|
OutputSection(StringRef Name, uint32_t Type, uint64_t Flags);
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2017-03-09 06:36:28 +08:00
|
|
|
static bool classof(const SectionBase *S) {
|
|
|
|
return S->kind() == SectionBase::Output;
|
|
|
|
}
|
2017-10-11 10:28:28 +08:00
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
static bool classof(const BaseCommand *C);
|
2017-03-09 06:36:28 +08:00
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
uint64_t getLMA() const { return Addr + LMAOffset; }
|
|
|
|
template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2015-10-16 04:55:22 +08:00
|
|
|
unsigned SectionIndex;
|
2017-05-12 22:52:22 +08:00
|
|
|
unsigned SortRank;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2016-07-27 22:10:56 +08:00
|
|
|
uint32_t getPhdrFlags() const;
|
2016-07-14 13:46:24 +08:00
|
|
|
|
2017-09-07 18:53:07 +08:00
|
|
|
// Pointer to the PT_LOAD segment, which this section resides in. This field
|
|
|
|
// is used to correctly compute file offset of a section. When two sections
|
|
|
|
// share the same load segment, difference between their file offsets should
|
|
|
|
// be equal to difference between their virtual addresses. To compute some
|
|
|
|
// section offset we use the following formula: Off = Off_first + VA -
|
|
|
|
// VA_first, where Off_first and VA_first is file offset and VA of first
|
|
|
|
// section in PT_LOAD.
|
|
|
|
PhdrEntry *PtLoad = nullptr;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2017-06-07 17:20:35 +08:00
|
|
|
// Pointer to a relocation section for this section. Usually nullptr because
|
|
|
|
// we consume relocations, but if --emit-relocs is specified (which is rare),
|
|
|
|
// it may have a non-null value.
|
|
|
|
OutputSection *RelocationSection = nullptr;
|
|
|
|
|
2016-11-09 09:42:41 +08:00
|
|
|
// The following fields correspond to Elf_Shdr members.
|
2016-11-10 07:23:45 +08:00
|
|
|
uint64_t Size = 0;
|
|
|
|
uint64_t Offset = 0;
|
|
|
|
uint64_t LMAOffset = 0;
|
|
|
|
uint64_t Addr = 0;
|
2016-11-09 09:42:41 +08:00
|
|
|
uint32_t ShName = 0;
|
2016-08-11 02:10:41 +08:00
|
|
|
|
2017-10-07 08:43:31 +08:00
|
|
|
void addSection(InputSection *IS);
|
2016-11-29 16:05:44 +08:00
|
|
|
|
|
|
|
// Location in the output buffer.
|
|
|
|
uint8_t *Loc = nullptr;
|
2017-07-28 03:22:43 +08:00
|
|
|
|
|
|
|
// The following members are normally only used in linker scripts.
|
|
|
|
MemoryRegion *MemRegion = nullptr;
|
|
|
|
Expr AddrExpr;
|
|
|
|
Expr AlignExpr;
|
|
|
|
Expr LMAExpr;
|
|
|
|
Expr SubalignExpr;
|
2017-10-11 09:50:56 +08:00
|
|
|
std::vector<BaseCommand *> SectionCommands;
|
2017-07-28 03:22:43 +08:00
|
|
|
std::vector<StringRef> Phdrs;
|
|
|
|
llvm::Optional<uint32_t> Filler;
|
|
|
|
ConstraintKind Constraint = ConstraintKind::NoConstraint;
|
|
|
|
std::string Location;
|
|
|
|
std::string MemoryRegionName;
|
|
|
|
bool Noload = false;
|
|
|
|
|
|
|
|
template <class ELFT> void finalize();
|
|
|
|
template <class ELFT> void writeTo(uint8_t *Buf);
|
|
|
|
template <class ELFT> void maybeCompress();
|
|
|
|
|
|
|
|
void sort(std::function<int(InputSectionBase *S)> Order);
|
|
|
|
void sortInitFini();
|
|
|
|
void sortCtorsDtors();
|
2017-10-07 05:42:37 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Used for implementation of --compress-debug-sections option.
|
|
|
|
std::vector<uint8_t> ZDebugHeader;
|
|
|
|
llvm::SmallVector<char, 1> CompressedData;
|
|
|
|
|
|
|
|
uint32_t getFiller();
|
2015-09-22 05:38:08 +08:00
|
|
|
};
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
int getPriority(StringRef S);
|
|
|
|
|
2017-02-27 10:31:26 +08:00
|
|
|
// All output sections that are handled by the linker specially are
|
2015-10-08 03:18:16 +08:00
|
|
|
// globally accessible. Writer initializes them, so don't use them
|
|
|
|
// until Writer is initialized.
|
2017-02-27 10:31:26 +08:00
|
|
|
struct Out {
|
2016-11-02 07:12:51 +08:00
|
|
|
static uint8_t First;
|
2017-02-24 23:07:30 +08:00
|
|
|
static OutputSection *Opd;
|
[ELF2/PPC64] Resolve local-call relocations using the correct function-descriptor values
Under PPC64 ELF v1 ABI, the symbols associated with each function name don't
point directly to the code in the .text section (or similar), but rather to a
function descriptor structure in a special data section named .opd. The
elements in the .opd structure include a pointer to the actual code, and a the
relevant TOC base value. Both of these are themselves set by relocations.
When we have a local call, we need the relevant relocation to refer directly to
the target code, not to the function-descriptor in the .opd section. Only when
we have a .plt stub do we care about the address of the .opd function
descriptor itself.
So we make a few changes here:
1. Always write .opd first, so that its relocated data values are available
for later use when writing the text sections. Record a pointer to the .opd
structure, and its corresponding buffer.
2. When processing a relative branch relocation under ppc64, if the
destination points into the .opd section, read the code pointer out of the
function descriptor structure and use that instead.
This this, I can link, and run, a dynamically-compiled "hello world"
application on big-Endian PPC64/Linux (ELF v1 ABI) using lld.
llvm-svn: 250122
2015-10-13 07:16:53 +08:00
|
|
|
static uint8_t *OpdBuf;
|
2016-12-20 01:01:01 +08:00
|
|
|
static PhdrEntry *TlsPhdr;
|
2017-02-24 23:07:30 +08:00
|
|
|
static OutputSection *DebugInfo;
|
|
|
|
static OutputSection *ElfHeader;
|
|
|
|
static OutputSection *ProgramHeaders;
|
|
|
|
static OutputSection *PreinitArray;
|
|
|
|
static OutputSection *InitArray;
|
|
|
|
static OutputSection *FiniArray;
|
2015-10-08 03:18:16 +08:00
|
|
|
};
|
2015-10-10 03:34:55 +08:00
|
|
|
|
2017-01-05 22:35:41 +08:00
|
|
|
struct SectionKey {
|
2016-07-12 17:49:43 +08:00
|
|
|
StringRef Name;
|
2017-01-05 22:35:41 +08:00
|
|
|
uint64_t Flags;
|
2017-03-09 03:35:29 +08:00
|
|
|
uint32_t Alignment;
|
2016-07-12 17:49:43 +08:00
|
|
|
};
|
2017-07-18 19:55:35 +08:00
|
|
|
} // namespace elf
|
|
|
|
} // namespace lld
|
2017-10-07 05:42:37 +08:00
|
|
|
|
2017-02-17 03:23:15 +08:00
|
|
|
namespace llvm {
|
|
|
|
template <> struct DenseMapInfo<lld::elf::SectionKey> {
|
|
|
|
static lld::elf::SectionKey getEmptyKey();
|
|
|
|
static lld::elf::SectionKey getTombstoneKey();
|
|
|
|
static unsigned getHashValue(const lld::elf::SectionKey &Val);
|
|
|
|
static bool isEqual(const lld::elf::SectionKey &LHS,
|
|
|
|
const lld::elf::SectionKey &RHS);
|
|
|
|
};
|
2017-07-18 19:55:35 +08:00
|
|
|
} // namespace llvm
|
2017-10-07 05:42:37 +08:00
|
|
|
|
2017-02-17 03:23:15 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace elf {
|
2016-07-12 17:49:43 +08:00
|
|
|
// This class knows how to create an output section for a given
|
|
|
|
// input section. Output section type is determined by various
|
|
|
|
// factors, including input section's sh_flags, sh_type and
|
|
|
|
// linker scripts.
|
2017-02-27 10:31:48 +08:00
|
|
|
class OutputSectionFactory {
|
2016-07-12 17:49:43 +08:00
|
|
|
public:
|
2017-07-07 00:40:44 +08:00
|
|
|
OutputSectionFactory();
|
2017-01-05 22:52:46 +08:00
|
|
|
~OutputSectionFactory();
|
2017-02-27 10:31:48 +08:00
|
|
|
|
2017-10-07 07:34:43 +08:00
|
|
|
OutputSection *addInputSec(InputSectionBase *IS, StringRef OutsecName);
|
2017-02-17 01:32:26 +08:00
|
|
|
|
2016-07-12 17:49:43 +08:00
|
|
|
private:
|
2017-02-24 23:07:30 +08:00
|
|
|
llvm::SmallDenseMap<SectionKey, OutputSection *> Map;
|
2016-07-12 17:49:43 +08:00
|
|
|
};
|
|
|
|
|
2017-03-13 22:40:58 +08:00
|
|
|
uint64_t getHeaderSize();
|
2017-08-04 18:25:29 +08:00
|
|
|
void sortByOrder(llvm::MutableArrayRef<InputSection *> In,
|
|
|
|
std::function<int(InputSectionBase *S)> Order);
|
2016-09-23 00:47:21 +08:00
|
|
|
|
2017-06-14 07:26:31 +08:00
|
|
|
extern std::vector<OutputSection *> OutputSections;
|
2016-02-28 08:25:54 +08:00
|
|
|
} // namespace elf
|
2015-11-04 10:11:57 +08:00
|
|
|
} // namespace lld
|
|
|
|
|
2016-07-12 17:49:43 +08:00
|
|
|
#endif
|