1//===- Module.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/Module.h"
10#include "mcld/Fragment/FragmentRef.h"
11#include "mcld/LD/EhFrame.h"
12#include "mcld/LD/LDSection.h"
13#include "mcld/LD/LDSymbol.h"
14#include "mcld/LD/NamePool.h"
15#include "mcld/LD/ResolveInfo.h"
16#include "mcld/LD/SectionData.h"
17#include "mcld/LD/StaticResolver.h"
18
19namespace mcld {
20
21static GCFactory<Module::AliasList, MCLD_SECTIONS_PER_INPUT>
22    gc_aliaslist_factory;
23
24//===----------------------------------------------------------------------===//
25// Module
26//===----------------------------------------------------------------------===//
27Module::Module(LinkerScript& pScript) : m_Script(pScript), m_NamePool(1024) {
28}
29
30Module::Module(const std::string& pName, LinkerScript& pScript)
31    : m_Name(pName), m_Script(pScript), m_NamePool(1024) {
32}
33
34Module::~Module() {
35}
36
37// Following two functions will be obsolette when we have new section merger.
38LDSection* Module::getSection(const std::string& pName) {
39  iterator sect, sectEnd = end();
40  for (sect = begin(); sect != sectEnd; ++sect) {
41    if ((*sect)->name() == pName)
42      return *sect;
43  }
44  return NULL;
45}
46
47const LDSection* Module::getSection(const std::string& pName) const {
48  const_iterator sect, sectEnd = end();
49  for (sect = begin(); sect != sectEnd; ++sect) {
50    if ((*sect)->name() == pName)
51      return *sect;
52  }
53  return NULL;
54}
55
56void Module::CreateAliasList(const ResolveInfo& pSym) {
57  AliasList* result = gc_aliaslist_factory.allocate();
58  new (result) AliasList();
59  m_AliasLists.push_back(result);
60  result->push_back(&pSym);
61}
62
63void Module::addAlias(const ResolveInfo& pAlias) {
64  assert(m_AliasLists.size() != 0);
65  uint32_t last_pos = m_AliasLists.size() - 1;
66  m_AliasLists[last_pos]->push_back(&pAlias);
67}
68
69Module::AliasList* Module::getAliasList(const ResolveInfo& pSym) {
70  std::vector<AliasList*>::iterator list_it, list_it_e = m_AliasLists.end();
71  for (list_it = m_AliasLists.begin(); list_it != list_it_e; ++list_it) {
72    AliasList& list = **list_it;
73    alias_iterator alias_it, alias_it_e = list.end();
74    for (alias_it = list.begin(); alias_it != alias_it_e; ++alias_it) {
75      if (strcmp((*alias_it)->name(), pSym.name()) == 0) {
76        return &list;
77      }
78    }
79  }
80  return NULL;
81}
82
83}  // namespace mcld
84