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//===----------------------------------------------------------------------===//
937b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Target/OutputRelocSection.h"
10cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao
1137b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/IRBuilder.h"
1237b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/LD/LDSection.h"
1337b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/LD/RelocationFactory.h"
1437b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/MsgHandling.h"
1537b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Module.h"
165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
17d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <llvm/Support/Casting.h>
18d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
1937b74a387bb3993387029859c2d9d051c41c724eStephen Hinesnamespace mcld {
205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
21cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao//===----------------------------------------------------------------------===//
225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// OutputRelocSection
23cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao//===----------------------------------------------------------------------===//
24d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei LiaoOutputRelocSection::OutputRelocSection(Module& pModule, LDSection& pSection)
2537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    : m_Module(pModule),
2637b74a387bb3993387029859c2d9d051c41c724eStephen Hines      m_pRelocData(NULL),
2737b74a387bb3993387029859c2d9d051c41c724eStephen Hines      m_isVisit(false),
2837b74a387bb3993387029859c2d9d051c41c724eStephen Hines      m_ValidEntryIterator() {
2937b74a387bb3993387029859c2d9d051c41c724eStephen Hines  assert(!pSection.hasRelocData() &&
3037b74a387bb3993387029859c2d9d051c41c724eStephen Hines         "Given section is not a relocation section");
3122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  m_pRelocData = IRBuilder::CreateRelocData(pSection);
325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3437b74a387bb3993387029859c2d9d051c41c724eStephen HinesOutputRelocSection::~OutputRelocSection() {
355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3737b74a387bb3993387029859c2d9d051c41c724eStephen HinesRelocation* OutputRelocSection::create() {
38f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  Relocation* reloc = Relocation::Create();
39f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  m_pRelocData->append(*reloc);
40f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines  return reloc;
41f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines}
42f33f6de54db174aa679a4b6d1e040d37e95541c0Stephen Hines
4337b74a387bb3993387029859c2d9d051c41c724eStephen Hinesvoid OutputRelocSection::reserveEntry(size_t pNum) {
4437b74a387bb3993387029859c2d9d051c41c724eStephen Hines  for (size_t i = 0; i < pNum; ++i)
45d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    m_pRelocData->append(*Relocation::Create());
465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4837b74a387bb3993387029859c2d9d051c41c724eStephen HinesRelocation* OutputRelocSection::consumeEntry() {
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // first time visit this function, set m_ValidEntryIterator to
505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // Fragments.begin()
5137b74a387bb3993387029859c2d9d051c41c724eStephen Hines  if (!m_isVisit) {
52d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    assert(!m_pRelocData->getRelocationList().empty() &&
5337b74a387bb3993387029859c2d9d051c41c724eStephen Hines           "DynRelSection contains no entries.");
54d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao    m_ValidEntryIterator = m_pRelocData->begin();
555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    m_isVisit = true;
5637b74a387bb3993387029859c2d9d051c41c724eStephen Hines  } else {
5722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // Add m_ValidEntryIterator here instead of at the end of this function.
5822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // We may reserve an entry and then consume it immediately, e.g. for COPY
5922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // relocation, so we need to avoid setting this iterator to
6022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // RelocData->end() in any case, or when reserve and consume again,
6122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    // ++m_ValidEntryIterator will still be RelocData->end().
625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ++m_ValidEntryIterator;
635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
6422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  assert(m_ValidEntryIterator != m_pRelocData->end() &&
6522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao         "No empty relocation entry for the incoming symbol.");
6622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
67d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  return &(*m_ValidEntryIterator);
685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
7037b74a387bb3993387029859c2d9d051c41c724eStephen Hinessize_t OutputRelocSection::numOfRelocs() {
71d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  return m_pRelocData->size();
7222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
7322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7437b74a387bb3993387029859c2d9d051c41c724eStephen Hinesbool OutputRelocSection::addSymbolToDynSym(LDSymbol& pSymbol) {
75f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  m_Module.getSymbolTable().changeToDynamic(pSymbol);
7622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return true;
7722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
78d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
7937b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // namespace mcld
80