Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 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 | |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 21 | #include <llvm/Support/Casting.h> |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 22 | |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 23 | #include <cassert> |
| 24 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 25 | namespace mcld { |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // Assignment |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | Assignment::Assignment(Level pLevel, |
| 31 | Type pType, |
| 32 | SymOperand& pSymbol, |
| 33 | RpnExpr& pRpnExpr) |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 34 | : ScriptCommand(ScriptCommand::ASSIGNMENT), |
| 35 | m_Level(pLevel), |
| 36 | m_Type(pType), |
| 37 | m_Symbol(pSymbol), |
| 38 | m_RpnExpr(pRpnExpr) { |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 41 | Assignment::~Assignment() { |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 44 | Assignment& Assignment::operator=(const Assignment& pAssignment) { |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 45 | return *this; |
| 46 | } |
| 47 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 48 | void Assignment::dump() const { |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 49 | switch (type()) { |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 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; |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 77 | void Assignment::activate(Module& pModule) { |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 78 | bool isLhsDot = m_Symbol.isDot(); |
| 79 | LinkerScript& script = pModule.getScript(); |
| 80 | switch (m_Level) { |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 81 | case OUTSIDE_SECTIONS: |
| 82 | assert(!isLhsDot); |
Colin Cross | 662e477 | 2022-01-13 15:01:43 -0800 | [diff] [blame] | 83 | script.assignments().push_back(std::make_pair(nullptr, *this)); |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 84 | break; |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 85 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 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 | } |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 100 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 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 | } |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 120 | |
| 121 | if (isLhsDot) { |
| 122 | out->dotAssignments().push_back(*this); |
| 123 | } else { |
Colin Cross | 662e477 | 2022-01-13 15:01:43 -0800 | [diff] [blame] | 124 | script.assignments().push_back(std::make_pair(nullptr, *this)); |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 125 | } |
| 126 | break; |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 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); |
Colin Cross | 662e477 | 2022-01-13 15:01:43 -0800 | [diff] [blame] | 139 | in->dotAssignments().push_back(std::make_pair(nullptr, assign)); |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 140 | } |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 141 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 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 |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 154 | if (isLhsDot) { |
| 155 | in->dotAssignments().push_back(std::make_pair( |
| 156 | in->getSection()->getSectionData()->front().getNextNode(), *this)); |
| 157 | } else { |
Colin Cross | 662e477 | 2022-01-13 15:01:43 -0800 | [diff] [blame] | 158 | script.assignments().push_back(std::make_pair(nullptr, *this)); |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 159 | } |
| 160 | break; |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 161 | } |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 162 | } // end of switch |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 165 | bool Assignment::assign(RpnEvaluator& pEvaluator) { |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 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 | } |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 172 | |
| 173 | } // namespace mcld |