summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/ELF/Mips/MipsELFWriters.h
blob: 7c417d1c75602373fd95bf86039493ccdda871b2 (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
//===- lib/ReaderWriter/ELF/Mips/MipsELFWriters.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_MIPS_MIPS_ELF_WRITERS_H
#define LLD_READER_WRITER_ELF_MIPS_MIPS_ELF_WRITERS_H

#include "MipsLinkingContext.h"
#include "OutputELFWriter.h"

namespace lld {
namespace elf {

template <class ELFT> class MipsTargetLayout;

template <typename ELFT> class MipsELFWriter {
public:
  MipsELFWriter(MipsLinkingContext &context,
                MipsTargetLayout<ELFT> &targetLayout)
      : _context(context), _targetLayout(targetLayout) {}

  void setELFHeader(ELFHeader<ELFT> &elfHeader) {
    elfHeader.e_version(1);
    elfHeader.e_ident(llvm::ELF::EI_VERSION, llvm::ELF::EV_CURRENT);
    elfHeader.e_ident(llvm::ELF::EI_OSABI, llvm::ELF::ELFOSABI_NONE);
    if (_targetLayout.findOutputSection(".got.plt"))
      elfHeader.e_ident(llvm::ELF::EI_ABIVERSION, 1);
    else
      elfHeader.e_ident(llvm::ELF::EI_ABIVERSION, 0);

    // FIXME (simon): Read elf flags from all inputs, check compatibility,
    // merge them and write result here.
    uint32_t flags = llvm::ELF::EF_MIPS_NOREORDER | llvm::ELF::EF_MIPS_ABI_O32 |
                     llvm::ELF::EF_MIPS_CPIC | llvm::ELF::EF_MIPS_ARCH_32R2;
    if (_context.getOutputELFType() == llvm::ELF::ET_DYN)
      flags |= EF_MIPS_PIC;
    elfHeader.e_flags(flags);
  }

  void finalizeMipsRuntimeAtomValues() {
    if (!_context.isDynamic())
      return;

    auto gotSection = _targetLayout.findOutputSection(".got");
    auto got = gotSection ? gotSection->virtualAddr() : 0;
    auto gp = gotSection ? got + _targetLayout.getGPOffset() : 0;

    setAtomValue("_GLOBAL_OFFSET_TABLE_", got);
    setAtomValue("_gp", gp);
    setAtomValue("_gp_disp", gp);
  }

  bool hasGlobalGOTEntry(const Atom *a) const {
    return _targetLayout.getGOTSection().hasGlobalGOTEntry(a);
  }

  std::unique_ptr<MipsRuntimeFile<ELFT>> createRuntimeFile() {
    auto file = llvm::make_unique<MipsRuntimeFile<ELFT>>(_context);
    if (_context.isDynamic()) {
      file->addAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
      file->addAbsoluteAtom("_gp");
      file->addAbsoluteAtom("_gp_disp");
    }
    return file;
  }

private:
  MipsLinkingContext &_context;
  MipsTargetLayout<ELFT> &_targetLayout;

  void setAtomValue(StringRef name, uint64_t value) {
    auto atom = _targetLayout.findAbsoluteAtom(name);
    assert(atom != _targetLayout.absoluteAtoms().end());
    (*atom)->_virtualAddr = value;
  }
};

} // elf
} // lld

#endif
OpenPOWER on IntegriCloud