summaryrefslogtreecommitdiff
path: root/lib/Target/Mips/MipsPLT.cpp
blob: aef25f93a213fab377c97d867c6c324729c2780a (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//===- MipsPLT.cpp --------------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <llvm/Support/Casting.h>
#include <llvm/Support/ELF.h>
#include "mcld/Support/MsgHandling.h"
#include "MipsGOTPLT.h"
#include "MipsPLT.h"

namespace {

const uint32_t PLT0[] = {
    0x3c1c0000,  // lui $28, %hi(&GOTPLT[0])
    0x8f990000,  // lw $25, %lo(&GOTPLT[0])($28)
    0x279c0000,  // addiu $28, $28, %lo(&GOTPLT[0])
    0x031cc023,  // subu $24, $24, $28
    0x03e07821,  // move $15, $31
    0x0018c082,  // srl $24, $24, 2
    0x0320f809,  // jalr $25
    0x2718fffe   // subu $24, $24, 2
};

const uint32_t PLTA[] = {
    0x3c0f0000,  // lui $15, %hi(.got.plt entry)
    0x8df90000,  // l[wd] $25, %lo(.got.plt entry)($15)
    0x03200008,  // jr $25
    0x25f80000   // addiu $24, $15, %lo(.got.plt entry)
};

}  // anonymous namespace

namespace mcld {

//===----------------------------------------------------------------------===//
// MipsPLT0 Entry
//===----------------------------------------------------------------------===//
class MipsPLT0 : public PLT::Entry<sizeof(PLT0)> {
 public:
  MipsPLT0(SectionData& pParent) : PLT::Entry<sizeof(PLT0)>(pParent) {}
};

//===----------------------------------------------------------------------===//
// MipsPLTA Entry
//===----------------------------------------------------------------------===//
class MipsPLTA : public PLT::Entry<sizeof(PLTA)> {
 public:
  MipsPLTA(SectionData& pParent) : PLT::Entry<sizeof(PLTA)>(pParent) {}
};

//===----------------------------------------------------------------------===//
// MipsPLT
//===----------------------------------------------------------------------===//
MipsPLT::MipsPLT(LDSection& pSection) : PLT(pSection) {
  new MipsPLT0(*m_pSectionData);
  m_Last = m_pSectionData->begin();
}

void MipsPLT::finalizeSectionSize() {
  uint64_t size = sizeof(PLT0) + (m_pSectionData->size() - 1) * sizeof(PLTA);
  m_Section.setSize(size);

  uint32_t offset = 0;
  SectionData::iterator frag, fragEnd = m_pSectionData->end();
  for (frag = m_pSectionData->begin(); frag != fragEnd; ++frag) {
    frag->setOffset(offset);
    offset += frag->size();
  }
}

bool MipsPLT::hasPLT1() const {
  return m_pSectionData->size() > 1;
}

uint64_t MipsPLT::emit(MemoryRegion& pRegion) {
  uint64_t result = 0x0;
  iterator it = begin();

  unsigned char* buffer = pRegion.begin();
  memcpy(buffer, llvm::cast<MipsPLT0>((*it)).getValue(), MipsPLT0::EntrySize);
  result += MipsPLT0::EntrySize;
  ++it;

  MipsPLTA* plta = 0;
  for (iterator ie = end(); it != ie; ++it) {
    plta = &(llvm::cast<MipsPLTA>(*it));
    memcpy(buffer + result, plta->getValue(), MipsPLTA::EntrySize);
    result += MipsPLTA::EntrySize;
  }
  return result;
}

void MipsPLT::reserveEntry(size_t pNum) {
  for (size_t i = 0; i < pNum; ++i) {
    Fragment* entry = new (std::nothrow) MipsPLTA(*m_pSectionData);

    if (entry == NULL)
      fatal(diag::fail_allocate_memory_plt);
  }
}

Fragment* MipsPLT::consume() {
  ++m_Last;
  assert(m_Last != m_pSectionData->end() &&
         "The number of PLT Entries and ResolveInfo doesn't match");
  return &(*m_Last);
}

void MipsPLT::applyAllPLT(MipsGOTPLT& pGOTPLT) {
  assert(m_Section.addr() && ".plt base address is NULL!");

  size_t count = 0;
  for (iterator it = m_pSectionData->begin(); it != m_pSectionData->end();
       ++it) {
    PLTEntryBase* plt = &(llvm::cast<PLTEntryBase>(*it));

    if (it == m_pSectionData->begin()) {
      uint32_t* data = static_cast<uint32_t*>(malloc(plt->size()));

      if (!data)
        fatal(diag::fail_allocate_memory_plt);

      memcpy(data, PLT0, plt->size());

      uint64_t gotAddr = pGOTPLT.addr();

      data[0] |= ((gotAddr + 0x8000) >> 16) & 0xffff;
      data[1] |= gotAddr & 0xffff;
      data[2] |= gotAddr & 0xffff;

      plt->setValue(reinterpret_cast<unsigned char*>(data));
    } else {
      uint32_t* data = static_cast<uint32_t*>(malloc(plt->size()));

      if (!data)
        fatal(diag::fail_allocate_memory_plt);

      memcpy(data, PLTA, plt->size());

      uint64_t gotEntryAddr = pGOTPLT.getEntryAddr(count++);

      data[0] |= ((gotEntryAddr + 0x8000) >> 16) & 0xffff;
      data[1] |= gotEntryAddr & 0xffff;
      data[3] |= gotEntryAddr & 0xffff;

      plt->setValue(reinterpret_cast<unsigned char*>(data));
    }
  }
}

}  // namespace mcld