aboutsummaryrefslogtreecommitdiff
path: root/lib/ReaderWriter/ELF/X86_64/X86_64TargetHandler.h
blob: 6e3e58f8aed6e97905b42be788a90d0f2f39f9e2 (plain) (blame)
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
//===- lib/ReaderWriter/ELF/X86_64/X86_64TargetHandler.h ------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLD_READER_WRITER_ELF_X86_64_X86_64_TARGET_HANDLER_H
#define LLD_READER_WRITER_ELF_X86_64_X86_64_TARGET_HANDLER_H

#include "ELFReader.h"
#include "TargetLayout.h"
#include "X86_64LinkingContext.h"
#include "X86_64RelocationHandler.h"
#include "X86_64SectionChunks.h"
#include "lld/Core/Simple.h"

namespace lld {
namespace elf {


class X86_64TargetLayout : public TargetLayout<ELF64LE> {
public:
  X86_64TargetLayout(X86_64LinkingContext &ctx) : TargetLayout(ctx),
    _gotSection(new (this->_allocator) X86_64GOTSection(ctx)) {}

  AtomSection<ELF64LE> *
  createSection(StringRef name, int32_t type,
                DefinedAtom::ContentPermissions permissions,
                TargetLayout<ELF64LE>::SectionOrder order) override {
    if (type == DefinedAtom::typeGOT && name == ".got")
      return _gotSection;
    return TargetLayout<ELF64LE>::createSection(name, type, permissions, order);
  }

  void finalizeOutputSectionLayout() override {
    sortOutputSectionByPriority<ELF64LE>(".init_array");
    sortOutputSectionByPriority<ELF64LE>(".fini_array");
  }

 const X86_64GOTSection &getGOTSection() const { return *_gotSection; }

private:
  uint32_t getPriority(StringRef sectionName) const {
    StringRef priority = sectionName.drop_front().rsplit('.').second;
    uint32_t prio;
    if (priority.getAsInteger(10, prio))
      return std::numeric_limits<uint32_t>::max();
    return prio;
  }

  template <typename T> void sortOutputSectionByPriority(StringRef prefix) {
    OutputSection<T> *section = findOutputSection(prefix);
    if (!section)
      return;
    auto sections = section->sections();
    std::sort(sections.begin(), sections.end(),
              [&](Chunk<T> *lhs, Chunk<T> *rhs) {
                Section<T> *lhsSection = dyn_cast<Section<T>>(lhs);
                Section<T> *rhsSection = dyn_cast<Section<T>>(rhs);
                if (!lhsSection || !rhsSection)
                  return false;
                StringRef lhsName = lhsSection->inputSectionName();
                StringRef rhsName = rhsSection->inputSectionName();
                if (!lhsName.startswith(prefix) || !rhsName.startswith(prefix))
                  return false;
                return getPriority(lhsName) < getPriority(rhsName);
              });
  }

private:
  X86_64GOTSection *_gotSection;
};

class X86_64TargetHandler : public TargetHandler {
public:
  X86_64TargetHandler(X86_64LinkingContext &ctx);

  const TargetRelocationHandler &getRelocationHandler() const override {
    return *_relocationHandler;
  }

  std::unique_ptr<Reader> getObjReader() override {
    return llvm::make_unique<ELFReader<ELFFile<ELF64LE>>>(_ctx);
  }

  std::unique_ptr<Reader> getDSOReader() override {
    return llvm::make_unique<ELFReader<DynamicFile<ELF64LE>>>(_ctx);
  }

  std::unique_ptr<Writer> getWriter() override;

protected:
  X86_64LinkingContext &_ctx;
  std::unique_ptr<X86_64TargetLayout> _targetLayout;
  std::unique_ptr<X86_64TargetRelocationHandler> _relocationHandler;
};

} // end namespace elf
} // end namespace lld

#endif