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