1cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao//===- X86PLT.cpp ---------------------------------------------------------===//
25460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
35460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//                     The MCLinker Project
45460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
55460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// This file is distributed under the University of Illinois Open Source
65460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// License. See LICENSE.TXT for details.
75460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
85460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
9affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include "X86GOTPLT.h"
105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include "X86PLT.h"
11cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao
12cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao#include <new>
13cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao
145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <llvm/Support/ELF.h>
15cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao#include <llvm/Support/Casting.h>
16cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao
175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <mcld/MC/MCLDOutput.h>
18affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/MsgHandling.h>
195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
20cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao//===----------------------------------------------------------------------===//
21cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao// PLT entry data
22cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao//===----------------------------------------------------------------------===//
235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace {
245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoconst uint8_t x86_dyn_plt0[] = {
26affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0xff, 0xb3, 0x04, 0, 0, 0, // pushl  0x4(%ebx)
27affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0xff, 0xa3, 0x08, 0, 0, 0, // jmp    *0x8(%ebx)
28affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0x0f, 0x1f, 0x4,  0        // nopl   0(%eax)
295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoconst uint8_t x86_dyn_plt1[] = {
32affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0xff, 0xa3, 0, 0, 0, 0,    // jmp    *sym@GOT(%ebx)
33affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0x68, 0, 0, 0, 0,          // pushl  $offset
34affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0xe9, 0, 0, 0, 0           // jmp    plt0
355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoconst uint8_t x86_exec_plt0[] = {
38affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0xff, 0x35, 0, 0, 0, 0,    // pushl  .got + 4
39affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0xff, 0x25, 0, 0, 0, 0,    // jmp    *(.got + 8)
40affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0x0f, 0x1f, 0x4, 0         // nopl   0(%eax)
415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoconst uint8_t x86_exec_plt1[] = {
44affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0xff, 0x25, 0, 0, 0, 0,    // jmp    *(sym in .got)
45affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0x68, 0, 0, 0, 0,          // pushl  $offset
46affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  0xe9, 0, 0, 0, 0           // jmp    plt0
475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace mcld {
525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
53cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei LiaoX86PLT0::X86PLT0(SectionData* pParent, unsigned int pSize)
545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  : PLTEntry(pSize, pParent) { }
555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
56cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei LiaoX86PLT1::X86PLT1(SectionData* pParent, unsigned int pSize)
575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  : PLTEntry(pSize, pParent) { }
585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// X86PLT
61cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao//===----------------------------------------------------------------------===//
625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoX86PLT::X86PLT(LDSection& pSection,
63cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao               SectionData& pSectionData,
64affc150dc44fab1911775a49636d0ce85333b634Zonr Chang               X86GOTPLT &pGOTPLT,
65affc150dc44fab1911775a49636d0ce85333b634Zonr Chang               const Output& pOutput)
66affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  : PLT(pSection, pSectionData),
67affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    m_GOTPLT(pGOTPLT),
68affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    m_PLTEntryIterator(),
69affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    m_Output(pOutput)
705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  assert (Output::DynObj == pOutput.type() || Output::Exec == pOutput.type());
725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  if (Output::DynObj == pOutput.type()) {
735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      m_PLT0 = x86_dyn_plt0;
745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      m_PLT1 = x86_dyn_plt1;
755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      m_PLT0Size = sizeof (x86_dyn_plt0);
765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      m_PLT1Size = sizeof (x86_dyn_plt1);
775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  else {
795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      m_PLT0 = x86_exec_plt0;
805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      m_PLT1 = x86_exec_plt1;
815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      m_PLT0Size = sizeof (x86_exec_plt0);
825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      m_PLT1Size = sizeof (x86_exec_plt1);
835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  X86PLT0* plt0_entry = new X86PLT0(&m_SectionData, m_PLT0Size);
855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  m_Section.setSize(m_Section.size() + plt0_entry->getEntrySize());
875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  m_PLTEntryIterator = pSectionData.begin();
895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoX86PLT::~X86PLT()
925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaovoid X86PLT::reserveEntry(size_t pNum)
965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  X86PLT1* plt1_entry = 0;
985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  for (size_t i = 0; i < pNum; ++i) {
1005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    plt1_entry = new (std::nothrow) X86PLT1(&m_SectionData, m_PLT1Size);
1015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (!plt1_entry)
103cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao      fatal(diag::fail_allocate_memory_plt);
1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    m_Section.setSize(m_Section.size() + plt1_entry->getEntrySize());
1065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
107affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    // reserve corresponding entry in .got.plt
108affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    m_GOTPLT.reserveEntry(pNum);
1095460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
1105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
1115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoPLTEntry* X86PLT::getPLTEntry(const ResolveInfo& pSymbol, bool& pExist)
1135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
1145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   X86PLT1 *&PLTEntry = m_PLTEntryMap[&pSymbol];
1155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   pExist = 1;
1175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   if (!PLTEntry) {
1195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao     pExist = 0;
1205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao     // This will skip PLT0.
1225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao     ++m_PLTEntryIterator;
1235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao     assert(m_PLTEntryIterator != m_SectionData.end() &&
1245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            "The number of PLT Entries and ResolveInfo doesn't match");
1255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao     PLTEntry = llvm::cast<X86PLT1>(&(*m_PLTEntryIterator));
1265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   }
1275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   return PLTEntry;
1285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
1295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoGOTEntry* X86PLT::getGOTPLTEntry(const ResolveInfo& pSymbol, bool& pExist)
1315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
132affc150dc44fab1911775a49636d0ce85333b634Zonr Chang   return m_GOTPLT.getEntry(pSymbol, pExist);
1335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
1345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoX86PLT0* X86PLT::getPLT0() const {
1365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  iterator first = m_SectionData.getFragmentList().begin();
1385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
13967e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao  assert(first != m_SectionData.getFragmentList().end() &&
14067e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao         "FragmentList is empty, getPLT0 failed!");
1415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  X86PLT0* plt0 = &(llvm::cast<X86PLT0>(*first));
1435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  return plt0;
1455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
1465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// FIXME: It only works on little endian machine.
1485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaovoid X86PLT::applyPLT0() {
1495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  iterator first = m_SectionData.getFragmentList().begin();
1515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
15267e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao  assert(first != m_SectionData.getFragmentList().end() &&
15367e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao         "FragmentList is empty, applyPLT0 failed!");
1545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  X86PLT0* plt0 = &(llvm::cast<X86PLT0>(*first));
1565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  unsigned char* data = 0;
1585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  data = static_cast<unsigned char*>(malloc(plt0->getEntrySize()));
1595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  if (!data)
161cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao    fatal(diag::fail_allocate_memory_plt);
1625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  memcpy(data, m_PLT0, plt0->getEntrySize());
1645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  if (m_PLT0 == x86_exec_plt0) {
166affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    uint64_t got_base = m_GOTPLT.getSection().addr();
1675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    assert(got_base && ".got base address is NULL!");
1685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    uint32_t *offset = reinterpret_cast<uint32_t*>(data + 2);
1695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    *offset = got_base + 4;
1705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    offset = reinterpret_cast<uint32_t*>(data + 8);
1715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    *offset = got_base + 8;
1725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
1735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  plt0->setContent(data);
1755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
1765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// FIXME: It only works on little endian machine.
1785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaovoid X86PLT::applyPLT1() {
1795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  uint64_t plt_base = m_Section.addr();
1815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  assert(plt_base && ".plt base address is NULL!");
1825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
183affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  uint64_t got_base = m_GOTPLT.getSection().addr();
1845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  assert(got_base && ".got base address is NULL!");
1855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  X86PLT::iterator it = m_SectionData.begin();
1875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  X86PLT::iterator ie = m_SectionData.end();
18867e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao  assert(it != ie && "FragmentList is empty, applyPLT1 failed!");
1895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
190affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  uint64_t GOTEntrySize = m_GOTPLT.getEntrySize();
1915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // Skip GOT0
193affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  uint64_t GOTEntryOffset = GOTEntrySize * X86GOTPLT0Num;
1945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  //skip PLT0
1965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  uint64_t PLTEntryOffset = m_PLT0Size;
1975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ++it;
1985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  X86PLT1* plt1 = 0;
2005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  uint64_t PLTRelOffset = 0;
2025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  while (it != ie) {
2045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    plt1 = &(llvm::cast<X86PLT1>(*it));
2055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    unsigned char *data;
2065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    data = static_cast<unsigned char*>(malloc(plt1->getEntrySize()));
2075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (!data)
209cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao      fatal(diag::fail_allocate_memory_plt);
2105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    memcpy(data, m_PLT1, plt1->getEntrySize());
2125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    uint32_t* offset;
2145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    offset = reinterpret_cast<uint32_t*>(data + 2);
216affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    if (m_Output.type() == Output::DynObj) {
217affc150dc44fab1911775a49636d0ce85333b634Zonr Chang      *offset = GOTEntryOffset;
218affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    } else {
219affc150dc44fab1911775a49636d0ce85333b634Zonr Chang      // Exec
220affc150dc44fab1911775a49636d0ce85333b634Zonr Chang      *offset = got_base + GOTEntryOffset;
221affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    }
2225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    GOTEntryOffset += GOTEntrySize;
2235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    offset = reinterpret_cast<uint32_t*>(data + 7);
2255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    *offset = PLTRelOffset;
2265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    PLTRelOffset += sizeof (llvm::ELF::Elf32_Rel);
2275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    offset = reinterpret_cast<uint32_t*>(data + 12);
2295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    *offset = -(PLTEntryOffset + 12 + 4);
2305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    PLTEntryOffset += m_PLT1Size;
2315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    plt1->setContent(data);
2335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ++it;
2345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
2355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
236affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // apply .got.plt
237affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  m_GOTPLT.applyAllGOTPLT(plt_base, m_PLT0Size, m_PLT1Size);
2385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
2395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // end namespace mcld
241affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
242