• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 using namespace mcld;
16 
Relocation(Relocation::Type pType,MCFragmentRef * pTargetRef,Relocation::Address pAddend,Relocation::DWord pTargetData)17 Relocation::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 
~Relocation()31 Relocation::~Relocation()
32 {
33 }
34 
place(const Layout & pLayout) const35 Relocation::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 
symValue() const41 Relocation::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 
apply(RelocationFactory & pRelocFactory,const MCLDInfo & pLDInfo)51 void Relocation::apply(RelocationFactory& pRelocFactory,
52                        const MCLDInfo& pLDInfo)
53 {
54   pRelocFactory.applyRelocation(*this, pLDInfo);
55 }
56 
setType(Type pType)57 void Relocation::setType(Type pType)
58 {
59   m_Type = pType;
60 }
61 
setAddend(Address pAddend)62 void Relocation::setAddend(Address pAddend)
63 {
64   m_Addend = pAddend;
65 }
66 
setSymInfo(ResolveInfo * pSym)67 void Relocation::setSymInfo(ResolveInfo* pSym)
68 {
69   m_pSymInfo = pSym;
70 }
71 
target()72 Relocation::DWord& Relocation::target()
73 {
74   return m_TargetData;
75 }
76 
target() const77 const Relocation::DWord& Relocation::target() const
78 {
79   return m_TargetData;
80 }
81 
82