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 
19 namespace mcld {
20 
21 static GCFactory<Module::AliasList, MCLD_SECTIONS_PER_INPUT>
22     gc_aliaslist_factory;
23 
24 //===----------------------------------------------------------------------===//
25 // Module
26 //===----------------------------------------------------------------------===//
Module(LinkerScript & pScript)27 Module::Module(LinkerScript& pScript) : m_Script(pScript), m_NamePool(1024) {
28 }
29 
Module(const std::string & pName,LinkerScript & pScript)30 Module::Module(const std::string& pName, LinkerScript& pScript)
31     : m_Name(pName), m_Script(pScript), m_NamePool(1024) {
32 }
33 
~Module()34 Module::~Module() {
35 }
36 
37 // Following two functions will be obsolette when we have new section merger.
getSection(const std::string & pName)38 LDSection* 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 
getSection(const std::string & pName) const47 const 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 
CreateAliasList(const ResolveInfo & pSym)56 void 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 
addAlias(const ResolveInfo & pAlias)63 void 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 
getAliasList(const ResolveInfo & pSym)69 Module::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