summaryrefslogtreecommitdiffstats
path: root/lld/ELF/Chunks.cpp
blob: c6e4e436271ccbda750385a4407aa176f230a31e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//===- Chunks.cpp ---------------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "Chunks.h"
#include "InputFiles.h"
#include "Writer.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;
using namespace llvm::object;
using namespace llvm::support::endian;
using namespace llvm::ELF;

using namespace lld;
using namespace lld::elfv2;

template <class ELFT>
SectionChunk<ELFT>::SectionChunk(elfv2::ObjectFile<ELFT> *F, const Elf_Shdr *H,
                                 uint32_t SI)
    : File(F), Header(H), SectionIndex(SI) {
  // Initialize SectionName.
  SectionName = *File->getObj()->getSectionName(Header);

  Align = Header->sh_addralign;

  // When a new chunk is created, we don't if if it's going to make it
  // to the final output. Initially all sections are unmarked in terms
  // of garbage collection. The writer will call markLive() to mark
  // all reachable section chunks.
  Live = false;

  Root = true;
}

template <class ELFT> void SectionChunk<ELFT>::writeTo(uint8_t *Buf) {
  if (!hasData())
    return;
  // Copy section contents from source object file to output file.
  ArrayRef<uint8_t> Data = *File->getObj()->getSectionContents(Header);
  memcpy(Buf + FileOff, Data.data(), Data.size());

  // FIXME: Relocations
}

template <class ELFT> void SectionChunk<ELFT>::mark() {
  assert(!Live);
  Live = true;

  // Mark all symbols listed in the relocation table for this section.
  // FIXME: Relocations
}

static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }

template <class ELFT>
void SectionChunk<ELFT>::applyReloc(uint8_t *Buf, const Elf_Rela *Rel) {
  // FIXME: Relocations
}

template <class ELFT>
void SectionChunk<ELFT>::applyReloc(uint8_t *Buf, const Elf_Rel *Rel) {}

template <class ELFT> bool SectionChunk<ELFT>::hasData() const {
  return Header->sh_type != SHT_NOBITS;
}

template <class ELFT> uint32_t SectionChunk<ELFT>::getFlags() const {
  return Header->sh_flags;
}

// Prints "Discarded <symbol>" for all external function symbols.
template <class ELFT> void SectionChunk<ELFT>::printDiscardedMessage() {
  auto Obj = File->getObj();

  for (auto &&Sym : Obj->symbols()) {
    auto Sec = Obj->getSection(&Sym);
    if (Sec && *Sec != Header)
      continue;
    if (Sym.getType() != STT_FUNC)
      continue;
    if (auto Name = Obj->getStaticSymbolName(&Sym)) {
      llvm::outs() << "Discarded " << *Name << " from " << File->getShortName()
                   << "\n";
    }
  }
}

template <class ELFT>
const llvm::object::Elf_Shdr_Impl<ELFT> *SectionChunk<ELFT>::getSectionHdr() {
  return Header;
}

template <class ELFT>
CommonChunk<ELFT>::CommonChunk(const Elf_Sym *S)
    : Sym(S) {
  // Alignment is a section attribute, but common symbols don't
  // belong to any section. How do we know common data alignments?
  // Needs investigating. For now, we set a large number as an alignment.
  Align = 16;
}

template <class ELFT> uint32_t CommonChunk<ELFT>::getFlags() const {
  return PF_R | PF_W;
}

template class SectionChunk<llvm::object::ELF32LE>;
template class SectionChunk<llvm::object::ELF32BE>;
template class SectionChunk<llvm::object::ELF64LE>;
template class SectionChunk<llvm::object::ELF64BE>;

template class CommonChunk<llvm::object::ELF32LE>;
template class CommonChunk<llvm::object::ELF32BE>;
template class CommonChunk<llvm::object::ELF64LE>;
template class CommonChunk<llvm::object::ELF64BE>;
OpenPOWER on IntegriCloud