1//===- Relocation.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 <llvm/MC/MCAssembler.h>
10//#include <mcld/MC/MCLDInfo.h>
11#include <mcld/LD/Relocation.h>
12#include <mcld/LD/RelocationFactory.h>
13#include <mcld/LD/Layout.h>
14
15using namespace mcld;
16
17Relocation::Relocation(Relocation::Type pType,
18                       MCFragmentRef* pTargetRef,
19                       Relocation::Address pAddend,
20                       Relocation::DWord pTargetData)
21  : MCFragment(llvm::MCFragment::FT_Reloc),
22    m_Type(pType),
23    m_TargetData(pTargetData),
24    m_pSymInfo(NULL),
25    m_Addend(pAddend)
26{
27  if(NULL != pTargetRef)
28     m_TargetAddress.assign(*pTargetRef->frag(), pTargetRef->offset()) ;
29}
30
31Relocation::~Relocation()
32{
33}
34
35Relocation::Address Relocation::place(const Layout& pLayout) const
36{
37  Address sect_addr = pLayout.getOutputLDSection(*(m_TargetAddress.frag()))->addr();
38  return sect_addr + pLayout.getOutputOffset(m_TargetAddress);
39}
40
41Relocation::Address Relocation::symValue() const
42{
43  if(m_pSymInfo->type() == ResolveInfo::Section &&
44     m_pSymInfo->outSymbol()->hasFragRef()) {
45    return llvm::cast<LDSection>(
46      m_pSymInfo->outSymbol()->fragRef()->frag()->getParent()->getSection()).addr();
47  }
48  return m_pSymInfo->outSymbol()->value();
49}
50
51void Relocation::apply(RelocationFactory& pRelocFactory,
52                       const MCLDInfo& pLDInfo)
53{
54  pRelocFactory.applyRelocation(*this, pLDInfo);
55}
56
57void Relocation::setType(Type pType)
58{
59  m_Type = pType;
60}
61
62void Relocation::setAddend(Address pAddend)
63{
64  m_Addend = pAddend;
65}
66
67void Relocation::setSymInfo(ResolveInfo* pSym)
68{
69  m_pSymInfo = pSym;
70}
71
72Relocation::DWord& Relocation::target()
73{
74  return m_TargetData;
75}
76
77const Relocation::DWord& Relocation::target() const
78{
79  return m_TargetData;
80}
81
82