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
19using namespace mcld;
20
21//===----------------------------------------------------------------------===//
22// OutputRelocSection
23//===----------------------------------------------------------------------===//
24OutputRelocSection::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
33OutputRelocSection::~OutputRelocSection()
34{
35}
36
37void OutputRelocSection::reserveEntry(size_t pNum)
38{
39  for(size_t i=0; i<pNum; i++)
40    m_pRelocData->append(*Relocation::Create());
41}
42
43Relocation* OutputRelocSection::consumeEntry()
44{
45  // first time visit this function, set m_ValidEntryIterator to
46  // Fragments.begin()
47  if(!m_isVisit) {
48    assert(!m_pRelocData->getRelocationList().empty() &&
49             "DynRelSection contains no entries.");
50    m_ValidEntryIterator = m_pRelocData->begin();
51    m_isVisit = true;
52  }
53  else {
54    // Add m_ValidEntryIterator here instead of at the end of this function.
55    // We may reserve an entry and then consume it immediately, e.g. for COPY
56    // relocation, so we need to avoid setting this iterator to
57    // RelocData->end() in any case, or when reserve and consume again,
58    // ++m_ValidEntryIterator will still be RelocData->end().
59    ++m_ValidEntryIterator;
60  }
61  assert(m_ValidEntryIterator != m_pRelocData->end() &&
62         "No empty relocation entry for the incoming symbol.");
63
64  return &(*m_ValidEntryIterator);
65}
66
67size_t OutputRelocSection::numOfRelocs()
68{
69  return m_pRelocData->size();
70}
71
72bool OutputRelocSection::addSymbolToDynSym(LDSymbol& pSymbol)
73{
74  m_Module.getSymbolTable().changeLocalToDynamic(pSymbol);
75  return true;
76}
77
78