Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1 | //===- BranchIsland.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 | //===----------------------------------------------------------------------===// |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 9 | #include "mcld/LD/BranchIsland.h" |
| 10 | |
| 11 | #include "mcld/Fragment/AlignFragment.h" |
| 12 | #include "mcld/Fragment/Stub.h" |
| 13 | #include "mcld/LD/LDSection.h" |
| 14 | #include "mcld/LD/ResolveInfo.h" |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 15 | |
| 16 | #include <sstream> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 17 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 18 | namespace mcld { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 19 | |
Stephen Hines | cfcb224 | 2016-03-08 00:18:09 -0800 | [diff] [blame^] | 20 | //============================================================================// |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 21 | // BranchIsland |
Stephen Hines | cfcb224 | 2016-03-08 00:18:09 -0800 | [diff] [blame^] | 22 | //============================================================================// |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 23 | BranchIsland::BranchIsland(Fragment& pEntryFrag, size_t pMaxSize, size_t pIndex) |
| 24 | : m_Entry(pEntryFrag), |
| 25 | m_pExit(pEntryFrag.getNextNode()), |
| 26 | m_pRear(NULL), |
| 27 | m_MaxSize(pMaxSize), |
| 28 | m_Name("island-") { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 29 | // island name |
| 30 | std::ostringstream index; |
| 31 | index << pIndex; |
| 32 | m_Name.append(index.str()); |
| 33 | } |
| 34 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 35 | BranchIsland::~BranchIsland() { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /// fragment iterators of the island |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 39 | SectionData::iterator BranchIsland::begin() { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 40 | return ++iterator(&m_Entry); |
| 41 | } |
| 42 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 43 | SectionData::const_iterator BranchIsland::begin() const { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 44 | return ++iterator(&m_Entry); |
| 45 | } |
| 46 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 47 | SectionData::iterator BranchIsland::end() { |
| 48 | if (m_pExit != NULL) |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 49 | return iterator(m_pExit); |
| 50 | return m_Entry.getParent()->end(); |
| 51 | } |
| 52 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 53 | SectionData::const_iterator BranchIsland::end() const { |
| 54 | if (m_pExit != NULL) |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 55 | return iterator(m_pExit); |
| 56 | return m_Entry.getParent()->end(); |
| 57 | } |
| 58 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 59 | uint64_t BranchIsland::offset() const { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 60 | return m_Entry.getOffset() + m_Entry.size(); |
| 61 | } |
| 62 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 63 | size_t BranchIsland::size() const { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 64 | size_t size = 0x0; |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 65 | if (numOfStubs() != 0x0) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 66 | size = m_pRear->getOffset() + m_pRear->size() - |
| 67 | m_Entry.getNextNode()->getOffset(); |
| 68 | } |
| 69 | return size; |
| 70 | } |
| 71 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 72 | size_t BranchIsland::maxSize() const { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 73 | return m_MaxSize; |
| 74 | } |
| 75 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 76 | const std::string& BranchIsland::name() const { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 77 | return m_Name; |
| 78 | } |
| 79 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 80 | size_t BranchIsland::numOfStubs() const { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 81 | return m_StubMap.numOfEntries(); |
| 82 | } |
| 83 | |
| 84 | /// findStub - return true if there is a stub built from the given prototype |
| 85 | /// for the given relocation |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 86 | Stub* BranchIsland::findStub(const Stub* pPrototype, const Relocation& pReloc) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 87 | Key key(pPrototype, pReloc.symInfo()->outSymbol(), pReloc.addend()); |
| 88 | StubMapType::iterator it = m_StubMap.find(key); |
| 89 | if (it != m_StubMap.end()) { |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 90 | assert(it.getEntry()->value() != NULL); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 91 | return it.getEntry()->value(); |
| 92 | } |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | /// addStub - add a stub into the island |
| 97 | bool BranchIsland::addStub(const Stub* pPrototype, |
| 98 | const Relocation& pReloc, |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 99 | Stub& pStub) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 100 | bool exist = false; |
| 101 | Key key(pPrototype, pReloc.symInfo()->outSymbol(), pReloc.addend()); |
| 102 | StubEntryType* entry = m_StubMap.insert(key, exist); |
| 103 | if (!exist) { |
| 104 | entry->setValue(&pStub); |
| 105 | m_pRear = &pStub; |
| 106 | SectionData* sd = m_Entry.getParent(); |
| 107 | |
| 108 | // insert alignment fragment |
| 109 | // TODO: check if we can reduce this alignment fragment for some cases |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 110 | AlignFragment* align_frag = |
| 111 | new AlignFragment(pStub.alignment(), 0x0, 1u, pStub.alignment() - 1); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 112 | align_frag->setParent(sd); |
| 113 | sd->getFragmentList().insert(end(), align_frag); |
| 114 | align_frag->setOffset(align_frag->getPrevNode()->getOffset() + |
| 115 | align_frag->getPrevNode()->size()); |
| 116 | |
| 117 | // insert stub fragment |
| 118 | pStub.setParent(sd); |
| 119 | sd->getFragmentList().insert(end(), &pStub); |
| 120 | pStub.setOffset(pStub.getPrevNode()->getOffset() + |
| 121 | pStub.getPrevNode()->size()); |
| 122 | } |
| 123 | return !exist; |
| 124 | } |
| 125 | |
Stephen Hines | cfcb224 | 2016-03-08 00:18:09 -0800 | [diff] [blame^] | 126 | void BranchIsland::addStub(Stub& pStub) { |
| 127 | bool exist = false; |
| 128 | Key key(&pStub, pStub.symInfo()->outSymbol(), 0); |
| 129 | m_StubMap.insert(key, exist); |
| 130 | |
| 131 | m_pRear = &pStub; |
| 132 | SectionData* sd = m_Entry.getParent(); |
| 133 | |
| 134 | // insert alignment fragment |
| 135 | // TODO: check if we can reduce this alignment fragment for some cases |
| 136 | AlignFragment* align_frag = |
| 137 | new AlignFragment(pStub.alignment(), 0x0, 1u, pStub.alignment() - 1); |
| 138 | align_frag->setParent(sd); |
| 139 | sd->getFragmentList().insert(end(), align_frag); |
| 140 | align_frag->setOffset(align_frag->getPrevNode()->getOffset() + |
| 141 | align_frag->getPrevNode()->size()); |
| 142 | |
| 143 | // insert stub fragment |
| 144 | pStub.setParent(sd); |
| 145 | sd->getFragmentList().insert(end(), &pStub); |
| 146 | pStub.setOffset(pStub.getPrevNode()->getOffset() + |
| 147 | pStub.getPrevNode()->size()); |
| 148 | } |
| 149 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 150 | /// addRelocation - add a relocation into island |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 151 | bool BranchIsland::addRelocation(Relocation& pReloc) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 152 | m_Relocations.push_back(&pReloc); |
| 153 | return true; |
| 154 | } |
| 155 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 156 | } // namespace mcld |