OutputRelocSection.cpp revision f7ac0f19a1c8d0ad14bcf6456ce368b830fea886
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
37d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liaovoid OutputRelocSection::reserveEntry(size_t pNum)
385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
39d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  for(size_t i=0; i<pNum; i++)
40d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    m_pRelocData->append(*Relocation::Create());
415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei LiaoRelocation* OutputRelocSection::consumeEntry()
445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // first time visit this function, set m_ValidEntryIterator to
465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // Fragments.begin()
475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  if(!m_isVisit) {
48d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    assert(!m_pRelocData->getRelocationList().empty() &&
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao             "DynRelSection contains no entries.");
50d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    m_ValidEntryIterator = m_pRelocData->begin();
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    m_isVisit = true;
525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  else {
5422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // Add m_ValidEntryIterator here instead of at the end of this function.
5522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // We may reserve an entry and then consume it immediately, e.g. for COPY
5622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // relocation, so we need to avoid setting this iterator to
5722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // RelocData->end() in any case, or when reserve and consume again,
5822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // ++m_ValidEntryIterator will still be RelocData->end().
595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ++m_ValidEntryIterator;
605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
6122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  assert(m_ValidEntryIterator != m_pRelocData->end() &&
6222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao         "No empty relocation entry for the incoming symbol.");
6322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
64d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  return &(*m_ValidEntryIterator);
655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
67d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liaosize_t OutputRelocSection::numOfRelocs()
6822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
69d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  return m_pRelocData->size();
7022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
7122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaobool OutputRelocSection::addSymbolToDynSym(LDSymbol& pSymbol)
7322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao{
74f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  m_Module.getSymbolTable().changeToDynamic(pSymbol);
7522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
7622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
77d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
78