1//===- X86GOTPLT.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 "X86GOTPLT.h"
10#include "X86PLT.h"
11
12#include <llvm/Support/Casting.h>
13
14#include <mcld/LD/LDSection.h>
15#include <mcld/LD/LDFileFormat.h>
16#include <mcld/Support/MsgHandling.h>
17
18namespace mcld {
19
20//===----------------------------------------------------------------------===//
21// X86_32GOTPLT
22//===----------------------------------------------------------------------===//
23X86_32GOTPLT::X86_32GOTPLT(LDSection& pSection)
24  : X86_32GOT(pSection)
25{
26  // create GOT0 entries
27  for (size_t i = 0; i < X86GOTPLT0Num; ++i)
28    create();
29}
30
31X86_32GOTPLT::~X86_32GOTPLT()
32{
33}
34
35bool X86_32GOTPLT::hasGOT1() const
36{
37  return (m_SectionData->size() > X86GOTPLT0Num);
38}
39
40void X86_32GOTPLT::applyGOT0(uint64_t pAddress)
41{
42  llvm::cast<X86_32GOTEntry>
43    (*(m_SectionData->getFragmentList().begin())).setValue(pAddress);
44}
45
46void X86_32GOTPLT::applyAllGOTPLT(const X86PLT& pPLT)
47{
48  iterator it = begin();
49  // skip GOT0
50  for (size_t i = 0; i < X86GOTPLT0Num; ++i)
51    ++it;
52  // address of corresponding plt entry
53  uint64_t plt_addr = pPLT.addr() + pPLT.getPLT0Size();
54  for (; it != end() ; ++it) {
55    llvm::cast<X86_32GOTEntry>(*it).setValue(plt_addr + 6);
56    plt_addr += pPLT.getPLT1Size();
57  }
58}
59
60//===----------------------------------------------------------------------===//
61// X86_64GOTPLT
62//===----------------------------------------------------------------------===//
63X86_64GOTPLT::X86_64GOTPLT(LDSection& pSection)
64  : X86_64GOT(pSection)
65{
66  for (size_t i = 0; i < X86GOTPLT0Num; ++i)
67    create();
68}
69
70X86_64GOTPLT::~X86_64GOTPLT()
71{
72}
73
74bool X86_64GOTPLT::hasGOT1() const
75{
76  return (m_SectionData->size() > X86GOTPLT0Num);
77}
78
79void X86_64GOTPLT::applyGOT0(uint64_t pAddress)
80{
81  llvm::cast<X86_64GOTEntry>
82    (*(m_SectionData->getFragmentList().begin())).setValue(pAddress);
83}
84
85void X86_64GOTPLT::applyAllGOTPLT(const X86PLT& pPLT)
86{
87  iterator it = begin();
88  // skip GOT0
89  for (size_t i = 0; i < X86GOTPLT0Num; ++i)
90    ++it;
91  // address of corresponding plt entry
92  uint64_t plt_addr = pPLT.addr() + pPLT.getPLT0Size();
93  for (; it != end() ; ++it) {
94    llvm::cast<X86_64GOTEntry>(*it).setValue(plt_addr + 6);
95    plt_addr += pPLT.getPLT1Size();
96  }
97}
98
99} //end mcld namespace
100