1 //===- ARMToARMStub.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
10 #include "ARMToARMStub.h"
11 #include "ARMLDBackend.h"
12
13 #include "mcld/Fragment/Relocation.h"
14 #include "mcld/LD/LDSymbol.h"
15 #include "mcld/LD/ResolveInfo.h"
16
17 #include <llvm/Support/ELF.h>
18
19 namespace mcld {
20
21 //===----------------------------------------------------------------------===//
22 // ARMToARMStub
23 //===----------------------------------------------------------------------===//
24 const uint32_t ARMToARMStub::PIC_TEMPLATE[] = {
25 0xe59fc000, // ldr r12, [pc]
26 0xe08ff00c, // add pc, pc, ip
27 0x0 // dcd R_ARM_REL32(X-4)
28 };
29
30 const uint32_t ARMToARMStub::TEMPLATE[] = {
31 0xe51ff004, // ldr pc, [pc, #-4]
32 0x0 // dcd R_ARM_ABS32(X)
33 };
34
ARMToARMStub(bool pIsOutputPIC)35 ARMToARMStub::ARMToARMStub(bool pIsOutputPIC)
36 : m_pData(NULL), m_Name("A2A_prototype"), m_Size(0x0) {
37 if (pIsOutputPIC) {
38 m_pData = PIC_TEMPLATE;
39 m_Size = sizeof(PIC_TEMPLATE);
40 addFixup(8u, -4, llvm::ELF::R_ARM_REL32);
41 } else {
42 m_pData = TEMPLATE;
43 m_Size = sizeof(TEMPLATE);
44 addFixup(4u, 0x0, llvm::ELF::R_ARM_ABS32);
45 }
46 }
47
48 /// for doClone
ARMToARMStub(const uint32_t * pData,size_t pSize,const_fixup_iterator pBegin,const_fixup_iterator pEnd)49 ARMToARMStub::ARMToARMStub(const uint32_t* pData,
50 size_t pSize,
51 const_fixup_iterator pBegin,
52 const_fixup_iterator pEnd)
53 : m_pData(pData), m_Name("A2A_veneer"), m_Size(pSize) {
54 for (const_fixup_iterator it = pBegin, ie = pEnd; it != ie; ++it)
55 addFixup(**it);
56 }
57
~ARMToARMStub()58 ARMToARMStub::~ARMToARMStub() {
59 }
60
isMyDuty(const class Relocation & pReloc,uint64_t pSource,uint64_t pTargetSymValue) const61 bool ARMToARMStub::isMyDuty(const class Relocation& pReloc,
62 uint64_t pSource,
63 uint64_t pTargetSymValue) const {
64 bool result = false;
65 // Check if the branch target is ARM
66 if ((pTargetSymValue & 0x1) == 0x0) {
67 switch (pReloc.type()) {
68 case llvm::ELF::R_ARM_PC24:
69 case llvm::ELF::R_ARM_CALL:
70 case llvm::ELF::R_ARM_JUMP24:
71 case llvm::ELF::R_ARM_PLT32: {
72 // Check if the branch target is too far
73 uint64_t dest = pTargetSymValue + pReloc.addend() + 8u;
74 int64_t branch_offset = static_cast<int64_t>(dest) - pSource;
75 if ((branch_offset > ARMGNULDBackend::ARM_MAX_FWD_BRANCH_OFFSET) ||
76 (branch_offset < ARMGNULDBackend::ARM_MAX_BWD_BRANCH_OFFSET)) {
77 result = true;
78 }
79 break;
80 }
81 default:
82 break;
83 }
84 }
85 return result;
86 }
87
name() const88 const std::string& ARMToARMStub::name() const {
89 return m_Name;
90 }
91
getContent() const92 const uint8_t* ARMToARMStub::getContent() const {
93 return reinterpret_cast<const uint8_t*>(m_pData);
94 }
95
size() const96 size_t ARMToARMStub::size() const {
97 return m_Size;
98 }
99
alignment() const100 size_t ARMToARMStub::alignment() const {
101 return 4u;
102 }
103
doClone()104 Stub* ARMToARMStub::doClone() {
105 return new ARMToARMStub(m_pData, m_Size, fixup_begin(), fixup_end());
106 }
107
108 } // namespace mcld
109