X86PLT.cpp revision 5460a1f25d9ddecb5c70667267d66d51af177a99
1//===- X86PLT.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 "X86GOT.h" 10#include "X86PLT.h" 11#include <llvm/Support/raw_ostream.h> 12#include <llvm/Support/ErrorHandling.h> 13#include <llvm/Support/ELF.h> 14#include <mcld/MC/MCLDOutput.h> 15#include <new> 16 17namespace { 18 19const uint8_t x86_dyn_plt0[] = { 20 0xff, 0xb3, 0x04, 0, 0, 0, // pushl 0x4(%ebx) 21 0xff, 0xa3, 0x08, 0, 0, 0, // jmp *0x8(%ebx) 22 0xf, 0x1f, 0x4, 0 // nopl 0(%eax) 23}; 24 25const uint8_t x86_dyn_plt1[] = { 26 0xff, 0xa3, 0, 0, 0, 0, // jmp *sym@GOT(%ebx) 27 0x68, 0, 0, 0, 0, // pushl $offset 28 0xe9, 0, 0, 0, 0 // jmp plt0 29}; 30 31const uint8_t x86_exec_plt0[] = { 32 0xff, 0x35, 0, 0, 0, 0, // pushl .got + 4 33 0xff, 0x25, 0, 0, 0, 0, // jmp *(.got + 8) 34 0xf, 0x1f, 0x4, 0 // nopl 0(%eax) 35}; 36 37const uint8_t x86_exec_plt1[] = { 38 0xff, 0x25, 0, 0, 0, 0, // jmp *(sym in .got) 39 0x68, 0, 0, 0, 0, // pushl $offset 40 0xe9, 0, 0, 0, 0 // jmp plt0 41}; 42 43} 44 45namespace mcld { 46 47X86PLT0::X86PLT0(llvm::MCSectionData* pParent, unsigned int pSize) 48 : PLTEntry(pSize, pParent) { } 49 50X86PLT1::X86PLT1(llvm::MCSectionData* pParent, unsigned int pSize) 51 : PLTEntry(pSize, pParent) { } 52 53//===----------------------------------------------------------------------===// 54// X86PLT 55 56X86PLT::X86PLT(LDSection& pSection, 57 llvm::MCSectionData& pSectionData, 58 X86GOT &pGOTPLT, 59 const Output& pOutput) 60 : PLT(pSection, pSectionData), m_GOT(pGOTPLT), m_PLTEntryIterator() 61{ 62 assert (Output::DynObj == pOutput.type() || Output::Exec == pOutput.type()); 63 if (Output::DynObj == pOutput.type()) { 64 m_PLT0 = x86_dyn_plt0; 65 m_PLT1 = x86_dyn_plt1; 66 m_PLT0Size = sizeof (x86_dyn_plt0); 67 m_PLT1Size = sizeof (x86_dyn_plt1); 68 } 69 else { 70 m_PLT0 = x86_exec_plt0; 71 m_PLT1 = x86_exec_plt1; 72 m_PLT0Size = sizeof (x86_exec_plt0); 73 m_PLT1Size = sizeof (x86_exec_plt1); 74 } 75 X86PLT0* plt0_entry = new X86PLT0(&m_SectionData, m_PLT0Size); 76 77 m_Section.setSize(m_Section.size() + plt0_entry->getEntrySize()); 78 79 m_PLTEntryIterator = pSectionData.begin(); 80} 81 82X86PLT::~X86PLT() 83{ 84} 85 86void X86PLT::reserveEntry(size_t pNum) 87{ 88 X86PLT1* plt1_entry = 0; 89 GOTEntry* got_entry = 0; 90 91 for (size_t i = 0; i < pNum; ++i) { 92 plt1_entry = new (std::nothrow) X86PLT1(&m_SectionData, m_PLT1Size); 93 94 if (!plt1_entry) 95 llvm::report_fatal_error("Allocating new memory for X86PLT1 failed!"); 96 97 m_Section.setSize(m_Section.size() + plt1_entry->getEntrySize()); 98 99 got_entry= new (std::nothrow) GOTEntry(0, m_GOT.getEntrySize(), 100 &(m_GOT.m_SectionData)); 101 102 if (!got_entry) 103 llvm::report_fatal_error("Allocating new memory for GOT failed!"); 104 105 m_GOT.m_Section.setSize(m_GOT.m_Section.size() + m_GOT.f_EntrySize); 106 107 ++(m_GOT.m_GOTPLTNum); 108 ++(m_GOT.m_GeneralGOTIterator); 109 } 110} 111 112PLTEntry* X86PLT::getPLTEntry(const ResolveInfo& pSymbol, bool& pExist) 113{ 114 X86PLT1 *&PLTEntry = m_PLTEntryMap[&pSymbol]; 115 116 pExist = 1; 117 118 if (!PLTEntry) { 119 GOTEntry *&GOTPLTEntry = m_GOT.m_GOTPLTMap[&pSymbol]; 120 assert(!GOTPLTEntry && "PLT entry and got.plt entry doesn't match!"); 121 122 pExist = 0; 123 124 // This will skip PLT0. 125 ++m_PLTEntryIterator; 126 assert(m_PLTEntryIterator != m_SectionData.end() && 127 "The number of PLT Entries and ResolveInfo doesn't match"); 128 ++(m_GOT.m_GOTPLTIterator); 129 130 PLTEntry = llvm::cast<X86PLT1>(&(*m_PLTEntryIterator)); 131 GOTPLTEntry = llvm::cast<GOTEntry>(&(*(m_GOT.m_GOTPLTIterator))); 132 } 133 134 return PLTEntry; 135} 136 137GOTEntry* X86PLT::getGOTPLTEntry(const ResolveInfo& pSymbol, bool& pExist) 138{ 139 GOTEntry *&GOTPLTEntry = m_GOT.m_GOTPLTMap[&pSymbol]; 140 141 pExist = 1; 142 143 if (!GOTPLTEntry) { 144 X86PLT1 *&PLTEntry = m_PLTEntryMap[&pSymbol]; 145 assert(!PLTEntry && "PLT entry and got.plt entry doesn't match!"); 146 147 pExist = 0; 148 149 // This will skip PLT0. 150 ++m_PLTEntryIterator; 151 assert(m_PLTEntryIterator != m_SectionData.end() && 152 "The number of PLT Entries and ResolveInfo doesn't match"); 153 ++(m_GOT.m_GOTPLTIterator); 154 155 PLTEntry = llvm::cast<X86PLT1>(&(*m_PLTEntryIterator)); 156 GOTPLTEntry = llvm::cast<GOTEntry>(&(*(m_GOT.m_GOTPLTIterator))); 157 } 158 159 return GOTPLTEntry; 160} 161 162X86PLT0* X86PLT::getPLT0() const { 163 164 iterator first = m_SectionData.getFragmentList().begin(); 165 iterator end = m_SectionData.getFragmentList().end(); 166 167 assert(first!=end && "FragmentList is empty, getPLT0 failed!"); 168 169 X86PLT0* plt0 = &(llvm::cast<X86PLT0>(*first)); 170 171 return plt0; 172} 173 174// FIXME: It only works on little endian machine. 175void X86PLT::applyPLT0() { 176 177 iterator first = m_SectionData.getFragmentList().begin(); 178 iterator end = m_SectionData.getFragmentList().end(); 179 180 assert(first!=end && "FragmentList is empty, applyPLT0 failed!"); 181 182 X86PLT0* plt0 = &(llvm::cast<X86PLT0>(*first)); 183 184 unsigned char* data = 0; 185 data = static_cast<unsigned char*>(malloc(plt0->getEntrySize())); 186 187 if (!data) 188 llvm::report_fatal_error("Allocating new memory for plt0 failed!"); 189 190 memcpy(data, m_PLT0, plt0->getEntrySize()); 191 192 if (m_PLT0 == x86_exec_plt0) { 193 uint64_t got_base = m_GOT.getSection().addr(); 194 assert(got_base && ".got base address is NULL!"); 195 uint32_t *offset = reinterpret_cast<uint32_t*>(data + 2); 196 *offset = got_base + 4; 197 offset = reinterpret_cast<uint32_t*>(data + 8); 198 *offset = got_base + 8; 199 } 200 201 plt0->setContent(data); 202} 203 204// FIXME: It only works on little endian machine. 205void X86PLT::applyPLT1() { 206 207 uint64_t plt_base = m_Section.addr(); 208 assert(plt_base && ".plt base address is NULL!"); 209 210 uint64_t got_base = m_GOT.getSection().addr(); 211 assert(got_base && ".got base address is NULL!"); 212 213 X86PLT::iterator it = m_SectionData.begin(); 214 X86PLT::iterator ie = m_SectionData.end(); 215 assert(it!=ie && "FragmentList is empty, applyPLT1 failed!"); 216 217 uint64_t GOTEntrySize = m_GOT.getEntrySize(); 218 219 // Skip GOT0 220 uint64_t GOTEntryOffset = GOTEntrySize * X86GOT0Num; 221 222 //skip PLT0 223 uint64_t PLTEntryOffset = m_PLT0Size; 224 ++it; 225 226 X86PLT1* plt1 = 0; 227 228 uint64_t PLTRelOffset = 0; 229 230 while (it != ie) { 231 plt1 = &(llvm::cast<X86PLT1>(*it)); 232 unsigned char *data; 233 data = static_cast<unsigned char*>(malloc(plt1->getEntrySize())); 234 235 if (!data) 236 llvm::report_fatal_error("Allocating new memory for plt1 failed!"); 237 238 memcpy(data, m_PLT1, plt1->getEntrySize()); 239 240 uint32_t* offset; 241 242 offset = reinterpret_cast<uint32_t*>(data + 2); 243 *offset = GOTEntryOffset; 244 GOTEntryOffset += GOTEntrySize; 245 246 offset = reinterpret_cast<uint32_t*>(data + 7); 247 *offset = PLTRelOffset; 248 PLTRelOffset += sizeof (llvm::ELF::Elf32_Rel); 249 250 offset = reinterpret_cast<uint32_t*>(data + 12); 251 *offset = -(PLTEntryOffset + 12 + 4); 252 PLTEntryOffset += m_PLT1Size; 253 254 plt1->setContent(data); 255 ++it; 256 } 257 258 unsigned int GOTPLTNum = m_GOT.getGOTPLTNum(); 259 260 if (GOTPLTNum != 0) { 261 X86GOT::iterator gotplt_it = m_GOT.getLastGOT0(); 262 X86GOT::iterator list_ie = m_GOT.getSectionData().getFragmentList().end(); 263 264 ++gotplt_it; 265 uint64_t PLTEntryAddress = plt_base + m_PLT0Size; 266 for (unsigned int i = 0; i < GOTPLTNum; ++i) { 267 if (gotplt_it == list_ie) 268 llvm::report_fatal_error( 269 "The number of got.plt entries is inconsistent!"); 270 271 llvm::cast<GOTEntry>(*gotplt_it).setContent(PLTEntryAddress + 6); 272 PLTEntryAddress += m_PLT1Size; 273 ++gotplt_it; 274 } 275 } 276} 277 278} // end namespace mcld 279 280