summaryrefslogtreecommitdiff
path: root/lib/Script/Assignment.cpp
blob: 333b366ca1a71fd0ce4d91f803b98b4605d6b0a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//===- Assignment.cpp -----------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "mcld/Script/Assignment.h"

#include "mcld/LinkerScript.h"
#include "mcld/Module.h"
#include "mcld/LD/LDSection.h"
#include "mcld/LD/SectionData.h"
#include "mcld/Script/Operand.h"
#include "mcld/Script/Operator.h"
#include "mcld/Script/RpnEvaluator.h"
#include "mcld/Script/RpnExpr.h"
#include "mcld/Support/raw_ostream.h"

#include <llvm/Support/Casting.h>

#include <cassert>

namespace mcld {

//===----------------------------------------------------------------------===//
// Assignment
//===----------------------------------------------------------------------===//
Assignment::Assignment(Level pLevel,
                       Type pType,
                       SymOperand& pSymbol,
                       RpnExpr& pRpnExpr)
    : ScriptCommand(ScriptCommand::ASSIGNMENT),
      m_Level(pLevel),
      m_Type(pType),
      m_Symbol(pSymbol),
      m_RpnExpr(pRpnExpr) {
}

Assignment::~Assignment() {
}

Assignment& Assignment::operator=(const Assignment& pAssignment) {
  return *this;
}

void Assignment::dump() const {
  switch (type()) {
    case DEFAULT:
      break;
    case HIDDEN:
      mcld::outs() << "HIDDEN ( ";
      break;
    case PROVIDE:
      mcld::outs() << "PROVIDE ( ";
      break;
    case PROVIDE_HIDDEN:
      mcld::outs() << "PROVIDE_HIDDEN ( ";
      break;
    default:
      break;
  }

  m_Symbol.dump();

  mcld::outs() << " = ";

  m_RpnExpr.dump();

  if (type() != DEFAULT)
    mcld::outs() << " )";

  mcld::outs() << ";\n";
}

void Assignment::activate(Module& pModule) {
  bool isLhsDot = m_Symbol.isDot();
  LinkerScript& script = pModule.getScript();
  switch (m_Level) {
    case OUTSIDE_SECTIONS:
      assert(!isLhsDot);
      script.assignments().push_back(
          std::make_pair(reinterpret_cast<LDSymbol*>(NULL), *this));
      break;

    case OUTPUT_SECTION: {
      bool hasDotInRhs = m_RpnExpr.hasDot();
      SectionMap::reference out = script.sectionMap().back();
      if (hasDotInRhs) {
        if (!isLhsDot && out->dotAssignments().empty()) {
          // . = ADDR ( `prev_output_sect' ) + SIZEOF ( `prev_output_sect' )
          SectionMap::iterator prev =
              script.sectionMap().begin() + script.sectionMap().size() - 2;
          Assignment assign(OUTPUT_SECTION,
                            HIDDEN,
                            *SymOperand::create("."),
                            *RpnExpr::buildHelperExpr(prev));
          out->dotAssignments().push_back(assign);
        }

        if (!out->dotAssignments().empty()) {
          Assignment& prevDotAssign = out->dotAssignments().back();
          // If this is the 1st explicit assignment that includes both lhs dot
          // and
          // rhs dot, then because of possible orphan sections, we are unable to
          // substitute the rhs dot now.
          if (!isLhsDot || prevDotAssign.type() == DEFAULT) {
            for (RpnExpr::iterator it = m_RpnExpr.begin(), ie = m_RpnExpr.end();
                 it != ie;
                 ++it) {
              // substitute the rhs dot with the appropriate helper expr
              if ((*it)->kind() == ExprToken::OPERAND &&
                  llvm::cast<Operand>(*it)->isDot()) {
                *it = &(prevDotAssign.symbol());
              }
            }  // for each expression token
          }
        }
      }

      if (isLhsDot) {
        out->dotAssignments().push_back(*this);
      } else {
        script.assignments().push_back(
            std::make_pair(reinterpret_cast<LDSymbol*>(NULL), *this));
      }
      break;
    }

    case INPUT_SECTION: {
      bool hasDotInRhs = m_RpnExpr.hasDot();
      SectionMap::Output::reference in = script.sectionMap().back()->back();
      if (hasDotInRhs) {
        if (in->dotAssignments().empty()) {
          // . = `frag'
          RpnExpr* expr = RpnExpr::buildHelperExpr(
              in->getSection()->getSectionData()->front());
          Assignment assign(
              INPUT_SECTION, HIDDEN, *SymOperand::create("."), *expr);
          in->dotAssignments().push_back(
              std::make_pair(reinterpret_cast<Fragment*>(NULL), assign));
        }

        Assignment& prevDotAssign = in->dotAssignments().back().second;
        for (RpnExpr::iterator it = m_RpnExpr.begin(), ie = m_RpnExpr.end();
             it != ie;
             ++it) {
          // substitute the rhs dot with the appropriate helper expr
          if ((*it)->kind() == ExprToken::OPERAND &&
              llvm::cast<Operand>(*it)->isDot()) {
            *it = &(prevDotAssign.symbol());
          }
        }  // end of for
      }

      if (isLhsDot) {
        in->dotAssignments().push_back(std::make_pair(
            in->getSection()->getSectionData()->front().getNextNode(), *this));
      } else {
        script.assignments().push_back(
            std::make_pair(reinterpret_cast<LDSymbol*>(NULL), *this));
      }
      break;
    }
  }  // end of switch
}

bool Assignment::assign(RpnEvaluator& pEvaluator) {
  uint64_t result = 0;
  bool success = pEvaluator.eval(m_RpnExpr, result);
  if (success)
    m_Symbol.setValue(result);
  return success;
}

}  // namespace mcld