2015-09-22 08:01:39 +08:00
|
|
|
//===- InputSection.h -------------------------------------------*- C++ -*-===//
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-09-22 08:01:39 +08:00
|
|
|
#ifndef LLD_ELF_INPUT_SECTION_H
|
|
|
|
#define LLD_ELF_INPUT_SECTION_H
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
#include "lld/Core/LLVM.h"
|
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
|
|
|
|
|
|
|
template <class ELFT> class ObjectFile;
|
2015-08-25 04:06:32 +08:00
|
|
|
template <class ELFT> class OutputSection;
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT> class OutputSectionBase;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
// This corresponds to a section of an input file.
|
2015-10-20 05:00:02 +08:00
|
|
|
template <class ELFT> class InputSectionBase {
|
|
|
|
protected:
|
2015-09-15 20:43:09 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
2015-09-22 06:01:00 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
2015-08-25 03:28:31 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
2015-10-20 05:00:02 +08:00
|
|
|
const Elf_Shdr *Header;
|
|
|
|
|
|
|
|
// The file this section is from.
|
|
|
|
ObjectFile<ELFT> *File;
|
2015-08-14 06:21:37 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
public:
|
2015-10-20 05:00:02 +08:00
|
|
|
enum Kind { Regular, Merge };
|
|
|
|
Kind SectionKind;
|
|
|
|
|
|
|
|
InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
|
|
|
|
Kind SectionKind);
|
|
|
|
OutputSectionBase<ELFT> *OutSec = nullptr;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
// Returns the size of this section (even if this is a common or BSS.)
|
2015-08-14 06:21:37 +08:00
|
|
|
size_t getSize() const { return Header->sh_size; }
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
static InputSectionBase<ELFT> Discarded;
|
2015-08-14 06:21:37 +08:00
|
|
|
|
|
|
|
StringRef getSectionName() const;
|
|
|
|
const Elf_Shdr *getSectionHdr() const { return Header; }
|
2015-09-24 23:11:50 +08:00
|
|
|
ObjectFile<ELFT> *getFile() const { return File; }
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
// The writer sets and uses the addresses.
|
2015-09-05 08:25:33 +08:00
|
|
|
uintX_t getAlign() {
|
|
|
|
// The ELF spec states that a value of 0 means the section has no alignment
|
|
|
|
// constraits.
|
|
|
|
return std::max<uintX_t>(Header->sh_addralign, 1);
|
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
uintX_t getOffset(const Elf_Sym &Sym) const;
|
|
|
|
ArrayRef<uint8_t> getSectionData() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
InputSectionBase<ELFT>
|
|
|
|
InputSectionBase<ELFT>::Discarded(nullptr, nullptr,
|
|
|
|
InputSectionBase<ELFT>::Regular);
|
|
|
|
|
|
|
|
// This corresponds to a SHF_MERGE section of an input file.
|
|
|
|
template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> {
|
|
|
|
typedef InputSectionBase<ELFT> Base;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
|
|
|
|
static bool classof(const InputSectionBase<ELFT> *S);
|
|
|
|
uintX_t getOffset(uintX_t Offset) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This corresponds to a non SHF_MERGE section of an input file.
|
|
|
|
template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
|
|
|
|
typedef InputSectionBase<ELFT> Base;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
|
|
|
|
|
|
|
|
// Write this section to a mmap'ed file, assuming Buf is pointing to
|
|
|
|
// beginning of the output section.
|
|
|
|
void writeTo(uint8_t *Buf);
|
|
|
|
|
2015-08-28 07:15:56 +08:00
|
|
|
// Relocation sections that refer to this one.
|
|
|
|
SmallVector<const Elf_Shdr *, 1> RelocSections;
|
|
|
|
|
2015-10-15 05:00:23 +08:00
|
|
|
// The offset from beginning of the output sections this section was assigned
|
|
|
|
// to. The writer sets a value.
|
2015-10-15 09:58:40 +08:00
|
|
|
uint64_t OutSecOff = 0;
|
2015-10-15 05:00:23 +08:00
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
static bool classof(const InputSectionBase<ELFT> *S);
|
2015-10-10 03:25:07 +08:00
|
|
|
|
2015-08-14 06:21:37 +08:00
|
|
|
private:
|
2015-09-22 06:01:00 +08:00
|
|
|
template <bool isRela>
|
2015-10-13 05:19:18 +08:00
|
|
|
void relocate(uint8_t *Buf, uint8_t *BufEnd,
|
2015-09-22 06:01:00 +08:00
|
|
|
llvm::iterator_range<
|
|
|
|
const llvm::object::Elf_Rel_Impl<ELFT, isRela> *> Rels,
|
2015-10-08 03:18:16 +08:00
|
|
|
const ObjectFile<ELFT> &File, uintX_t BaseAddr);
|
2015-07-25 05:03:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace elf2
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|