1 //===- 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 //===----------------------------------------------------------------------===//
9 #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 
21 #include <llvm/Support/Casting.h>
22 
23 #include <cassert>
24 
25 namespace mcld {
26 
27 //===----------------------------------------------------------------------===//
28 // Assignment
29 //===----------------------------------------------------------------------===//
Assignment(Level pLevel,Type pType,SymOperand & pSymbol,RpnExpr & pRpnExpr)30 Assignment::Assignment(Level pLevel,
31                        Type pType,
32                        SymOperand& pSymbol,
33                        RpnExpr& pRpnExpr)
34     : ScriptCommand(ScriptCommand::ASSIGNMENT),
35       m_Level(pLevel),
36       m_Type(pType),
37       m_Symbol(pSymbol),
38       m_RpnExpr(pRpnExpr) {
39 }
40 
~Assignment()41 Assignment::~Assignment() {
42 }
43 
operator =(const Assignment & pAssignment)44 Assignment& Assignment::operator=(const Assignment& pAssignment) {
45   return *this;
46 }
47 
dump() const48 void Assignment::dump() const {
49   switch (type()) {
50     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;
63   }
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 
activate(Module & pModule)77 void Assignment::activate(Module& pModule) {
78   bool isLhsDot = m_Symbol.isDot();
79   LinkerScript& script = pModule.getScript();
80   switch (m_Level) {
81     case OUTSIDE_SECTIONS:
82       assert(!isLhsDot);
83       script.assignments().push_back(std::make_pair(nullptr, *this));
84       break;
85 
86     case OUTPUT_SECTION: {
87       bool hasDotInRhs = m_RpnExpr.hasDot();
88       SectionMap::reference out = script.sectionMap().back();
89       if (hasDotInRhs) {
90         if (!isLhsDot && out->dotAssignments().empty()) {
91           // . = ADDR ( `prev_output_sect' ) + SIZEOF ( `prev_output_sect' )
92           SectionMap::iterator prev =
93               script.sectionMap().begin() + script.sectionMap().size() - 2;
94           Assignment assign(OUTPUT_SECTION,
95                             HIDDEN,
96                             *SymOperand::create("."),
97                             *RpnExpr::buildHelperExpr(prev));
98           out->dotAssignments().push_back(assign);
99         }
100 
101         if (!out->dotAssignments().empty()) {
102           Assignment& prevDotAssign = out->dotAssignments().back();
103           // If this is the 1st explicit assignment that includes both lhs dot
104           // and
105           // rhs dot, then because of possible orphan sections, we are unable to
106           // substitute the rhs dot now.
107           if (!isLhsDot || prevDotAssign.type() == DEFAULT) {
108             for (RpnExpr::iterator it = m_RpnExpr.begin(), ie = m_RpnExpr.end();
109                  it != ie;
110                  ++it) {
111               // substitute the rhs dot with the appropriate helper expr
112               if ((*it)->kind() == ExprToken::OPERAND &&
113                   llvm::cast<Operand>(*it)->isDot()) {
114                 *it = &(prevDotAssign.symbol());
115               }
116             }  // for each expression token
117           }
118         }
119       }
120 
121       if (isLhsDot) {
122         out->dotAssignments().push_back(*this);
123       } else {
124         script.assignments().push_back(std::make_pair(nullptr, *this));
125       }
126       break;
127     }
128 
129     case INPUT_SECTION: {
130       bool hasDotInRhs = m_RpnExpr.hasDot();
131       SectionMap::Output::reference in = script.sectionMap().back()->back();
132       if (hasDotInRhs) {
133         if (in->dotAssignments().empty()) {
134           // . = `frag'
135           RpnExpr* expr = RpnExpr::buildHelperExpr(
136               in->getSection()->getSectionData()->front());
137           Assignment assign(
138               INPUT_SECTION, HIDDEN, *SymOperand::create("."), *expr);
139           in->dotAssignments().push_back(std::make_pair(nullptr, assign));
140         }
141 
142         Assignment& prevDotAssign = in->dotAssignments().back().second;
143         for (RpnExpr::iterator it = m_RpnExpr.begin(), ie = m_RpnExpr.end();
144              it != ie;
145              ++it) {
146           // substitute the rhs dot with the appropriate helper expr
147           if ((*it)->kind() == ExprToken::OPERAND &&
148               llvm::cast<Operand>(*it)->isDot()) {
149             *it = &(prevDotAssign.symbol());
150           }
151         }  // end of for
152       }
153 
154       if (isLhsDot) {
155         in->dotAssignments().push_back(std::make_pair(
156             in->getSection()->getSectionData()->front().getNextNode(), *this));
157       } else {
158         script.assignments().push_back(std::make_pair(nullptr, *this));
159       }
160       break;
161     }
162   }  // end of switch
163 }
164 
assign(RpnEvaluator & pEvaluator)165 bool Assignment::assign(RpnEvaluator& pEvaluator) {
166   uint64_t result = 0;
167   bool success = pEvaluator.eval(m_RpnExpr, result);
168   if (success)
169     m_Symbol.setValue(result);
170   return success;
171 }
172 
173 }  // namespace mcld
174