15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- OutputRelocSection.cpp ---------------------------------------------===//
25460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
35460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//                     The MCLinker Project
45460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
55460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// This file is distributed under the University of Illinois Open Source
65460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// License. See LICENSE.TXT for details.
75460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
85460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
9d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <mcld/Target/OutputRelocSection.h>
10cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao
11cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao#include <mcld/LD/LDSection.h>
1222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/LD/RelocationFactory.h>
1322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Module.h>
14affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/MsgHandling.h>
1522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/IRBuilder.h>
165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
17d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <llvm/Support/Casting.h>
18d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaousing namespace mcld;
205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
21cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao//===----------------------------------------------------------------------===//
225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// OutputRelocSection
23cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao//===----------------------------------------------------------------------===//
24d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei LiaoOutputRelocSection::OutputRelocSection(Module& pModule, LDSection& pSection)
2522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  : m_Module(pModule),
2622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    m_pRelocData(NULL),
275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    m_isVisit(false),
285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    m_ValidEntryIterator(){
2922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  assert(!pSection.hasRelocData() && "Given section is not a relocation section");
3022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  m_pRelocData = IRBuilder::CreateRelocData(pSection);
315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoOutputRelocSection::~OutputRelocSection()
345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
37f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen HinesRelocation* OutputRelocSection::create()
38f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines{
39f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  Relocation* reloc = Relocation::Create();
40f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  m_pRelocData->append(*reloc);
41f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  return reloc;
42f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines}
43f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines
44d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liaovoid OutputRelocSection::reserveEntry(size_t pNum)
455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
46d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  for(size_t i=0; i<pNum; i++)
47d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    m_pRelocData->append(*Relocation::Create());
485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
5022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei LiaoRelocation* OutputRelocSection::consumeEntry()
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // first time visit this function, set m_ValidEntryIterator to
535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // Fragments.begin()
545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  if(!m_isVisit) {
55d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    assert(!m_pRelocData->getRelocationList().empty() &&
565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao             "DynRelSection contains no entries.");
57d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    m_ValidEntryIterator = m_pRelocData->begin();
585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    m_isVisit = true;
595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  else {
6122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // Add m_ValidEntryIterator here instead of at the end of this function.
6222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // We may reserve an entry and then consume it immediately, e.g. for COPY
6322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // relocation, so we need to avoid setting this iterator to
6422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // RelocData->end() in any case, or when reserve and consume again,
6522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // ++m_ValidEntryIterator will still be RelocData->end().
665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ++m_ValidEntryIterator;
675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
6822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  assert(m_ValidEntryIterator != m_pRelocData->end() &&
6922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao         "No empty relocation entry for the incoming symbol.");
7022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
71d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  return &(*m_ValidEntryIterator);
725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
74d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liaosize_t OutputRelocSection::numOfRelocs()
7522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
76d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  return m_pRelocData->size();
7722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
7822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool OutputRelocSection::addSymbolToDynSym(LDSymbol& pSymbol)
8022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
81f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  m_Module.getSymbolTable().changeToDynamic(pSymbol);
8222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
8322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
84d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
85