1 //===- SectionsCmd.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/Script/SectionsCmd.h>
10 #include <mcld/Support/raw_ostream.h>
11 #include <cassert>
12 
13 using namespace mcld;
14 
15 //===----------------------------------------------------------------------===//
16 // SectionsCmd
17 //===----------------------------------------------------------------------===//
SectionsCmd()18 SectionsCmd::SectionsCmd()
19   : ScriptCommand(ScriptCommand::SECTIONS)
20 {
21 }
22 
~SectionsCmd()23 SectionsCmd::~SectionsCmd()
24 {
25   for (iterator it = begin(), ie = end(); it != ie; ++it) {
26     if (*it != NULL)
27       delete *it;
28   }
29 }
30 
dump() const31 void SectionsCmd::dump() const
32 {
33   mcld::outs() << "SECTIONS\n{\n";
34 
35   for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
36     switch ((*it)->getKind()) {
37     case ScriptCommand::ENTRY:
38     case ScriptCommand::ASSIGNMENT:
39     case ScriptCommand::OUTPUT_SECT_DESC:
40       mcld::outs() << "\t";
41       (*it)->dump();
42       break;
43     default:
44       assert(0);
45       break;
46     }
47   }
48 
49   mcld::outs() << "}\n";
50 }
51 
push_back(ScriptCommand * pCommand)52 void SectionsCmd::push_back(ScriptCommand* pCommand)
53 {
54   switch (pCommand->getKind()) {
55   case ScriptCommand::ENTRY:
56   case ScriptCommand::ASSIGNMENT:
57   case ScriptCommand::OUTPUT_SECT_DESC:
58     m_SectionCommands.push_back(pCommand);
59     break;
60   default:
61     assert(0);
62     break;
63   }
64 }
65 
activate(Module & pModule)66 void SectionsCmd::activate(Module& pModule)
67 {
68   // Assignment between output sections
69   SectionCommands assignments;
70 
71   for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
72     switch ((*it)->getKind()) {
73     case ScriptCommand::ENTRY:
74       (*it)->activate(pModule);
75       break;
76     case ScriptCommand::ASSIGNMENT:
77       assignments.push_back(*it);
78       break;
79     case ScriptCommand::OUTPUT_SECT_DESC: {
80       (*it)->activate(pModule);
81 
82       iterator assign, assignEnd = assignments.end();
83       for (assign = assignments.begin(); assign != assignEnd; ++assign)
84         (*assign)->activate(pModule);
85       assignments.clear();
86 
87       break;
88     }
89     default:
90       assert(0);
91       break;
92     }
93   }
94 }
95