1 //===- OutputRelocSection.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 <mcld/Target/OutputRelocSection.h>
10
11 #include <mcld/LD/LDSection.h>
12 #include <mcld/LD/RelocationFactory.h>
13 #include <mcld/Module.h>
14 #include <mcld/Support/MsgHandling.h>
15 #include <mcld/IRBuilder.h>
16
17 #include <llvm/Support/Casting.h>
18
19 using namespace mcld;
20
21 //===----------------------------------------------------------------------===//
22 // OutputRelocSection
23 //===----------------------------------------------------------------------===//
OutputRelocSection(Module & pModule,LDSection & pSection)24 OutputRelocSection::OutputRelocSection(Module& pModule, LDSection& pSection)
25 : m_Module(pModule),
26 m_pRelocData(NULL),
27 m_isVisit(false),
28 m_ValidEntryIterator(){
29 assert(!pSection.hasRelocData() && "Given section is not a relocation section");
30 m_pRelocData = IRBuilder::CreateRelocData(pSection);
31 }
32
~OutputRelocSection()33 OutputRelocSection::~OutputRelocSection()
34 {
35 }
36
create()37 Relocation* OutputRelocSection::create()
38 {
39 Relocation* reloc = Relocation::Create();
40 m_pRelocData->append(*reloc);
41 return reloc;
42 }
43
reserveEntry(size_t pNum)44 void OutputRelocSection::reserveEntry(size_t pNum)
45 {
46 for(size_t i=0; i<pNum; i++)
47 m_pRelocData->append(*Relocation::Create());
48 }
49
consumeEntry()50 Relocation* OutputRelocSection::consumeEntry()
51 {
52 // first time visit this function, set m_ValidEntryIterator to
53 // Fragments.begin()
54 if(!m_isVisit) {
55 assert(!m_pRelocData->getRelocationList().empty() &&
56 "DynRelSection contains no entries.");
57 m_ValidEntryIterator = m_pRelocData->begin();
58 m_isVisit = true;
59 }
60 else {
61 // Add m_ValidEntryIterator here instead of at the end of this function.
62 // We may reserve an entry and then consume it immediately, e.g. for COPY
63 // relocation, so we need to avoid setting this iterator to
64 // RelocData->end() in any case, or when reserve and consume again,
65 // ++m_ValidEntryIterator will still be RelocData->end().
66 ++m_ValidEntryIterator;
67 }
68 assert(m_ValidEntryIterator != m_pRelocData->end() &&
69 "No empty relocation entry for the incoming symbol.");
70
71 return &(*m_ValidEntryIterator);
72 }
73
numOfRelocs()74 size_t OutputRelocSection::numOfRelocs()
75 {
76 return m_pRelocData->size();
77 }
78
addSymbolToDynSym(LDSymbol & pSymbol)79 bool OutputRelocSection::addSymbolToDynSym(LDSymbol& pSymbol)
80 {
81 m_Module.getSymbolTable().changeToDynamic(pSymbol);
82 return true;
83 }
84
85