blob: 333b366ca1a71fd0ce4d91f803b98b4605d6b0a0 [file] [log] [blame]
Stephen Hinesf33f6de2014-02-14 18:00:16 -08001//===- Assignment.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//===----------------------------------------------------------------------===//
Stephen Hines37b74a32014-11-26 18:48:20 -08009#include "mcld/Script/Assignment.h"
10
11#include "mcld/LinkerScript.h"
12#include "mcld/Module.h"
13#include "mcld/LD/LDSection.h"
14#include "mcld/LD/SectionData.h"
15#include "mcld/Script/Operand.h"
16#include "mcld/Script/Operator.h"
17#include "mcld/Script/RpnEvaluator.h"
18#include "mcld/Script/RpnExpr.h"
19#include "mcld/Support/raw_ostream.h"
20
Stephen Hinesf33f6de2014-02-14 18:00:16 -080021#include <llvm/Support/Casting.h>
Stephen Hines37b74a32014-11-26 18:48:20 -080022
Stephen Hinesf33f6de2014-02-14 18:00:16 -080023#include <cassert>
24
Stephen Hines37b74a32014-11-26 18:48:20 -080025namespace mcld {
Stephen Hinesf33f6de2014-02-14 18:00:16 -080026
27//===----------------------------------------------------------------------===//
28// Assignment
29//===----------------------------------------------------------------------===//
30Assignment::Assignment(Level pLevel,
31 Type pType,
32 SymOperand& pSymbol,
33 RpnExpr& pRpnExpr)
Stephen Hines37b74a32014-11-26 18:48:20 -080034 : ScriptCommand(ScriptCommand::ASSIGNMENT),
35 m_Level(pLevel),
36 m_Type(pType),
37 m_Symbol(pSymbol),
38 m_RpnExpr(pRpnExpr) {
Stephen Hinesf33f6de2014-02-14 18:00:16 -080039}
40
Stephen Hines37b74a32014-11-26 18:48:20 -080041Assignment::~Assignment() {
Stephen Hinesf33f6de2014-02-14 18:00:16 -080042}
43
Stephen Hines37b74a32014-11-26 18:48:20 -080044Assignment& Assignment::operator=(const Assignment& pAssignment) {
Stephen Hinesf33f6de2014-02-14 18:00:16 -080045 return *this;
46}
47
Stephen Hines37b74a32014-11-26 18:48:20 -080048void Assignment::dump() const {
Stephen Hinesf33f6de2014-02-14 18:00:16 -080049 switch (type()) {
Stephen Hines37b74a32014-11-26 18:48:20 -080050 case DEFAULT:
51 break;
52 case HIDDEN:
53 mcld::outs() << "HIDDEN ( ";
54 break;
55 case PROVIDE:
56 mcld::outs() << "PROVIDE ( ";
57 break;
58 case PROVIDE_HIDDEN:
59 mcld::outs() << "PROVIDE_HIDDEN ( ";
60 break;
61 default:
62 break;
Stephen Hinesf33f6de2014-02-14 18:00:16 -080063 }
64
65 m_Symbol.dump();
66
67 mcld::outs() << " = ";
68
69 m_RpnExpr.dump();
70
71 if (type() != DEFAULT)
72 mcld::outs() << " )";
73
74 mcld::outs() << ";\n";
75}
76
Stephen Hines37b74a32014-11-26 18:48:20 -080077void Assignment::activate(Module& pModule) {
Stephen Hinesf33f6de2014-02-14 18:00:16 -080078 bool isLhsDot = m_Symbol.isDot();
79 LinkerScript& script = pModule.getScript();
80 switch (m_Level) {
Stephen Hines37b74a32014-11-26 18:48:20 -080081 case OUTSIDE_SECTIONS:
82 assert(!isLhsDot);
83 script.assignments().push_back(
84 std::make_pair(reinterpret_cast<LDSymbol*>(NULL), *this));
85 break;
Stephen Hinesf33f6de2014-02-14 18:00:16 -080086
Stephen Hines37b74a32014-11-26 18:48:20 -080087 case OUTPUT_SECTION: {
88 bool hasDotInRhs = m_RpnExpr.hasDot();
89 SectionMap::reference out = script.sectionMap().back();
90 if (hasDotInRhs) {
91 if (!isLhsDot && out->dotAssignments().empty()) {
92 // . = ADDR ( `prev_output_sect' ) + SIZEOF ( `prev_output_sect' )
93 SectionMap::iterator prev =
94 script.sectionMap().begin() + script.sectionMap().size() - 2;
95 Assignment assign(OUTPUT_SECTION,
96 HIDDEN,
97 *SymOperand::create("."),
98 *RpnExpr::buildHelperExpr(prev));
99 out->dotAssignments().push_back(assign);
100 }
Stephen Hinesf33f6de2014-02-14 18:00:16 -0800101
Stephen Hines37b74a32014-11-26 18:48:20 -0800102 if (!out->dotAssignments().empty()) {
103 Assignment& prevDotAssign = out->dotAssignments().back();
104 // If this is the 1st explicit assignment that includes both lhs dot
105 // and
106 // rhs dot, then because of possible orphan sections, we are unable to
107 // substitute the rhs dot now.
108 if (!isLhsDot || prevDotAssign.type() == DEFAULT) {
109 for (RpnExpr::iterator it = m_RpnExpr.begin(), ie = m_RpnExpr.end();
110 it != ie;
111 ++it) {
112 // substitute the rhs dot with the appropriate helper expr
113 if ((*it)->kind() == ExprToken::OPERAND &&
114 llvm::cast<Operand>(*it)->isDot()) {
115 *it = &(prevDotAssign.symbol());
116 }
117 } // for each expression token
118 }
Stephen Hinesf33f6de2014-02-14 18:00:16 -0800119 }
120 }
Stephen Hines37b74a32014-11-26 18:48:20 -0800121
122 if (isLhsDot) {
123 out->dotAssignments().push_back(*this);
124 } else {
125 script.assignments().push_back(
126 std::make_pair(reinterpret_cast<LDSymbol*>(NULL), *this));
127 }
128 break;
Stephen Hinesf33f6de2014-02-14 18:00:16 -0800129 }
130
Stephen Hines37b74a32014-11-26 18:48:20 -0800131 case INPUT_SECTION: {
132 bool hasDotInRhs = m_RpnExpr.hasDot();
133 SectionMap::Output::reference in = script.sectionMap().back()->back();
134 if (hasDotInRhs) {
135 if (in->dotAssignments().empty()) {
136 // . = `frag'
137 RpnExpr* expr = RpnExpr::buildHelperExpr(
138 in->getSection()->getSectionData()->front());
139 Assignment assign(
140 INPUT_SECTION, HIDDEN, *SymOperand::create("."), *expr);
141 in->dotAssignments().push_back(
142 std::make_pair(reinterpret_cast<Fragment*>(NULL), assign));
143 }
Stephen Hinesf33f6de2014-02-14 18:00:16 -0800144
Stephen Hines37b74a32014-11-26 18:48:20 -0800145 Assignment& prevDotAssign = in->dotAssignments().back().second;
146 for (RpnExpr::iterator it = m_RpnExpr.begin(), ie = m_RpnExpr.end();
147 it != ie;
148 ++it) {
149 // substitute the rhs dot with the appropriate helper expr
150 if ((*it)->kind() == ExprToken::OPERAND &&
151 llvm::cast<Operand>(*it)->isDot()) {
152 *it = &(prevDotAssign.symbol());
153 }
154 } // end of for
Stephen Hinesf33f6de2014-02-14 18:00:16 -0800155 }
156
Stephen Hines37b74a32014-11-26 18:48:20 -0800157 if (isLhsDot) {
158 in->dotAssignments().push_back(std::make_pair(
159 in->getSection()->getSectionData()->front().getNextNode(), *this));
160 } else {
161 script.assignments().push_back(
162 std::make_pair(reinterpret_cast<LDSymbol*>(NULL), *this));
163 }
164 break;
Stephen Hinesf33f6de2014-02-14 18:00:16 -0800165 }
Stephen Hines37b74a32014-11-26 18:48:20 -0800166 } // end of switch
Stephen Hinesf33f6de2014-02-14 18:00:16 -0800167}
168
Stephen Hines37b74a32014-11-26 18:48:20 -0800169bool Assignment::assign(RpnEvaluator& pEvaluator) {
Stephen Hinesf33f6de2014-02-14 18:00:16 -0800170 uint64_t result = 0;
171 bool success = pEvaluator.eval(m_RpnExpr, result);
172 if (success)
173 m_Symbol.setValue(result);
174 return success;
175}
Stephen Hines37b74a32014-11-26 18:48:20 -0800176
177} // namespace mcld