ARMPLT.cpp revision 67e37f1be98c926645219cfb47fab9e90d8c725c
1//===- ARMPLT.cpp -----------------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "ARMGOT.h"
10#include "ARMPLT.h"
11#include <llvm/Support/raw_ostream.h>
12#include <mcld/Support/MemoryRegion.h>
13#include <mcld/Support/MsgHandling.h>
14#include <new>
15
16namespace {
17
18const uint32_t arm_plt0[] = {
19  0xe52de004, // str   lr, [sp, #-4]!
20  0xe59fe004, // ldr   lr, [pc, #4]
21  0xe08fe00e, // add   lr, pc, lr
22  0xe5bef008, // ldr   pc, [lr, #8]!
23  0x00000000, // &GOT[0] - .
24};
25
26const uint32_t arm_plt1[] = {
27  0xe28fc600, // add   ip, pc, #0xNN00000
28  0xe28cca00, // add   ip, ip, #0xNN000
29  0xe5bcf000, // ldr   pc, [ip, #0xNNN]!
30};
31
32} // anonymous namespace
33
34using namespace mcld;
35
36ARMPLT0::ARMPLT0(llvm::MCSectionData* pParent)
37  : PLTEntry(sizeof(arm_plt0), pParent) {}
38
39ARMPLT1::ARMPLT1(llvm::MCSectionData* pParent)
40  : PLTEntry(sizeof(arm_plt1), pParent) {}
41
42//===----------------------------------------------------------------------===//
43// ARMPLT
44
45ARMPLT::ARMPLT(LDSection& pSection,
46               llvm::MCSectionData& pSectionData,
47               ARMGOT &pGOTPLT)
48  : PLT(pSection, pSectionData), m_GOT(pGOTPLT), m_PLTEntryIterator() {
49  ARMPLT0* plt0_entry = new ARMPLT0(&m_SectionData);
50
51  m_Section.setSize(m_Section.size() + plt0_entry->getEntrySize());
52
53  m_PLTEntryIterator = pSectionData.begin();
54}
55
56ARMPLT::~ARMPLT()
57{
58}
59
60void ARMPLT::reserveEntry(size_t pNum)
61{
62  ARMPLT1* plt1_entry = 0;
63
64  for (size_t i = 0; i < pNum; ++i) {
65    plt1_entry = new (std::nothrow) ARMPLT1(&m_SectionData);
66
67    if (!plt1_entry)
68      fatal(diag::fail_allocate_memory) << "ARMPLT1";
69
70    m_Section.setSize(m_Section.size() + plt1_entry->getEntrySize());
71
72    m_GOT.reserveGOTPLTEntry();
73  }
74}
75
76PLTEntry* ARMPLT::getPLTEntry(const ResolveInfo& pSymbol, bool& pExist)
77{
78   ARMPLT1 *&PLTEntry = m_PLTEntryMap[&pSymbol];
79
80   pExist = 1;
81
82   if (!PLTEntry) {
83     GOTEntry *&GOTPLTEntry = m_GOT.lookupGOTPLTMap(pSymbol);
84     assert(!GOTPLTEntry && "PLT entry and got.plt entry doesn't match!");
85
86     pExist = 0;
87
88     // This will skip PLT0.
89     ++m_PLTEntryIterator;
90     assert(m_PLTEntryIterator != m_SectionData.end() &&
91            "The number of PLT Entries and ResolveInfo doesn't match");
92
93     ARMGOT::iterator got_it = m_GOT.getNextGOTPLTEntry();
94     assert(got_it != m_GOT.getGOTPLTEnd() && "The number of GOTPLT and PLT doesn't match");
95
96     PLTEntry = llvm::cast<ARMPLT1>(&(*m_PLTEntryIterator));
97     GOTPLTEntry = llvm::cast<GOTEntry>(&(*got_it));
98   }
99
100   return PLTEntry;
101}
102
103GOTEntry* ARMPLT::getGOTPLTEntry(const ResolveInfo& pSymbol, bool& pExist)
104{
105   GOTEntry *&GOTPLTEntry = m_GOT.lookupGOTPLTMap(pSymbol);
106
107   pExist = 1;
108
109   if (!GOTPLTEntry) {
110     ARMPLT1 *&PLTEntry = m_PLTEntryMap[&pSymbol];
111     assert(!PLTEntry && "PLT entry and got.plt entry doesn't match!");
112
113     pExist = 0;
114
115     // This will skip PLT0.
116     ++m_PLTEntryIterator;
117     assert(m_PLTEntryIterator != m_SectionData.end() &&
118            "The number of PLT Entries and ResolveInfo doesn't match");
119
120     ARMGOT::iterator got_it = m_GOT.getNextGOTPLTEntry();
121     assert(got_it != m_GOT.getGOTPLTEnd() &&
122            "The number of GOTPLT and PLT doesn't match");
123
124     PLTEntry = llvm::cast<ARMPLT1>(&(*m_PLTEntryIterator));
125     GOTPLTEntry = llvm::cast<GOTEntry>(&(*got_it));
126   }
127
128   return GOTPLTEntry;
129}
130
131ARMPLT0* ARMPLT::getPLT0() const {
132
133  iterator first = m_SectionData.getFragmentList().begin();
134
135  assert(first != m_SectionData.getFragmentList().end() &&
136         "FragmentList is empty, getPLT0 failed!");
137
138  ARMPLT0* plt0 = &(llvm::cast<ARMPLT0>(*first));
139
140  return plt0;
141}
142
143void ARMPLT::applyPLT0() {
144
145  uint64_t plt_base = m_Section.addr();
146  assert(plt_base && ".plt base address is NULL!");
147
148  uint64_t got_base = m_GOT.getSection().addr();
149  assert(got_base && ".got base address is NULL!");
150
151  uint32_t offset = 0;
152
153  if (got_base > plt_base)
154    offset = got_base - (plt_base + 16);
155  else
156    offset = (plt_base + 16) - got_base;
157
158  iterator first = m_SectionData.getFragmentList().begin();
159
160  assert(first != m_SectionData.getFragmentList().end() &&
161         "FragmentList is empty, applyPLT0 failed!");
162
163  ARMPLT0* plt0 = &(llvm::cast<ARMPLT0>(*first));
164
165  uint32_t* data = 0;
166  data = static_cast<uint32_t*>(malloc(plt0->getEntrySize()));
167
168  if (!data)
169    fatal(diag::fail_allocate_memory) << "plt0";
170
171  memcpy(data, arm_plt0, plt0->getEntrySize());
172  data[4] = offset;
173
174  plt0->setContent(reinterpret_cast<unsigned char*>(data));
175}
176
177void ARMPLT::applyPLT1() {
178
179  uint64_t plt_base = m_Section.addr();
180  assert(plt_base && ".plt base address is NULL!");
181
182  uint64_t got_base = m_GOT.getSection().addr();
183  assert(got_base && ".got base address is NULL!");
184
185  ARMPLT::iterator it = m_SectionData.begin();
186  ARMPLT::iterator ie = m_SectionData.end();
187  assert(it != ie && "FragmentList is empty, applyPLT1 failed!");
188
189  uint32_t GOTEntrySize = m_GOT.getEntrySize();
190  uint32_t GOTEntryAddress =
191    got_base +  GOTEntrySize * 3;
192
193  uint64_t PLTEntryAddress =
194    plt_base + llvm::cast<ARMPLT0>((*it)).getEntrySize(); //Offset of PLT0
195
196  ++it; //skip PLT0
197  uint64_t PLT1EntrySize = llvm::cast<ARMPLT1>((*it)).getEntrySize();
198  ARMPLT1* plt1 = NULL;
199
200  uint32_t* Out = NULL;
201  while (it != ie) {
202    plt1 = &(llvm::cast<ARMPLT1>(*it));
203    Out = static_cast<uint32_t*>(malloc(plt1->getEntrySize()));
204
205    if (!Out)
206      fatal(diag::fail_allocate_memory) << "plt1";
207
208    // Offset is the distance between the last PLT entry and the associated
209    // GOT entry.
210    int32_t Offset = (GOTEntryAddress - (PLTEntryAddress + 8));
211
212    Out[0] = arm_plt1[0] | ((Offset >> 20) & 0xFF);
213    Out[1] = arm_plt1[1] | ((Offset >> 12) & 0xFF);
214    Out[2] = arm_plt1[2] | (Offset & 0xFFF);
215
216    plt1->setContent(reinterpret_cast<unsigned char*>(Out));
217    ++it;
218
219    GOTEntryAddress += GOTEntrySize;
220    PLTEntryAddress += PLT1EntrySize;
221  }
222
223  m_GOT.applyAllGOTPLT(plt_base);
224}
225
226uint64_t ARMPLT::emit(MemoryRegion& pRegion)
227{
228  uint64_t result = 0x0;
229  iterator it = begin();
230  unsigned int plt0_size = llvm::cast<ARMPLT0>((*it)).getEntrySize();
231
232  unsigned char* buffer = pRegion.getBuffer();
233  memcpy(buffer, llvm::cast<ARMPLT0>((*it)).getContent(), plt0_size);
234  result += plt0_size;
235  ++it;
236
237  ARMPLT1* plt1 = 0;
238  ARMPLT::iterator ie = end();
239  unsigned int entry_size = 0;
240  while (it != ie) {
241    plt1 = &(llvm::cast<ARMPLT1>(*it));
242    entry_size = plt1->getEntrySize();
243    memcpy(buffer + result, plt1->getContent(), entry_size);
244    result += entry_size;
245    ++it;
246  }
247  return result;
248}
249
250