Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/MC/MCAssembler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Statistic.h" |
| 12 | #include "llvm/ADT/StringExtras.h" |
| 13 | #include "llvm/ADT/Twine.h" |
| 14 | #include "llvm/MC/MCAsmBackend.h" |
Daniel Dunbar | cf55f96 | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAsmLayout.h" |
Daniel Dunbar | eaa367f | 2010-03-19 10:43:23 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCCodeEmitter.h" |
Rafael Espindola | 0f8abeb | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCContext.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCDwarf.h" |
Daniel Dunbar | 8e6d889 | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCExpr.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCFixupKindInfo.h" |
Daniel Dunbar | f027abf | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCObjectWriter.h" |
Kevin Enderby | e46564a | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCSection.h" |
Daniel Dunbar | 8e6d889 | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSymbol.h" |
| 24 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | d821f4a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | bf387df | 2012-08-08 23:56:06 +0000 | [diff] [blame] | 27 | #include "llvm/Support/LEB128.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/Support/TargetRegistry.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | b245927 | 2014-04-29 23:46:48 +0000 | [diff] [blame] | 30 | #include <tuple> |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Chandler Carruth | f58e376 | 2014-04-22 03:04:17 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "assembler" |
| 34 | |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | namespace stats { |
Eli Bendersky | 2ccd044 | 2012-12-07 17:59:21 +0000 | [diff] [blame] | 37 | STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total"); |
Eli Bendersky | 0652dfd | 2013-01-08 17:41:59 +0000 | [diff] [blame] | 38 | STATISTIC(EmittedRelaxableFragments, |
| 39 | "Number of emitted assembler fragments - relaxable"); |
Eli Bendersky | c01322e | 2012-12-10 18:59:39 +0000 | [diff] [blame] | 40 | STATISTIC(EmittedDataFragments, |
| 41 | "Number of emitted assembler fragments - data"); |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 42 | STATISTIC(EmittedCompactEncodedInstFragments, |
| 43 | "Number of emitted assembler fragments - compact encoded inst"); |
Eli Bendersky | c01322e | 2012-12-10 18:59:39 +0000 | [diff] [blame] | 44 | STATISTIC(EmittedAlignFragments, |
| 45 | "Number of emitted assembler fragments - align"); |
| 46 | STATISTIC(EmittedFillFragments, |
| 47 | "Number of emitted assembler fragments - fill"); |
| 48 | STATISTIC(EmittedOrgFragments, |
| 49 | "Number of emitted assembler fragments - org"); |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 50 | STATISTIC(evaluateFixup, "Number of evaluated fixups"); |
Daniel Dunbar | d821f4a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 51 | STATISTIC(FragmentLayouts, "Number of fragment layouts"); |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 52 | STATISTIC(ObjectBytes, "Number of emitted object file bytes"); |
Daniel Dunbar | d821f4a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 53 | STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps"); |
| 54 | STATISTIC(RelaxedInstructions, "Number of relaxed instructions"); |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
Daniel Dunbar | 5a07d6a | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 57 | |
Daniel Dunbar | 2701eee | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 58 | // FIXME FIXME FIXME: There are number of places in this file where we convert |
| 59 | // what is a 64-bit assembler value used for computation into a value in the |
| 60 | // object file, which may truncate it. We should detect that truncation where |
| 61 | // invalid and report errors back. |
| 62 | |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 63 | /* *** */ |
| 64 | |
Daniel Dunbar | 9abade1 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 65 | MCAsmLayout::MCAsmLayout(MCAssembler &Asm) |
Rafael Espindola | 6624f47 | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 66 | : Assembler(Asm), LastValidFragment() |
Daniel Dunbar | 9abade1 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 67 | { |
Daniel Dunbar | e02c1f6 | 2010-05-12 15:42:59 +0000 | [diff] [blame] | 68 | // Compute the section layout order. Virtual sections must go last. |
| 69 | for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) |
Rafael Espindola | 7a2cd8b | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 70 | if (!it->getSection().isVirtualSection()) |
Daniel Dunbar | e02c1f6 | 2010-05-12 15:42:59 +0000 | [diff] [blame] | 71 | SectionOrder.push_back(&*it); |
| 72 | for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) |
Rafael Espindola | 7a2cd8b | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 73 | if (it->getSection().isVirtualSection()) |
Daniel Dunbar | e02c1f6 | 2010-05-12 15:42:59 +0000 | [diff] [blame] | 74 | SectionOrder.push_back(&*it); |
| 75 | } |
| 76 | |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 77 | bool MCAsmLayout::isFragmentValid(const MCFragment *F) const { |
Rafael Espindola | 6624f47 | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 78 | const MCSectionData &SD = *F->getParent(); |
| 79 | const MCFragment *LastValid = LastValidFragment.lookup(&SD); |
| 80 | if (!LastValid) |
| 81 | return false; |
| 82 | assert(LastValid->getParent() == F->getParent()); |
| 83 | return F->getLayoutOrder() <= LastValid->getLayoutOrder(); |
Daniel Dunbar | 9abade1 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Derek Schuff | 90aa1d8 | 2013-02-05 17:55:27 +0000 | [diff] [blame] | 86 | void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) { |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 87 | // If this fragment wasn't already valid, we don't need to do anything. |
| 88 | if (!isFragmentValid(F)) |
Daniel Dunbar | 4bce748 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 89 | return; |
Daniel Dunbar | 6432bd7 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 90 | |
Derek Schuff | 90aa1d8 | 2013-02-05 17:55:27 +0000 | [diff] [blame] | 91 | // Otherwise, reset the last valid fragment to the previous fragment |
| 92 | // (if this is the first fragment, it will be NULL). |
Rafael Espindola | 6624f47 | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 93 | const MCSectionData &SD = *F->getParent(); |
Derek Schuff | 90aa1d8 | 2013-02-05 17:55:27 +0000 | [diff] [blame] | 94 | LastValidFragment[&SD] = F->getPrevNode(); |
Daniel Dunbar | 4bce748 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 95 | } |
Daniel Dunbar | 6432bd7 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 96 | |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 97 | void MCAsmLayout::ensureValid(const MCFragment *F) const { |
Rafael Espindola | 6624f47 | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 98 | MCSectionData &SD = *F->getParent(); |
| 99 | |
| 100 | MCFragment *Cur = LastValidFragment[&SD]; |
| 101 | if (!Cur) |
| 102 | Cur = &*SD.begin(); |
| 103 | else |
| 104 | Cur = Cur->getNextNode(); |
| 105 | |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 106 | // Advance the layout position until the fragment is valid. |
| 107 | while (!isFragmentValid(F)) { |
| 108 | assert(Cur && "Layout bookkeeping error"); |
| 109 | const_cast<MCAsmLayout*>(this)->layoutFragment(Cur); |
Rafael Espindola | 6624f47 | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 110 | Cur = Cur->getNextNode(); |
Daniel Dunbar | 4bce748 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 111 | } |
Daniel Dunbar | 6432bd7 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Daniel Dunbar | 2522dd1 | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 114 | uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const { |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 115 | ensureValid(F); |
Daniel Dunbar | 2522dd1 | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 116 | assert(F->Offset != ~UINT64_C(0) && "Address not set!"); |
| 117 | return F->Offset; |
| 118 | } |
| 119 | |
Rafael Espindola | bc03586 | 2014-04-30 03:06:06 +0000 | [diff] [blame] | 120 | // Simple getSymbolOffset helper for the non-varibale case. |
Rafael Espindola | fee224f | 2014-04-30 21:51:13 +0000 | [diff] [blame] | 121 | static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbolData &SD, |
| 122 | bool ReportError, uint64_t &Val) { |
| 123 | if (!SD.getFragment()) { |
| 124 | if (ReportError) |
| 125 | report_fatal_error("unable to evaluate offset to undefined symbol '" + |
| 126 | SD.getSymbol().getName() + "'"); |
| 127 | return false; |
| 128 | } |
| 129 | Val = Layout.getFragmentOffset(SD.getFragment()) + SD.getOffset(); |
| 130 | return true; |
Rafael Espindola | bc03586 | 2014-04-30 03:06:06 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Rafael Espindola | fee224f | 2014-04-30 21:51:13 +0000 | [diff] [blame] | 133 | static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, |
| 134 | const MCSymbolData *SD, bool ReportError, |
| 135 | uint64_t &Val) { |
Daniel Dunbar | 7203286 | 2011-04-29 18:20:20 +0000 | [diff] [blame] | 136 | const MCSymbol &S = SD->getSymbol(); |
| 137 | |
Rafael Espindola | bc03586 | 2014-04-30 03:06:06 +0000 | [diff] [blame] | 138 | if (!S.isVariable()) |
Rafael Espindola | fee224f | 2014-04-30 21:51:13 +0000 | [diff] [blame] | 139 | return getLabelOffset(Layout, *SD, ReportError, Val); |
Daniel Dunbar | 7203286 | 2011-04-29 18:20:20 +0000 | [diff] [blame] | 140 | |
Rafael Espindola | bc03586 | 2014-04-30 03:06:06 +0000 | [diff] [blame] | 141 | // If SD is a variable, evaluate it. |
| 142 | MCValue Target; |
Rafael Espindola | fee224f | 2014-04-30 21:51:13 +0000 | [diff] [blame] | 143 | if (!S.getVariableValue()->EvaluateAsValue(Target, &Layout)) |
Rafael Espindola | bc03586 | 2014-04-30 03:06:06 +0000 | [diff] [blame] | 144 | report_fatal_error("unable to evaluate offset for variable '" + |
| 145 | S.getName() + "'"); |
Jim Grosbach | 6485a79 | 2011-10-26 22:44:41 +0000 | [diff] [blame] | 146 | |
Rafael Espindola | bc03586 | 2014-04-30 03:06:06 +0000 | [diff] [blame] | 147 | uint64_t Offset = Target.getConstant(); |
Daniel Dunbar | 7203286 | 2011-04-29 18:20:20 +0000 | [diff] [blame] | 148 | |
Rafael Espindola | fee224f | 2014-04-30 21:51:13 +0000 | [diff] [blame] | 149 | const MCAssembler &Asm = Layout.getAssembler(); |
| 150 | |
Rafael Espindola | bc03586 | 2014-04-30 03:06:06 +0000 | [diff] [blame] | 151 | const MCSymbolRefExpr *A = Target.getSymA(); |
Rafael Espindola | fee224f | 2014-04-30 21:51:13 +0000 | [diff] [blame] | 152 | if (A) { |
| 153 | uint64_t ValA; |
| 154 | if (!getLabelOffset(Layout, Asm.getSymbolData(A->getSymbol()), ReportError, |
| 155 | ValA)) |
| 156 | return false; |
| 157 | Offset += ValA; |
| 158 | } |
Rafael Espindola | bc03586 | 2014-04-30 03:06:06 +0000 | [diff] [blame] | 159 | |
| 160 | const MCSymbolRefExpr *B = Target.getSymB(); |
Rafael Espindola | fee224f | 2014-04-30 21:51:13 +0000 | [diff] [blame] | 161 | if (B) { |
| 162 | uint64_t ValB; |
| 163 | if (!getLabelOffset(Layout, Asm.getSymbolData(B->getSymbol()), ReportError, |
| 164 | ValB)) |
| 165 | return false; |
| 166 | Offset -= ValB; |
| 167 | } |
Rafael Espindola | bc03586 | 2014-04-30 03:06:06 +0000 | [diff] [blame] | 168 | |
Rafael Espindola | fee224f | 2014-04-30 21:51:13 +0000 | [diff] [blame] | 169 | Val = Offset; |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | bool MCAsmLayout::getSymbolOffset(const MCSymbolData *SD, uint64_t &Val) const { |
| 174 | return getSymbolOffsetImpl(*this, SD, false, Val); |
| 175 | } |
| 176 | |
| 177 | uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const { |
| 178 | uint64_t Val; |
| 179 | getSymbolOffsetImpl(*this, SD, true, Val); |
| 180 | return Val; |
Rafael Espindola | e7284c3 | 2010-12-06 02:57:26 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Rafael Espindola | 2aeac7a | 2014-05-01 13:24:25 +0000 | [diff] [blame^] | 183 | const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const { |
| 184 | if (!Symbol.isVariable()) |
| 185 | return &Symbol; |
| 186 | |
| 187 | const MCExpr *Expr = Symbol.getVariableValue(); |
| 188 | MCValue Value; |
| 189 | if (!Expr->EvaluateAsValue(Value, this)) |
| 190 | llvm_unreachable("Invalid Expression"); |
| 191 | |
| 192 | const MCSymbolRefExpr *RefB = Value.getSymB(); |
| 193 | if (RefB) |
| 194 | Assembler.getContext().FatalError( |
| 195 | SMLoc(), Twine("symbol '") + RefB->getSymbol().getName() + |
| 196 | "' could not be evaluated in a subtraction expression"); |
| 197 | |
| 198 | const MCSymbolRefExpr *A = Value.getSymA(); |
| 199 | if (!A) |
| 200 | return nullptr; |
| 201 | |
| 202 | return &A->getSymbol(); |
| 203 | } |
| 204 | |
Daniel Dunbar | d83889b | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 205 | uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const { |
Daniel Dunbar | a99a23b | 2010-05-14 00:37:11 +0000 | [diff] [blame] | 206 | // The size is the last fragment's end offset. |
Daniel Dunbar | d83889b | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 207 | const MCFragment &F = SD->getFragmentList().back(); |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 208 | return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F); |
Daniel Dunbar | 5b07334 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const { |
Daniel Dunbar | d83889b | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 212 | // Virtual sections have no file size. |
Rafael Espindola | 7a2cd8b | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 213 | if (SD->getSection().isVirtualSection()) |
Daniel Dunbar | d83889b | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 214 | return 0; |
| 215 | |
| 216 | // Otherwise, the file size is the same as the address space size. |
| 217 | return getSectionAddressSize(SD); |
Daniel Dunbar | 5b07334 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 220 | uint64_t MCAsmLayout::computeBundlePadding(const MCFragment *F, |
| 221 | uint64_t FOffset, uint64_t FSize) { |
| 222 | uint64_t BundleSize = Assembler.getBundleAlignSize(); |
Derek Schuff | 90aa1d8 | 2013-02-05 17:55:27 +0000 | [diff] [blame] | 223 | assert(BundleSize > 0 && |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 224 | "computeBundlePadding should only be called if bundling is enabled"); |
| 225 | uint64_t BundleMask = BundleSize - 1; |
| 226 | uint64_t OffsetInBundle = FOffset & BundleMask; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 227 | uint64_t EndOfFragment = OffsetInBundle + FSize; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 228 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 229 | // There are two kinds of bundling restrictions: |
Derek Schuff | 90aa1d8 | 2013-02-05 17:55:27 +0000 | [diff] [blame] | 230 | // |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 231 | // 1) For alignToBundleEnd(), add padding to ensure that the fragment will |
| 232 | // *end* on a bundle boundary. |
| 233 | // 2) Otherwise, check if the fragment would cross a bundle boundary. If it |
| 234 | // would, add padding until the end of the bundle so that the fragment |
| 235 | // will start in a new one. |
| 236 | if (F->alignToBundleEnd()) { |
| 237 | // Three possibilities here: |
| 238 | // |
| 239 | // A) The fragment just happens to end at a bundle boundary, so we're good. |
| 240 | // B) The fragment ends before the current bundle boundary: pad it just |
| 241 | // enough to reach the boundary. |
| 242 | // C) The fragment ends after the current bundle boundary: pad it until it |
| 243 | // reaches the end of the next bundle boundary. |
| 244 | // |
| 245 | // Note: this code could be made shorter with some modulo trickery, but it's |
| 246 | // intentionally kept in its more explicit form for simplicity. |
| 247 | if (EndOfFragment == BundleSize) |
| 248 | return 0; |
| 249 | else if (EndOfFragment < BundleSize) |
| 250 | return BundleSize - EndOfFragment; |
| 251 | else { // EndOfFragment > BundleSize |
| 252 | return 2 * BundleSize - EndOfFragment; |
| 253 | } |
| 254 | } else if (EndOfFragment > BundleSize) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 255 | return BundleSize - OffsetInBundle; |
| 256 | else |
| 257 | return 0; |
| 258 | } |
| 259 | |
Daniel Dunbar | 7c96955 | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 260 | /* *** */ |
| 261 | |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 262 | MCFragment::MCFragment() : Kind(FragmentType(~0)) { |
| 263 | } |
| 264 | |
Daniel Dunbar | e397dde | 2010-07-28 20:28:45 +0000 | [diff] [blame] | 265 | MCFragment::~MCFragment() { |
| 266 | } |
| 267 | |
Daniel Dunbar | aef5317 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 268 | MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent) |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 269 | : Kind(_Kind), Parent(_Parent), Atom(nullptr), Offset(~UINT64_C(0)) |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 270 | { |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 271 | if (Parent) |
| 272 | Parent->getFragmentList().push_back(this); |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* *** */ |
| 276 | |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 277 | MCEncodedFragment::~MCEncodedFragment() { |
| 278 | } |
| 279 | |
| 280 | /* *** */ |
| 281 | |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 282 | MCEncodedFragmentWithFixups::~MCEncodedFragmentWithFixups() { |
| 283 | } |
| 284 | |
| 285 | /* *** */ |
| 286 | |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 287 | MCSectionData::MCSectionData() : Section(nullptr) {} |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 288 | |
| 289 | MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A) |
Daniel Dunbar | 47f11ac | 2009-08-27 00:38:04 +0000 | [diff] [blame] | 290 | : Section(&_Section), |
Rafael Espindola | baf2f3b | 2010-12-06 03:48:09 +0000 | [diff] [blame] | 291 | Ordinal(~UINT32_C(0)), |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 292 | Alignment(1), |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 293 | BundleLockState(NotBundleLocked), BundleGroupBeforeFirstInst(false), |
Daniel Dunbar | 255a8c8 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 294 | HasInstructions(false) |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 295 | { |
| 296 | if (A) |
| 297 | A->getSectionList().push_back(this); |
| 298 | } |
| 299 | |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 300 | MCSectionData::iterator |
| 301 | MCSectionData::getSubsectionInsertionPoint(unsigned Subsection) { |
| 302 | if (Subsection == 0 && SubsectionFragmentMap.empty()) |
| 303 | return end(); |
| 304 | |
| 305 | SmallVectorImpl<std::pair<unsigned, MCFragment *> >::iterator MI = |
| 306 | std::lower_bound(SubsectionFragmentMap.begin(), SubsectionFragmentMap.end(), |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 307 | std::make_pair(Subsection, (MCFragment *)nullptr)); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 308 | bool ExactMatch = false; |
| 309 | if (MI != SubsectionFragmentMap.end()) { |
| 310 | ExactMatch = MI->first == Subsection; |
| 311 | if (ExactMatch) |
| 312 | ++MI; |
| 313 | } |
| 314 | iterator IP; |
| 315 | if (MI == SubsectionFragmentMap.end()) |
| 316 | IP = end(); |
| 317 | else |
| 318 | IP = MI->second; |
| 319 | if (!ExactMatch && Subsection != 0) { |
| 320 | // The GNU as documentation claims that subsections have an alignment of 4, |
| 321 | // although this appears not to be the case. |
| 322 | MCFragment *F = new MCDataFragment(); |
| 323 | SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F)); |
| 324 | getFragmentList().insert(IP, F); |
| 325 | F->setParent(this); |
| 326 | } |
| 327 | return IP; |
| 328 | } |
| 329 | |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 330 | /* *** */ |
| 331 | |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 332 | MCSymbolData::MCSymbolData() : Symbol(nullptr) {} |
Daniel Dunbar | ff3eafd | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 333 | |
Daniel Dunbar | 945e257 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 334 | MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, |
Daniel Dunbar | ff3eafd | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 335 | uint64_t _Offset, MCAssembler *A) |
Daniel Dunbar | 4fa0595 | 2009-09-01 04:09:03 +0000 | [diff] [blame] | 336 | : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset), |
Daniel Dunbar | 2701eee | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 337 | IsExternal(false), IsPrivateExtern(false), |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 338 | CommonSize(0), SymbolSize(nullptr), CommonAlign(0), |
Matt Fleming | c6beca5 | 2010-08-16 18:34:31 +0000 | [diff] [blame] | 339 | Flags(0), Index(0) |
Daniel Dunbar | ff3eafd | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 340 | { |
| 341 | if (A) |
| 342 | A->getSymbolList().push_back(this); |
| 343 | } |
| 344 | |
| 345 | /* *** */ |
| 346 | |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 347 | MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_, |
Daniel Dunbar | 42a037a | 2010-12-17 02:45:41 +0000 | [diff] [blame] | 348 | MCCodeEmitter &Emitter_, MCObjectWriter &Writer_, |
| 349 | raw_ostream &OS_) |
| 350 | : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_), |
Eli Bendersky | 75a7a33 | 2012-12-20 22:51:52 +0000 | [diff] [blame] | 351 | OS(OS_), BundleAlignSize(0), RelaxAll(false), NoExecStack(false), |
Jack Carter | 1bd90ff | 2013-01-30 02:09:52 +0000 | [diff] [blame] | 352 | SubsectionsViaSymbols(false), ELFHeaderEFlags(0) { |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 353 | VersionMinInfo.Major = 0; // Major version == 0 for "none specified" |
Daniel Dunbar | e269773 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 354 | } |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 355 | |
| 356 | MCAssembler::~MCAssembler() { |
| 357 | } |
| 358 | |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 359 | void MCAssembler::reset() { |
| 360 | Sections.clear(); |
| 361 | Symbols.clear(); |
| 362 | SectionMap.clear(); |
| 363 | SymbolMap.clear(); |
| 364 | IndirectSymbols.clear(); |
| 365 | DataRegions.clear(); |
| 366 | ThumbFuncs.clear(); |
| 367 | RelaxAll = false; |
| 368 | NoExecStack = false; |
| 369 | SubsectionsViaSymbols = false; |
Jack Carter | 1bd90ff | 2013-01-30 02:09:52 +0000 | [diff] [blame] | 370 | ELFHeaderEFlags = 0; |
Pedro Artigas | b95c53e | 2012-12-14 18:52:11 +0000 | [diff] [blame] | 371 | |
| 372 | // reset objects owned by us |
| 373 | getBackend().reset(); |
| 374 | getEmitter().reset(); |
| 375 | getWriter().reset(); |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 376 | getLOHContainer().reset(); |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Rafael Espindola | b60c829 | 2014-04-29 12:46:50 +0000 | [diff] [blame] | 379 | bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const { |
| 380 | if (ThumbFuncs.count(Symbol)) |
| 381 | return true; |
| 382 | |
| 383 | if (!Symbol->isVariable()) |
| 384 | return false; |
| 385 | |
Rafael Espindola | 5e09641 | 2014-04-30 12:42:22 +0000 | [diff] [blame] | 386 | // FIXME: It looks like gas supports some cases of the form "foo + 2". It |
Rafael Espindola | b60c829 | 2014-04-29 12:46:50 +0000 | [diff] [blame] | 387 | // is not clear if that is a bug or a feature. |
| 388 | const MCExpr *Expr = Symbol->getVariableValue(); |
| 389 | const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr); |
| 390 | if (!Ref) |
| 391 | return false; |
| 392 | |
| 393 | if (Ref->getKind() != MCSymbolRefExpr::VK_None) |
| 394 | return false; |
| 395 | |
| 396 | const MCSymbol &Sym = Ref->getSymbol(); |
| 397 | if (!isThumbFunc(&Sym)) |
| 398 | return false; |
| 399 | |
| 400 | ThumbFuncs.insert(Symbol); // Cache it. |
| 401 | return true; |
| 402 | } |
| 403 | |
Daniel Dunbar | aa627c3 | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 404 | bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const { |
Daniel Dunbar | c558ec2 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 405 | // Non-temporary labels should always be visible to the linker. |
Daniel Dunbar | aa627c3 | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 406 | if (!Symbol.isTemporary()) |
Daniel Dunbar | c558ec2 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 407 | return true; |
| 408 | |
| 409 | // Absolute temporary labels are never visible. |
Daniel Dunbar | aa627c3 | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 410 | if (!Symbol.isInSection()) |
Daniel Dunbar | c558ec2 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 411 | return false; |
| 412 | |
| 413 | // Otherwise, check if the section requires symbols even for temporary labels. |
Daniel Dunbar | aa627c3 | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 414 | return getBackend().doesSectionRequireSymbols(Symbol.getSection()); |
Daniel Dunbar | c558ec2 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Rafael Espindola | cceded4 | 2010-09-27 18:13:03 +0000 | [diff] [blame] | 417 | const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const { |
Daniel Dunbar | 6e13bb0 | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 418 | // Linker visible symbols define atoms. |
Daniel Dunbar | aa627c3 | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 419 | if (isSymbolLinkerVisible(SD->getSymbol())) |
Daniel Dunbar | 6e13bb0 | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 420 | return SD; |
| 421 | |
| 422 | // Absolute and undefined symbols have no defining atom. |
| 423 | if (!SD->getFragment()) |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 424 | return nullptr; |
Daniel Dunbar | 6e13bb0 | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 425 | |
Daniel Dunbar | ba2f4c3 | 2010-05-12 00:38:17 +0000 | [diff] [blame] | 426 | // Non-linker visible symbols in sections which can't be atomized have no |
| 427 | // defining atom. |
| 428 | if (!getBackend().isSectionAtomizable( |
| 429 | SD->getFragment()->getParent()->getSection())) |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 430 | return nullptr; |
Daniel Dunbar | ba2f4c3 | 2010-05-12 00:38:17 +0000 | [diff] [blame] | 431 | |
Daniel Dunbar | 3937e28 | 2010-05-11 17:22:50 +0000 | [diff] [blame] | 432 | // Otherwise, return the atom for the containing fragment. |
| 433 | return SD->getFragment()->getAtom(); |
Daniel Dunbar | 6e13bb0 | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 436 | bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout, |
Daniel Dunbar | b34440a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 437 | const MCFixup &Fixup, const MCFragment *DF, |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 438 | MCValue &Target, uint64_t &Value) const { |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 439 | ++stats::evaluateFixup; |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 440 | |
Rafael Espindola | 3d5d464 | 2014-03-12 16:55:59 +0000 | [diff] [blame] | 441 | if (!Fixup.getValue()->EvaluateAsRelocatable(Target, &Layout)) |
Jim Grosbach | d856fd0 | 2012-01-27 00:51:23 +0000 | [diff] [blame] | 442 | getContext().FatalError(Fixup.getLoc(), "expected relocatable expression"); |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 443 | |
Rafael Espindola | 0f8abeb | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 444 | bool IsPCRel = Backend.getFixupKindInfo( |
| 445 | Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel; |
| 446 | |
| 447 | bool IsResolved; |
| 448 | if (IsPCRel) { |
| 449 | if (Target.getSymB()) { |
| 450 | IsResolved = false; |
| 451 | } else if (!Target.getSymA()) { |
| 452 | IsResolved = false; |
| 453 | } else { |
Rafael Espindola | 490d02a | 2011-02-16 03:25:55 +0000 | [diff] [blame] | 454 | const MCSymbolRefExpr *A = Target.getSymA(); |
| 455 | const MCSymbol &SA = A->getSymbol(); |
| 456 | if (A->getKind() != MCSymbolRefExpr::VK_None || |
| 457 | SA.AliasedSymbol().isUndefined()) { |
Rafael Espindola | 0f8abeb | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 458 | IsResolved = false; |
| 459 | } else { |
| 460 | const MCSymbolData &DataA = getSymbolData(SA); |
| 461 | IsResolved = |
| 462 | getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA, |
| 463 | *DF, false, true); |
| 464 | } |
| 465 | } |
| 466 | } else { |
| 467 | IsResolved = Target.isAbsolute(); |
| 468 | } |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 469 | |
| 470 | Value = Target.getConstant(); |
| 471 | |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 472 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
Rafael Espindola | 8c3039b | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 473 | const MCSymbol &Sym = A->getSymbol().AliasedSymbol(); |
| 474 | if (Sym.isDefined()) |
Rafael Espindola | 93e3cf0 | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 475 | Value += Layout.getSymbolOffset(&getSymbolData(Sym)); |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 476 | } |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 477 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
Rafael Espindola | 8c3039b | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 478 | const MCSymbol &Sym = B->getSymbol().AliasedSymbol(); |
| 479 | if (Sym.isDefined()) |
Rafael Espindola | 93e3cf0 | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 480 | Value -= Layout.getSymbolOffset(&getSymbolData(Sym)); |
Daniel Dunbar | 5ec4bdd | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 481 | } |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 482 | |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 483 | |
Daniel Dunbar | 0c9d9fd | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 484 | bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags & |
Owen Anderson | 622ad51 | 2010-12-15 18:48:27 +0000 | [diff] [blame] | 485 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits; |
| 486 | assert((ShouldAlignPC ? IsPCRel : true) && |
| 487 | "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!"); |
| 488 | |
Owen Anderson | 3ef19d9 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 489 | if (IsPCRel) { |
Owen Anderson | 7985529 | 2010-12-17 21:49:48 +0000 | [diff] [blame] | 490 | uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset(); |
Jim Grosbach | 6485a79 | 2011-10-26 22:44:41 +0000 | [diff] [blame] | 491 | |
Owen Anderson | 622ad51 | 2010-12-15 18:48:27 +0000 | [diff] [blame] | 492 | // A number of ARM fixups in Thumb mode require that the effective PC |
| 493 | // address be determined as the 32-bit aligned version of the actual offset. |
Owen Anderson | c8fa5fc | 2010-12-15 19:24:24 +0000 | [diff] [blame] | 494 | if (ShouldAlignPC) Offset &= ~0x3; |
Owen Anderson | 7985529 | 2010-12-17 21:49:48 +0000 | [diff] [blame] | 495 | Value -= Offset; |
Owen Anderson | 3ef19d9 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 496 | } |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 497 | |
Jim Grosbach | 7b811d3 | 2012-02-27 21:36:23 +0000 | [diff] [blame] | 498 | // Let the backend adjust the fixup value if necessary, including whether |
| 499 | // we need a relocation. |
| 500 | Backend.processFixupValue(*this, Layout, Fixup, DF, Target, Value, |
| 501 | IsResolved); |
Jim Grosbach | 41955ff | 2010-12-14 18:46:57 +0000 | [diff] [blame] | 502 | |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 503 | return IsResolved; |
| 504 | } |
| 505 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 506 | uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout, |
Rafael Espindola | 6bdb49d | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 507 | const MCFragment &F) const { |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 508 | switch (F.getKind()) { |
| 509 | case MCFragment::FT_Data: |
Eli Bendersky | 5277120 | 2013-01-08 22:05:10 +0000 | [diff] [blame] | 510 | case MCFragment::FT_Relaxable: |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 511 | case MCFragment::FT_CompactEncodedInst: |
Eli Bendersky | 5277120 | 2013-01-08 22:05:10 +0000 | [diff] [blame] | 512 | return cast<MCEncodedFragment>(F).getContents().size(); |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 513 | case MCFragment::FT_Fill: |
| 514 | return cast<MCFillFragment>(F).getSize(); |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 515 | |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 516 | case MCFragment::FT_LEB: |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 517 | return cast<MCLEBFragment>(F).getContents().size(); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 518 | |
Rafael Espindola | 6bdb49d | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 519 | case MCFragment::FT_Align: { |
| 520 | const MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 521 | unsigned Offset = Layout.getFragmentOffset(&AF); |
| 522 | unsigned Size = OffsetToAlignment(Offset, AF.getAlignment()); |
Owen Anderson | 9d0f923 | 2012-08-29 22:18:56 +0000 | [diff] [blame] | 523 | // If we are padding with nops, force the padding to be larger than the |
| 524 | // minimum nop size. |
| 525 | if (Size > 0 && AF.hasEmitNops()) { |
| 526 | while (Size % getBackend().getMinimumNopSize()) |
| 527 | Size += AF.getAlignment(); |
| 528 | } |
Rafael Espindola | 6bdb49d | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 529 | if (Size > AF.getMaxBytesToEmit()) |
| 530 | return 0; |
| 531 | return Size; |
| 532 | } |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 533 | |
Rafael Espindola | 6bdb49d | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 534 | case MCFragment::FT_Org: { |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 535 | const MCOrgFragment &OF = cast<MCOrgFragment>(F); |
Rafael Espindola | 6bdb49d | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 536 | int64_t TargetLocation; |
| 537 | if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout)) |
| 538 | report_fatal_error("expected assembly-time absolute expression"); |
| 539 | |
| 540 | // FIXME: We need a way to communicate this error. |
| 541 | uint64_t FragmentOffset = Layout.getFragmentOffset(&OF); |
| 542 | int64_t Size = TargetLocation - FragmentOffset; |
| 543 | if (Size < 0 || Size >= 0x40000000) |
| 544 | report_fatal_error("invalid .org offset '" + Twine(TargetLocation) + |
| 545 | "' (at offset '" + Twine(FragmentOffset) + "')"); |
| 546 | return Size; |
| 547 | } |
Kevin Enderby | e46564a | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 548 | |
Rafael Espindola | 5665a93 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 549 | case MCFragment::FT_Dwarf: |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 550 | return cast<MCDwarfLineAddrFragment>(F).getContents().size(); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 551 | case MCFragment::FT_DwarfFrame: |
| 552 | return cast<MCDwarfCallFrameFragment>(F).getContents().size(); |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 555 | llvm_unreachable("invalid fragment kind"); |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 558 | void MCAsmLayout::layoutFragment(MCFragment *F) { |
Daniel Dunbar | 9abade1 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 559 | MCFragment *Prev = F->getPrevNode(); |
Daniel Dunbar | a7cc32a | 2010-05-12 21:35:25 +0000 | [diff] [blame] | 560 | |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 561 | // We should never try to recompute something which is valid. |
| 562 | assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!"); |
| 563 | // We should never try to compute the fragment layout if its predecessor |
| 564 | // isn't valid. |
| 565 | assert((!Prev || isFragmentValid(Prev)) && |
| 566 | "Attempt to compute fragment before its predecessor!"); |
Daniel Dunbar | a7cc32a | 2010-05-12 21:35:25 +0000 | [diff] [blame] | 567 | |
| 568 | ++stats::FragmentLayouts; |
| 569 | |
Daniel Dunbar | b9f6ac0 | 2010-05-13 20:40:12 +0000 | [diff] [blame] | 570 | // Compute fragment offset and size. |
Rafael Espindola | 93e3cf0 | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 571 | if (Prev) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 572 | F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev); |
| 573 | else |
| 574 | F->Offset = 0; |
Rafael Espindola | 6624f47 | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 575 | LastValidFragment[F->getParent()] = F; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 576 | |
| 577 | // If bundling is enabled and this fragment has instructions in it, it has to |
| 578 | // obey the bundling restrictions. With padding, we'll have: |
| 579 | // |
| 580 | // |
| 581 | // BundlePadding |
Derek Schuff | b76ec3b | 2013-01-31 17:00:03 +0000 | [diff] [blame] | 582 | // ||| |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 583 | // ------------------------------------- |
| 584 | // Prev |##########| F | |
| 585 | // ------------------------------------- |
| 586 | // ^ |
| 587 | // | |
| 588 | // F->Offset |
| 589 | // |
| 590 | // The fragment's offset will point to after the padding, and its computed |
| 591 | // size won't include the padding. |
| 592 | // |
| 593 | if (Assembler.isBundlingEnabled() && F->hasInstructions()) { |
| 594 | assert(isa<MCEncodedFragment>(F) && |
| 595 | "Only MCEncodedFragment implementations have instructions"); |
| 596 | uint64_t FSize = Assembler.computeFragmentSize(*this, *F); |
| 597 | |
| 598 | if (FSize > Assembler.getBundleAlignSize()) |
| 599 | report_fatal_error("Fragment can't be larger than a bundle size"); |
| 600 | |
| 601 | uint64_t RequiredBundlePadding = computeBundlePadding(F, F->Offset, FSize); |
| 602 | if (RequiredBundlePadding > UINT8_MAX) |
| 603 | report_fatal_error("Padding cannot exceed 255 bytes"); |
| 604 | F->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding)); |
| 605 | F->Offset += RequiredBundlePadding; |
| 606 | } |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 609 | /// \brief Write the contents of a fragment to the given object writer. Expects |
| 610 | /// a MCEncodedFragment. |
| 611 | static void writeFragmentContents(const MCFragment &F, MCObjectWriter *OW) { |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 612 | const MCEncodedFragment &EF = cast<MCEncodedFragment>(F); |
Eli Bendersky | 84b2a79 | 2012-12-07 22:06:56 +0000 | [diff] [blame] | 613 | OW->WriteBytes(EF.getContents()); |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /// \brief Write the fragment \p F to the output file. |
| 617 | static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout, |
| 618 | const MCFragment &F) { |
Daniel Dunbar | 5026928 | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 619 | MCObjectWriter *OW = &Asm.getWriter(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 620 | |
Derek Schuff | b76ec3b | 2013-01-31 17:00:03 +0000 | [diff] [blame] | 621 | // FIXME: Embed in fragments instead? |
| 622 | uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F); |
| 623 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 624 | // Should NOP padding be written out before this fragment? |
| 625 | unsigned BundlePadding = F.getBundlePadding(); |
| 626 | if (BundlePadding > 0) { |
| 627 | assert(Asm.isBundlingEnabled() && |
| 628 | "Writing bundle padding with disabled bundling"); |
| 629 | assert(F.hasInstructions() && |
| 630 | "Writing bundle padding for a fragment without instructions"); |
| 631 | |
Derek Schuff | b76ec3b | 2013-01-31 17:00:03 +0000 | [diff] [blame] | 632 | unsigned TotalLength = BundlePadding + static_cast<unsigned>(FragmentSize); |
| 633 | if (F.alignToBundleEnd() && TotalLength > Asm.getBundleAlignSize()) { |
| 634 | // If the padding itself crosses a bundle boundary, it must be emitted |
| 635 | // in 2 pieces, since even nop instructions must not cross boundaries. |
| 636 | // v--------------v <- BundleAlignSize |
| 637 | // v---------v <- BundlePadding |
| 638 | // ---------------------------- |
| 639 | // | Prev |####|####| F | |
| 640 | // ---------------------------- |
| 641 | // ^-------------------^ <- TotalLength |
| 642 | unsigned DistanceToBoundary = TotalLength - Asm.getBundleAlignSize(); |
| 643 | if (!Asm.getBackend().writeNopData(DistanceToBoundary, OW)) |
| 644 | report_fatal_error("unable to write NOP sequence of " + |
| 645 | Twine(DistanceToBoundary) + " bytes"); |
| 646 | BundlePadding -= DistanceToBoundary; |
| 647 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 648 | if (!Asm.getBackend().writeNopData(BundlePadding, OW)) |
| 649 | report_fatal_error("unable to write NOP sequence of " + |
| 650 | Twine(BundlePadding) + " bytes"); |
| 651 | } |
| 652 | |
| 653 | // This variable (and its dummy usage) is to participate in the assert at |
| 654 | // the end of the function. |
Daniel Dunbar | f027abf | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 655 | uint64_t Start = OW->getStream().tell(); |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 656 | (void) Start; |
Daniel Dunbar | e024def | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 657 | |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 658 | ++stats::EmittedFragments; |
Daniel Dunbar | 5a07d6a | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 659 | |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 660 | switch (F.getKind()) { |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 661 | case MCFragment::FT_Align: { |
Eli Bendersky | c01322e | 2012-12-10 18:59:39 +0000 | [diff] [blame] | 662 | ++stats::EmittedAlignFragments; |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 663 | const MCAlignFragment &AF = cast<MCAlignFragment>(F); |
Daniel Dunbar | 51402b7 | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 664 | assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!"); |
| 665 | |
Eric Christopher | ed420bb | 2013-08-07 18:51:09 +0000 | [diff] [blame] | 666 | uint64_t Count = FragmentSize / AF.getValueSize(); |
| 667 | |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 668 | // FIXME: This error shouldn't actually occur (the front end should emit |
| 669 | // multiple .align directives to enforce the semantics it wants), but is |
| 670 | // severe enough that we want to report it. How to handle this? |
Daniel Dunbar | 2522dd1 | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 671 | if (Count * AF.getValueSize() != FragmentSize) |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 672 | report_fatal_error("undefined .align directive, value size '" + |
Daniel Dunbar | e024def | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 673 | Twine(AF.getValueSize()) + |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 674 | "' is not a divisor of padding size '" + |
Daniel Dunbar | 2522dd1 | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 675 | Twine(FragmentSize) + "'"); |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 676 | |
Kevin Enderby | e83d74f | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 677 | // See if we are aligning with nops, and if so do that first to try to fill |
| 678 | // the Count bytes. Then if that did not fill any bytes or there are any |
Sylvestre Ledru | 35521e2 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 679 | // bytes left to fill use the Value and ValueSize to fill the rest. |
Daniel Dunbar | a9ae3ae | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 680 | // If we are aligning with nops, ask that target to emit the right data. |
Daniel Dunbar | b76df22 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 681 | if (AF.hasEmitNops()) { |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 682 | if (!Asm.getBackend().writeNopData(Count, OW)) |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 683 | report_fatal_error("unable to write nop sequence of " + |
Daniel Dunbar | a9ae3ae | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 684 | Twine(Count) + " bytes"); |
| 685 | break; |
Kevin Enderby | e83d74f | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Daniel Dunbar | a9ae3ae | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 688 | // Otherwise, write out in multiples of the value size. |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 689 | for (uint64_t i = 0; i != Count; ++i) { |
| 690 | switch (AF.getValueSize()) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 691 | default: llvm_unreachable("Invalid size!"); |
Daniel Dunbar | 49e5a8a | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 692 | case 1: OW->Write8 (uint8_t (AF.getValue())); break; |
| 693 | case 2: OW->Write16(uint16_t(AF.getValue())); break; |
| 694 | case 4: OW->Write32(uint32_t(AF.getValue())); break; |
| 695 | case 8: OW->Write64(uint64_t(AF.getValue())); break; |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | break; |
| 699 | } |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 700 | |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 701 | case MCFragment::FT_Data: |
Eli Bendersky | 2ccd044 | 2012-12-07 17:59:21 +0000 | [diff] [blame] | 702 | ++stats::EmittedDataFragments; |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 703 | writeFragmentContents(F, OW); |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 704 | break; |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 705 | |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 706 | case MCFragment::FT_Relaxable: |
Eli Bendersky | 0652dfd | 2013-01-08 17:41:59 +0000 | [diff] [blame] | 707 | ++stats::EmittedRelaxableFragments; |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 708 | writeFragmentContents(F, OW); |
| 709 | break; |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 710 | |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 711 | case MCFragment::FT_CompactEncodedInst: |
| 712 | ++stats::EmittedCompactEncodedInstFragments; |
| 713 | writeFragmentContents(F, OW); |
| 714 | break; |
| 715 | |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 716 | case MCFragment::FT_Fill: { |
Eli Bendersky | c01322e | 2012-12-10 18:59:39 +0000 | [diff] [blame] | 717 | ++stats::EmittedFillFragments; |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 718 | const MCFillFragment &FF = cast<MCFillFragment>(F); |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 719 | |
| 720 | assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!"); |
| 721 | |
Daniel Dunbar | 597eb48 | 2010-05-12 22:51:32 +0000 | [diff] [blame] | 722 | for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) { |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 723 | switch (FF.getValueSize()) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 724 | default: llvm_unreachable("Invalid size!"); |
Daniel Dunbar | 49e5a8a | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 725 | case 1: OW->Write8 (uint8_t (FF.getValue())); break; |
| 726 | case 2: OW->Write16(uint16_t(FF.getValue())); break; |
| 727 | case 4: OW->Write32(uint32_t(FF.getValue())); break; |
| 728 | case 8: OW->Write64(uint64_t(FF.getValue())); break; |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | break; |
| 732 | } |
Daniel Dunbar | e024def | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 733 | |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 734 | case MCFragment::FT_LEB: { |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 735 | const MCLEBFragment &LF = cast<MCLEBFragment>(F); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 736 | OW->WriteBytes(LF.getContents().str()); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 737 | break; |
| 738 | } |
| 739 | |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 740 | case MCFragment::FT_Org: { |
Eli Bendersky | c01322e | 2012-12-10 18:59:39 +0000 | [diff] [blame] | 741 | ++stats::EmittedOrgFragments; |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 742 | const MCOrgFragment &OF = cast<MCOrgFragment>(F); |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 743 | |
Daniel Dunbar | 2522dd1 | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 744 | for (uint64_t i = 0, e = FragmentSize; i != e; ++i) |
Daniel Dunbar | 49e5a8a | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 745 | OW->Write8(uint8_t(OF.getValue())); |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 746 | |
| 747 | break; |
| 748 | } |
Kevin Enderby | e46564a | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 749 | |
| 750 | case MCFragment::FT_Dwarf: { |
| 751 | const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 752 | OW->WriteBytes(OF.getContents().str()); |
Kevin Enderby | e46564a | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 753 | break; |
| 754 | } |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 755 | case MCFragment::FT_DwarfFrame: { |
| 756 | const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F); |
| 757 | OW->WriteBytes(CF.getContents().str()); |
| 758 | break; |
| 759 | } |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 762 | assert(OW->getStream().tell() - Start == FragmentSize && |
| 763 | "The stream should advance by fragment size"); |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 766 | void MCAssembler::writeSectionData(const MCSectionData *SD, |
Daniel Dunbar | 5026928 | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 767 | const MCAsmLayout &Layout) const { |
Daniel Dunbar | 42a39d0 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 768 | // Ignore virtual sections. |
Rafael Espindola | 7a2cd8b | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 769 | if (SD->getSection().isVirtualSection()) { |
Daniel Dunbar | 5a93253 | 2010-05-13 03:50:50 +0000 | [diff] [blame] | 770 | assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!"); |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 771 | |
| 772 | // Check that contents are only things legal inside a virtual section. |
| 773 | for (MCSectionData::const_iterator it = SD->begin(), |
| 774 | ie = SD->end(); it != ie; ++it) { |
| 775 | switch (it->getKind()) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 776 | default: llvm_unreachable("Invalid fragment in virtual section!"); |
Daniel Dunbar | 8e92d9b | 2010-08-18 18:22:37 +0000 | [diff] [blame] | 777 | case MCFragment::FT_Data: { |
| 778 | // Check that we aren't trying to write a non-zero contents (or fixups) |
| 779 | // into a virtual section. This is to support clients which use standard |
| 780 | // directives to fill the contents of virtual sections. |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 781 | const MCDataFragment &DF = cast<MCDataFragment>(*it); |
Daniel Dunbar | 8e92d9b | 2010-08-18 18:22:37 +0000 | [diff] [blame] | 782 | assert(DF.fixup_begin() == DF.fixup_end() && |
| 783 | "Cannot have fixups in virtual section!"); |
| 784 | for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i) |
| 785 | assert(DF.getContents()[i] == 0 && |
| 786 | "Invalid data value for virtual section!"); |
| 787 | break; |
| 788 | } |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 789 | case MCFragment::FT_Align: |
Daniel Dunbar | 8e92d9b | 2010-08-18 18:22:37 +0000 | [diff] [blame] | 790 | // Check that we aren't trying to write a non-zero value into a virtual |
| 791 | // section. |
Serge Pavlov | 24a3ebb | 2013-06-27 14:35:03 +0000 | [diff] [blame] | 792 | assert((cast<MCAlignFragment>(it)->getValueSize() == 0 || |
| 793 | cast<MCAlignFragment>(it)->getValue() == 0) && |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 794 | "Invalid align in virtual section!"); |
| 795 | break; |
| 796 | case MCFragment::FT_Fill: |
Serge Pavlov | 24a3ebb | 2013-06-27 14:35:03 +0000 | [diff] [blame] | 797 | assert((cast<MCFillFragment>(it)->getValueSize() == 0 || |
| 798 | cast<MCFillFragment>(it)->getValue() == 0) && |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 799 | "Invalid fill in virtual section!"); |
| 800 | break; |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
Daniel Dunbar | 42a39d0 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 804 | return; |
| 805 | } |
| 806 | |
Daniel Dunbar | 5026928 | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 807 | uint64_t Start = getWriter().getStream().tell(); |
Jim Grosbach | 0aac6ce | 2012-09-18 23:05:18 +0000 | [diff] [blame] | 808 | (void)Start; |
Daniel Dunbar | e024def | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 809 | |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 810 | for (MCSectionData::const_iterator it = SD->begin(), ie = SD->end(); |
| 811 | it != ie; ++it) |
| 812 | writeFragment(*this, Layout, *it); |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 813 | |
Daniel Dunbar | 5026928 | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 814 | assert(getWriter().getStream().tell() - Start == |
| 815 | Layout.getSectionAddressSize(SD)); |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 818 | std::pair<uint64_t, bool> MCAssembler::handleFixup(const MCAsmLayout &Layout, |
| 819 | MCFragment &F, |
| 820 | const MCFixup &Fixup) { |
Joerg Sonnenberger | 808df67 | 2014-01-13 15:50:36 +0000 | [diff] [blame] | 821 | // Evaluate the fixup. |
| 822 | MCValue Target; |
| 823 | uint64_t FixedValue; |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 824 | bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags & |
| 825 | MCFixupKindInfo::FKF_IsPCRel; |
Joerg Sonnenberger | 808df67 | 2014-01-13 15:50:36 +0000 | [diff] [blame] | 826 | if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) { |
| 827 | // The fixup was unresolved, we need a relocation. Inform the object |
| 828 | // writer of the relocation, and give it an opportunity to adjust the |
| 829 | // fixup value if need be. |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 830 | getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, IsPCRel, |
| 831 | FixedValue); |
Joerg Sonnenberger | 808df67 | 2014-01-13 15:50:36 +0000 | [diff] [blame] | 832 | } |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 833 | return std::make_pair(FixedValue, IsPCRel); |
Joerg Sonnenberger | 808df67 | 2014-01-13 15:50:36 +0000 | [diff] [blame] | 834 | } |
Rafael Espindola | 0f30fec | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 835 | |
Daniel Dunbar | 42a037a | 2010-12-17 02:45:41 +0000 | [diff] [blame] | 836 | void MCAssembler::Finish() { |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 837 | DEBUG_WITH_TYPE("mc-dump", { |
| 838 | llvm::errs() << "assembler backend - pre-layout\n--\n"; |
| 839 | dump(); }); |
| 840 | |
Daniel Dunbar | c7c53ea | 2010-05-13 02:34:14 +0000 | [diff] [blame] | 841 | // Create the layout object. |
Daniel Dunbar | 32ffc58 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 842 | MCAsmLayout Layout(*this); |
Daniel Dunbar | c7c53ea | 2010-05-13 02:34:14 +0000 | [diff] [blame] | 843 | |
Daniel Dunbar | 8f9d226 | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 844 | // Create dummy fragments and assign section ordinals. |
Daniel Dunbar | aa8bd69 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 845 | unsigned SectionIndex = 0; |
Daniel Dunbar | aa8bd69 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 846 | for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) { |
| 847 | // Create dummy fragments to eliminate any empty sections, this simplifies |
| 848 | // layout. |
Duncan Sands | a85a907 | 2010-06-29 13:30:08 +0000 | [diff] [blame] | 849 | if (it->getFragmentList().empty()) |
Rafael Espindola | f667d92 | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 850 | new MCDataFragment(it); |
Daniel Dunbar | aa8bd69 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 851 | |
| 852 | it->setOrdinal(SectionIndex++); |
Daniel Dunbar | 8f9d226 | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 853 | } |
Daniel Dunbar | aa8bd69 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 854 | |
Daniel Dunbar | 8f9d226 | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 855 | // Assign layout order indices to sections and fragments. |
Daniel Dunbar | 8f9d226 | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 856 | for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) { |
| 857 | MCSectionData *SD = Layout.getSectionOrder()[i]; |
| 858 | SD->setLayoutOrder(i); |
| 859 | |
Rafael Espindola | 6624f47 | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 860 | unsigned FragmentIndex = 0; |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 861 | for (MCSectionData::iterator iFrag = SD->begin(), iFragEnd = SD->end(); |
| 862 | iFrag != iFragEnd; ++iFrag) |
| 863 | iFrag->setLayoutOrder(FragmentIndex++); |
Daniel Dunbar | aa8bd69 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Daniel Dunbar | c7c53ea | 2010-05-13 02:34:14 +0000 | [diff] [blame] | 866 | // Layout until everything fits. |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 867 | while (layoutOnce(Layout)) |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 868 | continue; |
| 869 | |
| 870 | DEBUG_WITH_TYPE("mc-dump", { |
Daniel Dunbar | b31c49a | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 871 | llvm::errs() << "assembler backend - post-relaxation\n--\n"; |
| 872 | dump(); }); |
| 873 | |
| 874 | // Finalize the layout, including fragment lowering. |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 875 | finishLayout(Layout); |
Daniel Dunbar | b31c49a | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 876 | |
| 877 | DEBUG_WITH_TYPE("mc-dump", { |
| 878 | llvm::errs() << "assembler backend - final-layout\n--\n"; |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 879 | dump(); }); |
| 880 | |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 881 | uint64_t StartOffset = OS.tell(); |
Reid Kleckner | d85e3c5 | 2010-07-22 05:58:53 +0000 | [diff] [blame] | 882 | |
Daniel Dunbar | d84d196 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 883 | // Allow the object writer a chance to perform post-layout binding (for |
| 884 | // example, to set the index fields in the symbol data). |
Daniel Dunbar | 5026928 | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 885 | getWriter().ExecutePostLayoutBinding(*this, Layout); |
Daniel Dunbar | d84d196 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 886 | |
Daniel Dunbar | 3cab275 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 887 | // Evaluate and apply the fixups, generating relocation entries as necessary. |
Daniel Dunbar | 3cab275 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 888 | for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) { |
| 889 | for (MCSectionData::iterator it2 = it->begin(), |
| 890 | ie2 = it->end(); it2 != ie2; ++it2) { |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 891 | MCEncodedFragmentWithFixups *F = |
| 892 | dyn_cast<MCEncodedFragmentWithFixups>(it2); |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 893 | if (F) { |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 894 | for (MCEncodedFragmentWithFixups::fixup_iterator it3 = F->fixup_begin(), |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 895 | ie3 = F->fixup_end(); it3 != ie3; ++it3) { |
Rafael Espindola | 0f30fec | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 896 | MCFixup &Fixup = *it3; |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 897 | uint64_t FixedValue; |
| 898 | bool IsPCRel; |
| 899 | std::tie(FixedValue, IsPCRel) = handleFixup(Layout, *F, Fixup); |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 900 | getBackend().applyFixup(Fixup, F->getContents().data(), |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 901 | F->getContents().size(), FixedValue, IsPCRel); |
Rafael Espindola | 0f30fec | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 902 | } |
Daniel Dunbar | 3cab275 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
Daniel Dunbar | d84d196 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 907 | // Write the object file. |
Daniel Dunbar | 5026928 | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 908 | getWriter().WriteObject(*this, Layout); |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 909 | |
| 910 | stats::ObjectBytes += OS.tell() - StartOffset; |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 913 | bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup, |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 914 | const MCRelaxableFragment *DF, |
Daniel Dunbar | 32ffc58 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 915 | const MCAsmLayout &Layout) const { |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 916 | // If we cannot resolve the fixup value, it requires relaxation. |
| 917 | MCValue Target; |
| 918 | uint64_t Value; |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 919 | if (!evaluateFixup(Layout, Fixup, DF, Target, Value)) |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 920 | return true; |
| 921 | |
Jim Grosbach | 25b63fa | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 922 | return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout); |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 925 | bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F, |
Daniel Dunbar | de04b3f | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 926 | const MCAsmLayout &Layout) const { |
| 927 | // If this inst doesn't ever need relaxation, ignore it. This occurs when we |
| 928 | // are intentionally pushing out inst fragments, or because we relaxed a |
| 929 | // previous instruction to one that doesn't need relaxation. |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 930 | if (!getBackend().mayNeedRelaxation(F->getInst())) |
Daniel Dunbar | de04b3f | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 931 | return false; |
| 932 | |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 933 | for (MCRelaxableFragment::const_fixup_iterator it = F->fixup_begin(), |
| 934 | ie = F->fixup_end(); it != ie; ++it) |
| 935 | if (fixupNeedsRelaxation(*it, F, Layout)) |
Daniel Dunbar | de04b3f | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 936 | return true; |
| 937 | |
| 938 | return false; |
| 939 | } |
| 940 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 941 | bool MCAssembler::relaxInstruction(MCAsmLayout &Layout, |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 942 | MCRelaxableFragment &F) { |
| 943 | if (!fragmentNeedsRelaxation(&F, Layout)) |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 944 | return false; |
| 945 | |
| 946 | ++stats::RelaxedInstructions; |
| 947 | |
| 948 | // FIXME-PERF: We could immediately lower out instructions if we can tell |
| 949 | // they are fully resolved, to avoid retesting on later passes. |
| 950 | |
| 951 | // Relax the fragment. |
| 952 | |
| 953 | MCInst Relaxed; |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 954 | getBackend().relaxInstruction(F.getInst(), Relaxed); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 955 | |
| 956 | // Encode the new instruction. |
| 957 | // |
| 958 | // FIXME-PERF: If it matters, we could let the target do this. It can |
| 959 | // probably do so more efficiently in many cases. |
| 960 | SmallVector<MCFixup, 4> Fixups; |
| 961 | SmallString<256> Code; |
| 962 | raw_svector_ostream VecOS(Code); |
David Woodhouse | 9784cef | 2014-01-28 23:13:07 +0000 | [diff] [blame] | 963 | getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups, F.getSubtargetInfo()); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 964 | VecOS.flush(); |
| 965 | |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 966 | // Update the fragment. |
| 967 | F.setInst(Relaxed); |
| 968 | F.getContents() = Code; |
| 969 | F.getFixups() = Fixups; |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 970 | |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 971 | return true; |
| 972 | } |
| 973 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 974 | bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) { |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 975 | int64_t Value = 0; |
| 976 | uint64_t OldSize = LF.getContents().size(); |
Rafael Espindola | 3584ab6 | 2011-04-26 02:17:58 +0000 | [diff] [blame] | 977 | bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout); |
| 978 | (void)IsAbs; |
| 979 | assert(IsAbs); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 980 | SmallString<8> &Data = LF.getContents(); |
| 981 | Data.clear(); |
| 982 | raw_svector_ostream OSE(Data); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 983 | if (LF.isSigned()) |
Jim Grosbach | bf387df | 2012-08-08 23:56:06 +0000 | [diff] [blame] | 984 | encodeSLEB128(Value, OSE); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 985 | else |
Jim Grosbach | bf387df | 2012-08-08 23:56:06 +0000 | [diff] [blame] | 986 | encodeULEB128(Value, OSE); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 987 | OSE.flush(); |
| 988 | return OldSize != LF.getContents().size(); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 991 | bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout, |
Jim Grosbach | 46be301 | 2011-12-06 00:13:09 +0000 | [diff] [blame] | 992 | MCDwarfLineAddrFragment &DF) { |
Ulrich Weigand | 32d725b | 2013-06-12 14:46:54 +0000 | [diff] [blame] | 993 | MCContext &Context = Layout.getAssembler().getContext(); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 994 | int64_t AddrDelta = 0; |
| 995 | uint64_t OldSize = DF.getContents().size(); |
Rafael Espindola | d91e119 | 2010-12-22 22:04:28 +0000 | [diff] [blame] | 996 | bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout); |
| 997 | (void)IsAbs; |
| 998 | assert(IsAbs); |
Rafael Espindola | 5665a93 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 999 | int64_t LineDelta; |
| 1000 | LineDelta = DF.getLineDelta(); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 1001 | SmallString<8> &Data = DF.getContents(); |
| 1002 | Data.clear(); |
| 1003 | raw_svector_ostream OSE(Data); |
Ulrich Weigand | 32d725b | 2013-06-12 14:46:54 +0000 | [diff] [blame] | 1004 | MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OSE); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 1005 | OSE.flush(); |
| 1006 | return OldSize != Data.size(); |
Rafael Espindola | 5665a93 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 1009 | bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout, |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1010 | MCDwarfCallFrameFragment &DF) { |
Ulrich Weigand | 32d725b | 2013-06-12 14:46:54 +0000 | [diff] [blame] | 1011 | MCContext &Context = Layout.getAssembler().getContext(); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1012 | int64_t AddrDelta = 0; |
| 1013 | uint64_t OldSize = DF.getContents().size(); |
| 1014 | bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout); |
| 1015 | (void)IsAbs; |
| 1016 | assert(IsAbs); |
| 1017 | SmallString<8> &Data = DF.getContents(); |
| 1018 | Data.clear(); |
| 1019 | raw_svector_ostream OSE(Data); |
Ulrich Weigand | 32d725b | 2013-06-12 14:46:54 +0000 | [diff] [blame] | 1020 | MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OSE); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1021 | OSE.flush(); |
| 1022 | return OldSize != Data.size(); |
| 1023 | } |
| 1024 | |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 1025 | bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD) { |
| 1026 | // Holds the first fragment which needed relaxing during this layout. It will |
| 1027 | // remain NULL if none were relaxed. |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 1028 | // When a fragment is relaxed, all the fragments following it should get |
| 1029 | // invalidated because their offset is going to change. |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 1030 | MCFragment *FirstRelaxedFragment = nullptr; |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 1031 | |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 1032 | // Attempt to relax all the fragments in the section. |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 1033 | for (MCSectionData::iterator I = SD.begin(), IE = SD.end(); I != IE; ++I) { |
| 1034 | // Check if this is a fragment that needs relaxation. |
| 1035 | bool RelaxedFrag = false; |
| 1036 | switch(I->getKind()) { |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 1037 | default: |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 1038 | break; |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 1039 | case MCFragment::FT_Relaxable: |
Eli Bendersky | 0f74f17 | 2012-12-11 17:16:00 +0000 | [diff] [blame] | 1040 | assert(!getRelaxAll() && |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 1041 | "Did not expect a MCRelaxableFragment in RelaxAll mode"); |
| 1042 | RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I)); |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 1043 | break; |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 1044 | case MCFragment::FT_Dwarf: |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 1045 | RelaxedFrag = relaxDwarfLineAddr(Layout, |
| 1046 | *cast<MCDwarfLineAddrFragment>(I)); |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 1047 | break; |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1048 | case MCFragment::FT_DwarfFrame: |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 1049 | RelaxedFrag = |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 1050 | relaxDwarfCallFrameFragment(Layout, |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 1051 | *cast<MCDwarfCallFrameFragment>(I)); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1052 | break; |
| 1053 | case MCFragment::FT_LEB: |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 1054 | RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I)); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1055 | break; |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 1056 | } |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 1057 | if (RelaxedFrag && !FirstRelaxedFragment) |
| 1058 | FirstRelaxedFragment = I; |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 1059 | } |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 1060 | if (FirstRelaxedFragment) { |
Derek Schuff | 90aa1d8 | 2013-02-05 17:55:27 +0000 | [diff] [blame] | 1061 | Layout.invalidateFragmentsFrom(FirstRelaxedFragment); |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 1062 | return true; |
| 1063 | } |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 1067 | bool MCAssembler::layoutOnce(MCAsmLayout &Layout) { |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 1068 | ++stats::RelaxationSteps; |
| 1069 | |
Daniel Dunbar | 6432bd7 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 1070 | bool WasRelaxed = false; |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 1071 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 1072 | MCSectionData &SD = *it; |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 1073 | while (layoutSectionOnce(Layout, SD)) |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 1074 | WasRelaxed = true; |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Daniel Dunbar | 6432bd7 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 1077 | return WasRelaxed; |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1078 | } |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1079 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 1080 | void MCAssembler::finishLayout(MCAsmLayout &Layout) { |
Rafael Espindola | 8867390 | 2010-12-04 22:47:22 +0000 | [diff] [blame] | 1081 | // The layout is done. Mark every fragment as valid. |
Rafael Espindola | 6624f47 | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 1082 | for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) { |
| 1083 | Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin()); |
| 1084 | } |
Daniel Dunbar | b31c49a | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1087 | // Debugging methods |
| 1088 | |
| 1089 | namespace llvm { |
| 1090 | |
Daniel Dunbar | b34440a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 1091 | raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) { |
| 1092 | OS << "<MCFixup" << " Offset:" << AF.getOffset() |
Daniel Dunbar | 353a91ff | 2010-05-26 15:18:31 +0000 | [diff] [blame] | 1093 | << " Value:" << *AF.getValue() |
| 1094 | << " Kind:" << AF.getKind() << ">"; |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1095 | return OS; |
| 1096 | } |
| 1097 | |
| 1098 | } |
| 1099 | |
Manman Ren | 49d684e | 2012-09-12 05:06:18 +0000 | [diff] [blame] | 1100 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1101 | void MCFragment::dump() { |
| 1102 | raw_ostream &OS = llvm::errs(); |
| 1103 | |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1104 | OS << "<"; |
| 1105 | switch (getKind()) { |
| 1106 | case MCFragment::FT_Align: OS << "MCAlignFragment"; break; |
| 1107 | case MCFragment::FT_Data: OS << "MCDataFragment"; break; |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 1108 | case MCFragment::FT_CompactEncodedInst: |
| 1109 | OS << "MCCompactEncodedInstFragment"; break; |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1110 | case MCFragment::FT_Fill: OS << "MCFillFragment"; break; |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 1111 | case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break; |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1112 | case MCFragment::FT_Org: OS << "MCOrgFragment"; break; |
Kevin Enderby | e46564a | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 1113 | case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break; |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1114 | case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break; |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 1115 | case MCFragment::FT_LEB: OS << "MCLEBFragment"; break; |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Daniel Dunbar | 8f9d226 | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 1118 | OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 1119 | << " Offset:" << Offset |
| 1120 | << " HasInstructions:" << hasInstructions() |
Derek Schuff | 90aa1d8 | 2013-02-05 17:55:27 +0000 | [diff] [blame] | 1121 | << " BundlePadding:" << static_cast<unsigned>(getBundlePadding()) << ">"; |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1122 | |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1123 | switch (getKind()) { |
| 1124 | case MCFragment::FT_Align: { |
| 1125 | const MCAlignFragment *AF = cast<MCAlignFragment>(this); |
| 1126 | if (AF->hasEmitNops()) |
| 1127 | OS << " (emit nops)"; |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1128 | OS << "\n "; |
| 1129 | OS << " Alignment:" << AF->getAlignment() |
| 1130 | << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize() |
| 1131 | << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">"; |
| 1132 | break; |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1133 | } |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1134 | case MCFragment::FT_Data: { |
| 1135 | const MCDataFragment *DF = cast<MCDataFragment>(this); |
| 1136 | OS << "\n "; |
| 1137 | OS << " Contents:["; |
| 1138 | const SmallVectorImpl<char> &Contents = DF->getContents(); |
| 1139 | for (unsigned i = 0, e = Contents.size(); i != e; ++i) { |
| 1140 | if (i) OS << ","; |
| 1141 | OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); |
Daniel Dunbar | 97867a9 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 1142 | } |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1143 | OS << "] (" << Contents.size() << " bytes)"; |
| 1144 | |
Eli Bendersky | 02631c4 | 2012-12-05 22:11:02 +0000 | [diff] [blame] | 1145 | if (DF->fixup_begin() != DF->fixup_end()) { |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1146 | OS << ",\n "; |
| 1147 | OS << " Fixups:["; |
| 1148 | for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(), |
| 1149 | ie = DF->fixup_end(); it != ie; ++it) { |
| 1150 | if (it != DF->fixup_begin()) OS << ",\n "; |
| 1151 | OS << *it; |
| 1152 | } |
| 1153 | OS << "]"; |
| 1154 | } |
| 1155 | break; |
Daniel Dunbar | 97867a9 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 1156 | } |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 1157 | case MCFragment::FT_CompactEncodedInst: { |
| 1158 | const MCCompactEncodedInstFragment *CEIF = |
| 1159 | cast<MCCompactEncodedInstFragment>(this); |
| 1160 | OS << "\n "; |
| 1161 | OS << " Contents:["; |
| 1162 | const SmallVectorImpl<char> &Contents = CEIF->getContents(); |
| 1163 | for (unsigned i = 0, e = Contents.size(); i != e; ++i) { |
| 1164 | if (i) OS << ","; |
| 1165 | OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); |
| 1166 | } |
| 1167 | OS << "] (" << Contents.size() << " bytes)"; |
| 1168 | break; |
| 1169 | } |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1170 | case MCFragment::FT_Fill: { |
| 1171 | const MCFillFragment *FF = cast<MCFillFragment>(this); |
| 1172 | OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize() |
| 1173 | << " Size:" << FF->getSize(); |
| 1174 | break; |
| 1175 | } |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 1176 | case MCFragment::FT_Relaxable: { |
| 1177 | const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this); |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1178 | OS << "\n "; |
| 1179 | OS << " Inst:"; |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 1180 | F->getInst().dump_pretty(OS); |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1181 | break; |
| 1182 | } |
| 1183 | case MCFragment::FT_Org: { |
| 1184 | const MCOrgFragment *OF = cast<MCOrgFragment>(this); |
| 1185 | OS << "\n "; |
| 1186 | OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue(); |
| 1187 | break; |
| 1188 | } |
Kevin Enderby | e46564a | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 1189 | case MCFragment::FT_Dwarf: { |
| 1190 | const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this); |
| 1191 | OS << "\n "; |
| 1192 | OS << " AddrDelta:" << OF->getAddrDelta() |
| 1193 | << " LineDelta:" << OF->getLineDelta(); |
| 1194 | break; |
| 1195 | } |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 1196 | case MCFragment::FT_DwarfFrame: { |
| 1197 | const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this); |
| 1198 | OS << "\n "; |
| 1199 | OS << " AddrDelta:" << CF->getAddrDelta(); |
| 1200 | break; |
| 1201 | } |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 1202 | case MCFragment::FT_LEB: { |
| 1203 | const MCLEBFragment *LF = cast<MCLEBFragment>(this); |
| 1204 | OS << "\n "; |
| 1205 | OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned(); |
| 1206 | break; |
| 1207 | } |
Daniel Dunbar | 870e575 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 1208 | } |
Daniel Dunbar | 97867a9 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 1209 | OS << ">"; |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1212 | void MCSectionData::dump() { |
| 1213 | raw_ostream &OS = llvm::errs(); |
| 1214 | |
| 1215 | OS << "<MCSectionData"; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 1216 | OS << " Alignment:" << getAlignment() |
| 1217 | << " Fragments:[\n "; |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1218 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 1219 | if (it != begin()) OS << ",\n "; |
| 1220 | it->dump(); |
| 1221 | } |
| 1222 | OS << "]>"; |
| 1223 | } |
| 1224 | |
| 1225 | void MCSymbolData::dump() { |
| 1226 | raw_ostream &OS = llvm::errs(); |
| 1227 | |
| 1228 | OS << "<MCSymbolData Symbol:" << getSymbol() |
| 1229 | << " Fragment:" << getFragment() << " Offset:" << getOffset() |
| 1230 | << " Flags:" << getFlags() << " Index:" << getIndex(); |
| 1231 | if (isCommon()) |
| 1232 | OS << " (common, size:" << getCommonSize() |
| 1233 | << " align: " << getCommonAlignment() << ")"; |
| 1234 | if (isExternal()) |
| 1235 | OS << " (external)"; |
| 1236 | if (isPrivateExtern()) |
| 1237 | OS << " (private extern)"; |
| 1238 | OS << ">"; |
| 1239 | } |
| 1240 | |
| 1241 | void MCAssembler::dump() { |
| 1242 | raw_ostream &OS = llvm::errs(); |
| 1243 | |
| 1244 | OS << "<MCAssembler\n"; |
Daniel Dunbar | cb01210 | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 1245 | OS << " Sections:[\n "; |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1246 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 1247 | if (it != begin()) OS << ",\n "; |
| 1248 | it->dump(); |
| 1249 | } |
| 1250 | OS << "],\n"; |
| 1251 | OS << " Symbols:["; |
| 1252 | |
| 1253 | for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { |
Daniel Dunbar | cb01210 | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 1254 | if (it != symbol_begin()) OS << ",\n "; |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 1255 | it->dump(); |
| 1256 | } |
| 1257 | OS << "]>\n"; |
| 1258 | } |
Manman Ren | c3366cc | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 1259 | #endif |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 1260 | |
| 1261 | // anchors for MC*Fragment vtables |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 1262 | void MCEncodedFragment::anchor() { } |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 1263 | void MCEncodedFragmentWithFixups::anchor() { } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 1264 | void MCDataFragment::anchor() { } |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 1265 | void MCCompactEncodedInstFragment::anchor() { } |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 1266 | void MCRelaxableFragment::anchor() { } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 1267 | void MCAlignFragment::anchor() { } |
| 1268 | void MCFillFragment::anchor() { } |
| 1269 | void MCOrgFragment::anchor() { } |
| 1270 | void MCLEBFragment::anchor() { } |
| 1271 | void MCDwarfLineAddrFragment::anchor() { } |
| 1272 | void MCDwarfCallFrameFragment::anchor() { } |