Daniel Dunbar | fb4a6b3 | 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 | |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "assembler" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAssembler.h" |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmLayout.h" |
Daniel Dunbar | b36052f | 2010-03-19 10:43:23 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCCodeEmitter.h" |
Rafael Espindola | fea753b | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCExpr.h" |
Craig Topper | f1d0f77 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCFixupKindInfo.h" |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCObjectWriter.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSection.h" |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCSymbol.h" |
| 20 | #include "llvm/MC/MCValue.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCDwarf.h" |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCAsmBackend.h" |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 29 | #include "llvm/Support/TargetRegistry.h" |
Jim Grosbach | 2d39a0e | 2012-08-08 23:56:06 +0000 | [diff] [blame^] | 30 | #include "llvm/Support/LEB128.h" |
Daniel Dunbar | f634676 | 2010-02-13 09:29:02 +0000 | [diff] [blame] | 31 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | namespace stats { |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 36 | STATISTIC(EmittedFragments, "Number of emitted assembler fragments"); |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 37 | STATISTIC(evaluateFixup, "Number of evaluated fixups"); |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 38 | STATISTIC(FragmentLayouts, "Number of fragment layouts"); |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 39 | STATISTIC(ObjectBytes, "Number of emitted object file bytes"); |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 40 | STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps"); |
| 41 | STATISTIC(RelaxedInstructions, "Number of relaxed instructions"); |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 44 | |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 45 | // FIXME FIXME FIXME: There are number of places in this file where we convert |
| 46 | // what is a 64-bit assembler value used for computation into a value in the |
| 47 | // object file, which may truncate it. We should detect that truncation where |
| 48 | // invalid and report errors back. |
| 49 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 50 | /* *** */ |
| 51 | |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 52 | MCAsmLayout::MCAsmLayout(MCAssembler &Asm) |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 53 | : Assembler(Asm), LastValidFragment() |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 54 | { |
Daniel Dunbar | bc1a0cf | 2010-05-12 15:42:59 +0000 | [diff] [blame] | 55 | // Compute the section layout order. Virtual sections must go last. |
| 56 | for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 57 | if (!it->getSection().isVirtualSection()) |
Daniel Dunbar | bc1a0cf | 2010-05-12 15:42:59 +0000 | [diff] [blame] | 58 | SectionOrder.push_back(&*it); |
| 59 | for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 60 | if (it->getSection().isVirtualSection()) |
Daniel Dunbar | bc1a0cf | 2010-05-12 15:42:59 +0000 | [diff] [blame] | 61 | SectionOrder.push_back(&*it); |
| 62 | } |
| 63 | |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 64 | bool MCAsmLayout::isFragmentUpToDate(const MCFragment *F) const { |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 65 | const MCSectionData &SD = *F->getParent(); |
| 66 | const MCFragment *LastValid = LastValidFragment.lookup(&SD); |
| 67 | if (!LastValid) |
| 68 | return false; |
| 69 | assert(LastValid->getParent() == F->getParent()); |
| 70 | return F->getLayoutOrder() <= LastValid->getLayoutOrder(); |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Rafael Espindola | a9d4281 | 2010-11-23 08:08:33 +0000 | [diff] [blame] | 73 | void MCAsmLayout::Invalidate(MCFragment *F) { |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 74 | // If this fragment wasn't already up-to-date, we don't need to do anything. |
| 75 | if (!isFragmentUpToDate(F)) |
| 76 | return; |
Daniel Dunbar | 0cc8bd4 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 77 | |
Rafael Espindola | 2bf6afc | 2010-12-15 08:45:53 +0000 | [diff] [blame] | 78 | // Otherwise, reset the last valid fragment to this fragment. |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 79 | const MCSectionData &SD = *F->getParent(); |
Rafael Espindola | 2bf6afc | 2010-12-15 08:45:53 +0000 | [diff] [blame] | 80 | LastValidFragment[&SD] = F; |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 81 | } |
Daniel Dunbar | 0cc8bd4 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 82 | |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 83 | void MCAsmLayout::EnsureValid(const MCFragment *F) const { |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 84 | MCSectionData &SD = *F->getParent(); |
| 85 | |
| 86 | MCFragment *Cur = LastValidFragment[&SD]; |
| 87 | if (!Cur) |
| 88 | Cur = &*SD.begin(); |
| 89 | else |
| 90 | Cur = Cur->getNextNode(); |
| 91 | |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 92 | // Advance the layout position until the fragment is up-to-date. |
| 93 | while (!isFragmentUpToDate(F)) { |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 94 | const_cast<MCAsmLayout*>(this)->LayoutFragment(Cur); |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 95 | Cur = Cur->getNextNode(); |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 96 | } |
Daniel Dunbar | 0cc8bd4 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 99 | uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const { |
Daniel Dunbar | 47b3ec4 | 2010-05-14 00:51:14 +0000 | [diff] [blame] | 100 | EnsureValid(F); |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 101 | assert(F->Offset != ~UINT64_C(0) && "Address not set!"); |
| 102 | return F->Offset; |
| 103 | } |
| 104 | |
Rafael Espindola | ffd902b | 2010-12-06 02:57:26 +0000 | [diff] [blame] | 105 | uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const { |
Daniel Dunbar | c8497b6 | 2011-04-29 18:20:20 +0000 | [diff] [blame] | 106 | const MCSymbol &S = SD->getSymbol(); |
| 107 | |
| 108 | // If this is a variable, then recursively evaluate now. |
| 109 | if (S.isVariable()) { |
| 110 | MCValue Target; |
| 111 | if (!S.getVariableValue()->EvaluateAsRelocatable(Target, *this)) |
| 112 | report_fatal_error("unable to evaluate offset for variable '" + |
| 113 | S.getName() + "'"); |
| 114 | |
| 115 | // Verify that any used symbols are defined. |
| 116 | if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined()) |
| 117 | report_fatal_error("unable to evaluate offset to undefined symbol '" + |
| 118 | Target.getSymA()->getSymbol().getName() + "'"); |
| 119 | if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined()) |
| 120 | report_fatal_error("unable to evaluate offset to undefined symbol '" + |
| 121 | Target.getSymB()->getSymbol().getName() + "'"); |
Jim Grosbach | 684457d | 2011-10-26 22:44:41 +0000 | [diff] [blame] | 122 | |
Daniel Dunbar | c8497b6 | 2011-04-29 18:20:20 +0000 | [diff] [blame] | 123 | uint64_t Offset = Target.getConstant(); |
| 124 | if (Target.getSymA()) |
| 125 | Offset += getSymbolOffset(&Assembler.getSymbolData( |
| 126 | Target.getSymA()->getSymbol())); |
| 127 | if (Target.getSymB()) |
| 128 | Offset -= getSymbolOffset(&Assembler.getSymbolData( |
| 129 | Target.getSymB()->getSymbol())); |
| 130 | return Offset; |
| 131 | } |
| 132 | |
Rafael Espindola | ffd902b | 2010-12-06 02:57:26 +0000 | [diff] [blame] | 133 | assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!"); |
| 134 | return getFragmentOffset(SD->getFragment()) + SD->getOffset(); |
| 135 | } |
| 136 | |
Daniel Dunbar | 2661f11 | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 137 | uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const { |
Daniel Dunbar | afc6acd | 2010-05-14 00:37:11 +0000 | [diff] [blame] | 138 | // The size is the last fragment's end offset. |
Daniel Dunbar | 2661f11 | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 139 | const MCFragment &F = SD->getFragmentList().back(); |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 140 | return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F); |
Daniel Dunbar | 5d42851 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const { |
Daniel Dunbar | 2661f11 | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 144 | // Virtual sections have no file size. |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 145 | if (SD->getSection().isVirtualSection()) |
Daniel Dunbar | 2661f11 | 2010-05-13 03:19:50 +0000 | [diff] [blame] | 146 | return 0; |
| 147 | |
| 148 | // Otherwise, the file size is the same as the address space size. |
| 149 | return getSectionAddressSize(SD); |
Daniel Dunbar | 5d42851 | 2010-03-25 02:00:07 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Daniel Dunbar | 207e06e | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 152 | /* *** */ |
| 153 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 154 | MCFragment::MCFragment() : Kind(FragmentType(~0)) { |
| 155 | } |
| 156 | |
Daniel Dunbar | 36880e7 | 2010-07-28 20:28:45 +0000 | [diff] [blame] | 157 | MCFragment::~MCFragment() { |
| 158 | } |
| 159 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 160 | MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent) |
Rafael Espindola | 2bf6afc | 2010-12-15 08:45:53 +0000 | [diff] [blame] | 161 | : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 162 | { |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 163 | if (Parent) |
| 164 | Parent->getFragmentList().push_back(this); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* *** */ |
| 168 | |
Daniel Dunbar | 81e4000 | 2009-08-27 00:38:04 +0000 | [diff] [blame] | 169 | MCSectionData::MCSectionData() : Section(0) {} |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 170 | |
| 171 | MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A) |
Daniel Dunbar | 81e4000 | 2009-08-27 00:38:04 +0000 | [diff] [blame] | 172 | : Section(&_Section), |
Rafael Espindola | f8803fe | 2010-12-06 03:48:09 +0000 | [diff] [blame] | 173 | Ordinal(~UINT32_C(0)), |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 174 | Alignment(1), |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 175 | HasInstructions(false) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 176 | { |
| 177 | if (A) |
| 178 | A->getSectionList().push_back(this); |
| 179 | } |
| 180 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 181 | /* *** */ |
| 182 | |
Daniel Dunbar | efbb533 | 2009-09-01 04:09:03 +0000 | [diff] [blame] | 183 | MCSymbolData::MCSymbolData() : Symbol(0) {} |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 184 | |
Daniel Dunbar | cb579b3 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 185 | MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 186 | uint64_t _Offset, MCAssembler *A) |
Daniel Dunbar | efbb533 | 2009-09-01 04:09:03 +0000 | [diff] [blame] | 187 | : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset), |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 188 | IsExternal(false), IsPrivateExtern(false), |
Matt Fleming | 6c8b3d2 | 2010-08-16 18:34:31 +0000 | [diff] [blame] | 189 | CommonSize(0), SymbolSize(0), CommonAlign(0), |
| 190 | Flags(0), Index(0) |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 191 | { |
| 192 | if (A) |
| 193 | A->getSymbolList().push_back(this); |
| 194 | } |
| 195 | |
| 196 | /* *** */ |
| 197 | |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 198 | MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_, |
Daniel Dunbar | feb7ba3 | 2010-12-17 02:45:41 +0000 | [diff] [blame] | 199 | MCCodeEmitter &Emitter_, MCObjectWriter &Writer_, |
| 200 | raw_ostream &OS_) |
| 201 | : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_), |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 202 | OS(OS_), RelaxAll(false), NoExecStack(false), SubsectionsViaSymbols(false) |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 203 | { |
| 204 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 205 | |
| 206 | MCAssembler::~MCAssembler() { |
| 207 | } |
| 208 | |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 209 | bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const { |
Daniel Dunbar | 2386985 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 210 | // Non-temporary labels should always be visible to the linker. |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 211 | if (!Symbol.isTemporary()) |
Daniel Dunbar | 2386985 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 212 | return true; |
| 213 | |
| 214 | // Absolute temporary labels are never visible. |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 215 | if (!Symbol.isInSection()) |
Daniel Dunbar | 2386985 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 216 | return false; |
| 217 | |
| 218 | // Otherwise, check if the section requires symbols even for temporary labels. |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 219 | return getBackend().doesSectionRequireSymbols(Symbol.getSection()); |
Daniel Dunbar | 2386985 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Rafael Espindola | b814110 | 2010-09-27 18:13:03 +0000 | [diff] [blame] | 222 | const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const { |
Daniel Dunbar | 8ad0dcc | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 223 | // Linker visible symbols define atoms. |
Daniel Dunbar | 843aa1f | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 224 | if (isSymbolLinkerVisible(SD->getSymbol())) |
Daniel Dunbar | 8ad0dcc | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 225 | return SD; |
| 226 | |
| 227 | // Absolute and undefined symbols have no defining atom. |
| 228 | if (!SD->getFragment()) |
| 229 | return 0; |
| 230 | |
Daniel Dunbar | a5f1d57 | 2010-05-12 00:38:17 +0000 | [diff] [blame] | 231 | // Non-linker visible symbols in sections which can't be atomized have no |
| 232 | // defining atom. |
| 233 | if (!getBackend().isSectionAtomizable( |
| 234 | SD->getFragment()->getParent()->getSection())) |
| 235 | return 0; |
| 236 | |
Daniel Dunbar | 651804c | 2010-05-11 17:22:50 +0000 | [diff] [blame] | 237 | // Otherwise, return the atom for the containing fragment. |
| 238 | return SD->getFragment()->getAtom(); |
Daniel Dunbar | 8ad0dcc | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 241 | bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout, |
Daniel Dunbar | c90e30a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 242 | const MCFixup &Fixup, const MCFragment *DF, |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 243 | MCValue &Target, uint64_t &Value) const { |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 244 | ++stats::evaluateFixup; |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 245 | |
Rafael Espindola | 33a38a1 | 2010-12-22 16:11:57 +0000 | [diff] [blame] | 246 | if (!Fixup.getValue()->EvaluateAsRelocatable(Target, Layout)) |
Jim Grosbach | f3c9367 | 2012-01-27 00:51:23 +0000 | [diff] [blame] | 247 | getContext().FatalError(Fixup.getLoc(), "expected relocatable expression"); |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 248 | |
Rafael Espindola | fea753b | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 249 | bool IsPCRel = Backend.getFixupKindInfo( |
| 250 | Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel; |
| 251 | |
| 252 | bool IsResolved; |
| 253 | if (IsPCRel) { |
| 254 | if (Target.getSymB()) { |
| 255 | IsResolved = false; |
| 256 | } else if (!Target.getSymA()) { |
| 257 | IsResolved = false; |
| 258 | } else { |
Rafael Espindola | 908159b | 2011-02-16 03:25:55 +0000 | [diff] [blame] | 259 | const MCSymbolRefExpr *A = Target.getSymA(); |
| 260 | const MCSymbol &SA = A->getSymbol(); |
| 261 | if (A->getKind() != MCSymbolRefExpr::VK_None || |
| 262 | SA.AliasedSymbol().isUndefined()) { |
Rafael Espindola | fea753b | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 263 | IsResolved = false; |
| 264 | } else { |
| 265 | const MCSymbolData &DataA = getSymbolData(SA); |
| 266 | IsResolved = |
| 267 | getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA, |
| 268 | *DF, false, true); |
| 269 | } |
| 270 | } |
| 271 | } else { |
| 272 | IsResolved = Target.isAbsolute(); |
| 273 | } |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 274 | |
| 275 | Value = Target.getConstant(); |
| 276 | |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 277 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 278 | const MCSymbol &Sym = A->getSymbol().AliasedSymbol(); |
| 279 | if (Sym.isDefined()) |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 280 | Value += Layout.getSymbolOffset(&getSymbolData(Sym)); |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 281 | } |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 282 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 283 | const MCSymbol &Sym = B->getSymbol().AliasedSymbol(); |
| 284 | if (Sym.isDefined()) |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 285 | Value -= Layout.getSymbolOffset(&getSymbolData(Sym)); |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 286 | } |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 287 | |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 288 | |
Daniel Dunbar | 2761fc4 | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 289 | bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags & |
Owen Anderson | 47dbd42 | 2010-12-15 18:48:27 +0000 | [diff] [blame] | 290 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits; |
| 291 | assert((ShouldAlignPC ? IsPCRel : true) && |
| 292 | "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!"); |
| 293 | |
Owen Anderson | 05018c2 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 294 | if (IsPCRel) { |
Owen Anderson | 175fb36 | 2010-12-17 21:49:48 +0000 | [diff] [blame] | 295 | uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset(); |
Jim Grosbach | 684457d | 2011-10-26 22:44:41 +0000 | [diff] [blame] | 296 | |
Owen Anderson | 47dbd42 | 2010-12-15 18:48:27 +0000 | [diff] [blame] | 297 | // A number of ARM fixups in Thumb mode require that the effective PC |
| 298 | // address be determined as the 32-bit aligned version of the actual offset. |
Owen Anderson | d18e011 | 2010-12-15 19:24:24 +0000 | [diff] [blame] | 299 | if (ShouldAlignPC) Offset &= ~0x3; |
Owen Anderson | 175fb36 | 2010-12-17 21:49:48 +0000 | [diff] [blame] | 300 | Value -= Offset; |
Owen Anderson | 05018c2 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 301 | } |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 302 | |
Jim Grosbach | 7b25ecf | 2012-02-27 21:36:23 +0000 | [diff] [blame] | 303 | // Let the backend adjust the fixup value if necessary, including whether |
| 304 | // we need a relocation. |
| 305 | Backend.processFixupValue(*this, Layout, Fixup, DF, Target, Value, |
| 306 | IsResolved); |
Jim Grosbach | 4750020 | 2010-12-14 18:46:57 +0000 | [diff] [blame] | 307 | |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 308 | return IsResolved; |
| 309 | } |
| 310 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 311 | uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout, |
Rafael Espindola | 7a45903 | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 312 | const MCFragment &F) const { |
Daniel Dunbar | 2c18d3b | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 313 | switch (F.getKind()) { |
| 314 | case MCFragment::FT_Data: |
| 315 | return cast<MCDataFragment>(F).getContents().size(); |
| 316 | case MCFragment::FT_Fill: |
| 317 | return cast<MCFillFragment>(F).getSize(); |
| 318 | case MCFragment::FT_Inst: |
| 319 | return cast<MCInstFragment>(F).getInstSize(); |
| 320 | |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 321 | case MCFragment::FT_LEB: |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 322 | return cast<MCLEBFragment>(F).getContents().size(); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 323 | |
Rafael Espindola | 7a45903 | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 324 | case MCFragment::FT_Align: { |
| 325 | const MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 326 | unsigned Offset = Layout.getFragmentOffset(&AF); |
| 327 | unsigned Size = OffsetToAlignment(Offset, AF.getAlignment()); |
| 328 | if (Size > AF.getMaxBytesToEmit()) |
| 329 | return 0; |
| 330 | return Size; |
| 331 | } |
Daniel Dunbar | 2c18d3b | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 332 | |
Rafael Espindola | 7a45903 | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 333 | case MCFragment::FT_Org: { |
| 334 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 335 | int64_t TargetLocation; |
| 336 | if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout)) |
| 337 | report_fatal_error("expected assembly-time absolute expression"); |
| 338 | |
| 339 | // FIXME: We need a way to communicate this error. |
| 340 | uint64_t FragmentOffset = Layout.getFragmentOffset(&OF); |
| 341 | int64_t Size = TargetLocation - FragmentOffset; |
| 342 | if (Size < 0 || Size >= 0x40000000) |
| 343 | report_fatal_error("invalid .org offset '" + Twine(TargetLocation) + |
| 344 | "' (at offset '" + Twine(FragmentOffset) + "')"); |
| 345 | return Size; |
| 346 | } |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 347 | |
Rafael Espindola | 187d833 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 348 | case MCFragment::FT_Dwarf: |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 349 | return cast<MCDwarfLineAddrFragment>(F).getContents().size(); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 350 | case MCFragment::FT_DwarfFrame: |
| 351 | return cast<MCDwarfCallFrameFragment>(F).getContents().size(); |
Daniel Dunbar | 2c18d3b | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 354 | llvm_unreachable("invalid fragment kind"); |
Daniel Dunbar | 2c18d3b | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Daniel Dunbar | b69fc04 | 2010-05-13 20:40:12 +0000 | [diff] [blame] | 357 | void MCAsmLayout::LayoutFragment(MCFragment *F) { |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 358 | MCFragment *Prev = F->getPrevNode(); |
Daniel Dunbar | f0d17d2 | 2010-05-12 21:35:25 +0000 | [diff] [blame] | 359 | |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 360 | // We should never try to recompute something which is up-to-date. |
| 361 | assert(!isFragmentUpToDate(F) && "Attempt to recompute up-to-date fragment!"); |
Daniel Dunbar | 9005d45 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 362 | // We should never try to compute the fragment layout if it's predecessor |
| 363 | // isn't up-to-date. |
| 364 | assert((!Prev || isFragmentUpToDate(Prev)) && |
| 365 | "Attempt to compute fragment before it's predecessor!"); |
Daniel Dunbar | f0d17d2 | 2010-05-12 21:35:25 +0000 | [diff] [blame] | 366 | |
| 367 | ++stats::FragmentLayouts; |
| 368 | |
Daniel Dunbar | b69fc04 | 2010-05-13 20:40:12 +0000 | [diff] [blame] | 369 | // Compute fragment offset and size. |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 370 | uint64_t Offset = 0; |
| 371 | if (Prev) |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 372 | Offset += Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev); |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 373 | |
| 374 | F->Offset = Offset; |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 375 | LastValidFragment[F->getParent()] = F; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 378 | /// WriteFragmentData - Write the \arg F data to the output file. |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 379 | static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout, |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 380 | const MCFragment &F) { |
| 381 | MCObjectWriter *OW = &Asm.getWriter(); |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 382 | uint64_t Start = OW->getStream().tell(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 383 | (void) Start; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 384 | |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 385 | ++stats::EmittedFragments; |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 386 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 387 | // FIXME: Embed in fragments instead? |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 388 | uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 389 | switch (F.getKind()) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 390 | case MCFragment::FT_Align: { |
| 391 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 392 | uint64_t Count = FragmentSize / AF.getValueSize(); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 393 | |
Daniel Dunbar | e73d49e | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 394 | assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!"); |
| 395 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 396 | // FIXME: This error shouldn't actually occur (the front end should emit |
| 397 | // multiple .align directives to enforce the semantics it wants), but is |
| 398 | // severe enough that we want to report it. How to handle this? |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 399 | if (Count * AF.getValueSize() != FragmentSize) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 400 | report_fatal_error("undefined .align directive, value size '" + |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 401 | Twine(AF.getValueSize()) + |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 402 | "' is not a divisor of padding size '" + |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 403 | Twine(FragmentSize) + "'"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 404 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 405 | // See if we are aligning with nops, and if so do that first to try to fill |
| 406 | // the Count bytes. Then if that did not fill any bytes or there are any |
Sylvestre Ledru | c8e41c5 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 407 | // bytes left to fill use the Value and ValueSize to fill the rest. |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 408 | // If we are aligning with nops, ask that target to emit the right data. |
Daniel Dunbar | 1c15413 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 409 | if (AF.hasEmitNops()) { |
Jim Grosbach | ec34338 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 410 | if (!Asm.getBackend().writeNopData(Count, OW)) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 411 | report_fatal_error("unable to write nop sequence of " + |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 412 | Twine(Count) + " bytes"); |
| 413 | break; |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Daniel Dunbar | 8f9b80e | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 416 | // Otherwise, write out in multiples of the value size. |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 417 | for (uint64_t i = 0; i != Count; ++i) { |
| 418 | switch (AF.getValueSize()) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 419 | default: llvm_unreachable("Invalid size!"); |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 420 | case 1: OW->Write8 (uint8_t (AF.getValue())); break; |
| 421 | case 2: OW->Write16(uint16_t(AF.getValue())); break; |
| 422 | case 4: OW->Write32(uint32_t(AF.getValue())); break; |
| 423 | case 8: OW->Write64(uint64_t(AF.getValue())); break; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | break; |
| 427 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 428 | |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 429 | case MCFragment::FT_Data: { |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 430 | MCDataFragment &DF = cast<MCDataFragment>(F); |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 431 | assert(FragmentSize == DF.getContents().size() && "Invalid size!"); |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 432 | OW->WriteBytes(DF.getContents().str()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 433 | break; |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 434 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 435 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 436 | case MCFragment::FT_Fill: { |
| 437 | MCFillFragment &FF = cast<MCFillFragment>(F); |
Daniel Dunbar | e2fee5b | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 438 | |
| 439 | assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!"); |
| 440 | |
Daniel Dunbar | 3153fec | 2010-05-12 22:51:32 +0000 | [diff] [blame] | 441 | for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 442 | switch (FF.getValueSize()) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 443 | default: llvm_unreachable("Invalid size!"); |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 444 | case 1: OW->Write8 (uint8_t (FF.getValue())); break; |
| 445 | case 2: OW->Write16(uint16_t(FF.getValue())); break; |
| 446 | case 4: OW->Write32(uint32_t(FF.getValue())); break; |
| 447 | case 8: OW->Write64(uint64_t(FF.getValue())); break; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | break; |
| 451 | } |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 452 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 453 | case MCFragment::FT_Inst: { |
| 454 | MCInstFragment &IF = cast<MCInstFragment>(F); |
| 455 | OW->WriteBytes(StringRef(IF.getCode().begin(), IF.getCode().size())); |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 456 | break; |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 457 | } |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 458 | |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 459 | case MCFragment::FT_LEB: { |
| 460 | MCLEBFragment &LF = cast<MCLEBFragment>(F); |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 461 | OW->WriteBytes(LF.getContents().str()); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 462 | break; |
| 463 | } |
| 464 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 465 | case MCFragment::FT_Org: { |
| 466 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 467 | |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 468 | for (uint64_t i = 0, e = FragmentSize; i != e; ++i) |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 469 | OW->Write8(uint8_t(OF.getValue())); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 470 | |
| 471 | break; |
| 472 | } |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 473 | |
| 474 | case MCFragment::FT_Dwarf: { |
| 475 | const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F); |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 476 | OW->WriteBytes(OF.getContents().str()); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 477 | break; |
| 478 | } |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 479 | case MCFragment::FT_DwarfFrame: { |
| 480 | const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F); |
| 481 | OW->WriteBytes(CF.getContents().str()); |
| 482 | break; |
| 483 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Daniel Dunbar | 432cd5f | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 486 | assert(OW->getStream().tell() - Start == FragmentSize); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 489 | void MCAssembler::writeSectionData(const MCSectionData *SD, |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 490 | const MCAsmLayout &Layout) const { |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 491 | // Ignore virtual sections. |
Rafael Espindola | f2dc4aa | 2010-11-17 20:03:54 +0000 | [diff] [blame] | 492 | if (SD->getSection().isVirtualSection()) { |
Daniel Dunbar | 054be92 | 2010-05-13 03:50:50 +0000 | [diff] [blame] | 493 | assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!"); |
Daniel Dunbar | e2fee5b | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 494 | |
| 495 | // Check that contents are only things legal inside a virtual section. |
| 496 | for (MCSectionData::const_iterator it = SD->begin(), |
| 497 | ie = SD->end(); it != ie; ++it) { |
| 498 | switch (it->getKind()) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 499 | default: llvm_unreachable("Invalid fragment in virtual section!"); |
Daniel Dunbar | c983b20 | 2010-08-18 18:22:37 +0000 | [diff] [blame] | 500 | case MCFragment::FT_Data: { |
| 501 | // Check that we aren't trying to write a non-zero contents (or fixups) |
| 502 | // into a virtual section. This is to support clients which use standard |
| 503 | // directives to fill the contents of virtual sections. |
| 504 | MCDataFragment &DF = cast<MCDataFragment>(*it); |
| 505 | assert(DF.fixup_begin() == DF.fixup_end() && |
| 506 | "Cannot have fixups in virtual section!"); |
| 507 | for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i) |
| 508 | assert(DF.getContents()[i] == 0 && |
| 509 | "Invalid data value for virtual section!"); |
| 510 | break; |
| 511 | } |
Daniel Dunbar | e2fee5b | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 512 | case MCFragment::FT_Align: |
Daniel Dunbar | c983b20 | 2010-08-18 18:22:37 +0000 | [diff] [blame] | 513 | // Check that we aren't trying to write a non-zero value into a virtual |
| 514 | // section. |
| 515 | assert((!cast<MCAlignFragment>(it)->getValueSize() || |
| 516 | !cast<MCAlignFragment>(it)->getValue()) && |
Daniel Dunbar | e2fee5b | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 517 | "Invalid align in virtual section!"); |
| 518 | break; |
| 519 | case MCFragment::FT_Fill: |
| 520 | assert(!cast<MCFillFragment>(it)->getValueSize() && |
| 521 | "Invalid fill in virtual section!"); |
| 522 | break; |
Daniel Dunbar | e2fee5b | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 529 | uint64_t Start = getWriter().getStream().tell(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 530 | (void) Start; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 531 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 532 | for (MCSectionData::const_iterator it = SD->begin(), |
| 533 | ie = SD->end(); it != ie; ++it) |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 534 | WriteFragmentData(*this, Layout, *it); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 535 | |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 536 | assert(getWriter().getStream().tell() - Start == |
| 537 | Layout.getSectionAddressSize(SD)); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 540 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 541 | uint64_t MCAssembler::handleFixup(const MCAsmLayout &Layout, |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 542 | MCFragment &F, |
| 543 | const MCFixup &Fixup) { |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 544 | // Evaluate the fixup. |
| 545 | MCValue Target; |
| 546 | uint64_t FixedValue; |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 547 | if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) { |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 548 | // The fixup was unresolved, we need a relocation. Inform the object |
| 549 | // writer of the relocation, and give it an opportunity to adjust the |
| 550 | // fixup value if need be. |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 551 | getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue); |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 552 | } |
| 553 | return FixedValue; |
| 554 | } |
| 555 | |
Daniel Dunbar | feb7ba3 | 2010-12-17 02:45:41 +0000 | [diff] [blame] | 556 | void MCAssembler::Finish() { |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 557 | DEBUG_WITH_TYPE("mc-dump", { |
| 558 | llvm::errs() << "assembler backend - pre-layout\n--\n"; |
| 559 | dump(); }); |
| 560 | |
Daniel Dunbar | 61066db | 2010-05-13 02:34:14 +0000 | [diff] [blame] | 561 | // Create the layout object. |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 562 | MCAsmLayout Layout(*this); |
Daniel Dunbar | 61066db | 2010-05-13 02:34:14 +0000 | [diff] [blame] | 563 | |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 564 | // Create dummy fragments and assign section ordinals. |
Daniel Dunbar | 49ed921 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 565 | unsigned SectionIndex = 0; |
Daniel Dunbar | 49ed921 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 566 | for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) { |
| 567 | // Create dummy fragments to eliminate any empty sections, this simplifies |
| 568 | // layout. |
Duncan Sands | 6f74f69 | 2010-06-29 13:30:08 +0000 | [diff] [blame] | 569 | if (it->getFragmentList().empty()) |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 570 | new MCDataFragment(it); |
Daniel Dunbar | 49ed921 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 571 | |
| 572 | it->setOrdinal(SectionIndex++); |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 573 | } |
Daniel Dunbar | 49ed921 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 574 | |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 575 | // Assign layout order indices to sections and fragments. |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 576 | for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) { |
| 577 | MCSectionData *SD = Layout.getSectionOrder()[i]; |
| 578 | SD->setLayoutOrder(i); |
| 579 | |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 580 | unsigned FragmentIndex = 0; |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 581 | for (MCSectionData::iterator it2 = SD->begin(), |
| 582 | ie2 = SD->end(); it2 != ie2; ++it2) |
| 583 | it2->setLayoutOrder(FragmentIndex++); |
Daniel Dunbar | 49ed921 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Daniel Dunbar | 61066db | 2010-05-13 02:34:14 +0000 | [diff] [blame] | 586 | // Layout until everything fits. |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 587 | while (layoutOnce(Layout)) |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 588 | continue; |
| 589 | |
| 590 | DEBUG_WITH_TYPE("mc-dump", { |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 591 | llvm::errs() << "assembler backend - post-relaxation\n--\n"; |
| 592 | dump(); }); |
| 593 | |
| 594 | // Finalize the layout, including fragment lowering. |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 595 | finishLayout(Layout); |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 596 | |
| 597 | DEBUG_WITH_TYPE("mc-dump", { |
| 598 | llvm::errs() << "assembler backend - final-layout\n--\n"; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 599 | dump(); }); |
| 600 | |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 601 | uint64_t StartOffset = OS.tell(); |
Reid Kleckner | c96a82a | 2010-07-22 05:58:53 +0000 | [diff] [blame] | 602 | |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 603 | // Allow the object writer a chance to perform post-layout binding (for |
| 604 | // example, to set the index fields in the symbol data). |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 605 | getWriter().ExecutePostLayoutBinding(*this, Layout); |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 606 | |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 607 | // Evaluate and apply the fixups, generating relocation entries as necessary. |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 608 | for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) { |
| 609 | for (MCSectionData::iterator it2 = it->begin(), |
| 610 | ie2 = it->end(); it2 != ie2; ++it2) { |
| 611 | MCDataFragment *DF = dyn_cast<MCDataFragment>(it2); |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 612 | if (DF) { |
| 613 | for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(), |
| 614 | ie3 = DF->fixup_end(); it3 != ie3; ++it3) { |
| 615 | MCFixup &Fixup = *it3; |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 616 | uint64_t FixedValue = handleFixup(Layout, *DF, Fixup); |
Jim Grosbach | ec34338 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 617 | getBackend().applyFixup(Fixup, DF->getContents().data(), |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 618 | DF->getContents().size(), FixedValue); |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 619 | } |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 620 | } |
| 621 | MCInstFragment *IF = dyn_cast<MCInstFragment>(it2); |
| 622 | if (IF) { |
| 623 | for (MCInstFragment::fixup_iterator it3 = IF->fixup_begin(), |
| 624 | ie3 = IF->fixup_end(); it3 != ie3; ++it3) { |
| 625 | MCFixup &Fixup = *it3; |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 626 | uint64_t FixedValue = handleFixup(Layout, *IF, Fixup); |
Jim Grosbach | ec34338 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 627 | getBackend().applyFixup(Fixup, IF->getCode().data(), |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 628 | IF->getCode().size(), FixedValue); |
| 629 | } |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 634 | // Write the object file. |
Daniel Dunbar | 5d2477c | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 635 | getWriter().WriteObject(*this, Layout); |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 636 | |
| 637 | stats::ObjectBytes += OS.tell() - StartOffset; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 640 | bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup, |
Jim Grosbach | 370b78d | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 641 | const MCInstFragment *DF, |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 642 | const MCAsmLayout &Layout) const { |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 643 | if (getRelaxAll()) |
| 644 | return true; |
| 645 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 646 | // If we cannot resolve the fixup value, it requires relaxation. |
| 647 | MCValue Target; |
| 648 | uint64_t Value; |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 649 | if (!evaluateFixup(Layout, Fixup, DF, Target, Value)) |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 650 | return true; |
| 651 | |
Jim Grosbach | 370b78d | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 652 | return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout); |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 655 | bool MCAssembler::fragmentNeedsRelaxation(const MCInstFragment *IF, |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 656 | const MCAsmLayout &Layout) const { |
| 657 | // If this inst doesn't ever need relaxation, ignore it. This occurs when we |
| 658 | // are intentionally pushing out inst fragments, or because we relaxed a |
| 659 | // previous instruction to one that doesn't need relaxation. |
Jim Grosbach | ec34338 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 660 | if (!getBackend().mayNeedRelaxation(IF->getInst())) |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 661 | return false; |
| 662 | |
| 663 | for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(), |
| 664 | ie = IF->fixup_end(); it != ie; ++it) |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 665 | if (fixupNeedsRelaxation(*it, IF, Layout)) |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 666 | return true; |
| 667 | |
| 668 | return false; |
| 669 | } |
| 670 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 671 | bool MCAssembler::relaxInstruction(MCAsmLayout &Layout, |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 672 | MCInstFragment &IF) { |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 673 | if (!fragmentNeedsRelaxation(&IF, Layout)) |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 674 | return false; |
| 675 | |
| 676 | ++stats::RelaxedInstructions; |
| 677 | |
| 678 | // FIXME-PERF: We could immediately lower out instructions if we can tell |
| 679 | // they are fully resolved, to avoid retesting on later passes. |
| 680 | |
| 681 | // Relax the fragment. |
| 682 | |
| 683 | MCInst Relaxed; |
Jim Grosbach | ec34338 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 684 | getBackend().relaxInstruction(IF.getInst(), Relaxed); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 685 | |
| 686 | // Encode the new instruction. |
| 687 | // |
| 688 | // FIXME-PERF: If it matters, we could let the target do this. It can |
| 689 | // probably do so more efficiently in many cases. |
| 690 | SmallVector<MCFixup, 4> Fixups; |
| 691 | SmallString<256> Code; |
| 692 | raw_svector_ostream VecOS(Code); |
| 693 | getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups); |
| 694 | VecOS.flush(); |
| 695 | |
| 696 | // Update the instruction fragment. |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 697 | IF.setInst(Relaxed); |
| 698 | IF.getCode() = Code; |
| 699 | IF.getFixups().clear(); |
| 700 | // FIXME: Eliminate copy. |
| 701 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) |
| 702 | IF.getFixups().push_back(Fixups[i]); |
| 703 | |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 704 | return true; |
| 705 | } |
| 706 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 707 | bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) { |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 708 | int64_t Value = 0; |
| 709 | uint64_t OldSize = LF.getContents().size(); |
Rafael Espindola | d88cac0 | 2011-04-26 02:17:58 +0000 | [diff] [blame] | 710 | bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout); |
| 711 | (void)IsAbs; |
| 712 | assert(IsAbs); |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 713 | SmallString<8> &Data = LF.getContents(); |
| 714 | Data.clear(); |
| 715 | raw_svector_ostream OSE(Data); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 716 | if (LF.isSigned()) |
Jim Grosbach | 2d39a0e | 2012-08-08 23:56:06 +0000 | [diff] [blame^] | 717 | encodeSLEB128(Value, OSE); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 718 | else |
Jim Grosbach | 2d39a0e | 2012-08-08 23:56:06 +0000 | [diff] [blame^] | 719 | encodeULEB128(Value, OSE); |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 720 | OSE.flush(); |
| 721 | return OldSize != LF.getContents().size(); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 724 | bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout, |
Jim Grosbach | f68a26b | 2011-12-06 00:13:09 +0000 | [diff] [blame] | 725 | MCDwarfLineAddrFragment &DF) { |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 726 | int64_t AddrDelta = 0; |
| 727 | uint64_t OldSize = DF.getContents().size(); |
Rafael Espindola | 835439a | 2010-12-22 22:04:28 +0000 | [diff] [blame] | 728 | bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout); |
| 729 | (void)IsAbs; |
| 730 | assert(IsAbs); |
Rafael Espindola | 187d833 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 731 | int64_t LineDelta; |
| 732 | LineDelta = DF.getLineDelta(); |
Rafael Espindola | db74aea | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 733 | SmallString<8> &Data = DF.getContents(); |
| 734 | Data.clear(); |
| 735 | raw_svector_ostream OSE(Data); |
| 736 | MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OSE); |
| 737 | OSE.flush(); |
| 738 | return OldSize != Data.size(); |
Rafael Espindola | 187d833 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 741 | bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout, |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 742 | MCDwarfCallFrameFragment &DF) { |
| 743 | int64_t AddrDelta = 0; |
| 744 | uint64_t OldSize = DF.getContents().size(); |
| 745 | bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout); |
| 746 | (void)IsAbs; |
| 747 | assert(IsAbs); |
| 748 | SmallString<8> &Data = DF.getContents(); |
| 749 | Data.clear(); |
| 750 | raw_svector_ostream OSE(Data); |
Rafael Espindola | 4eafe10 | 2011-05-08 14:35:21 +0000 | [diff] [blame] | 751 | MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OSE); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 752 | OSE.flush(); |
| 753 | return OldSize != Data.size(); |
| 754 | } |
| 755 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 756 | bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 757 | MCSectionData &SD) { |
| 758 | MCFragment *FirstInvalidFragment = NULL; |
| 759 | // Scan for fragments that need relaxation. |
| 760 | for (MCSectionData::iterator it2 = SD.begin(), |
| 761 | ie2 = SD.end(); it2 != ie2; ++it2) { |
| 762 | // Check if this is an fragment that needs relaxation. |
| 763 | bool relaxedFrag = false; |
| 764 | switch(it2->getKind()) { |
| 765 | default: |
| 766 | break; |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 767 | case MCFragment::FT_Inst: |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 768 | relaxedFrag = relaxInstruction(Layout, *cast<MCInstFragment>(it2)); |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 769 | break; |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 770 | case MCFragment::FT_Dwarf: |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 771 | relaxedFrag = relaxDwarfLineAddr(Layout, |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 772 | *cast<MCDwarfLineAddrFragment>(it2)); |
| 773 | break; |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 774 | case MCFragment::FT_DwarfFrame: |
| 775 | relaxedFrag = |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 776 | relaxDwarfCallFrameFragment(Layout, |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 777 | *cast<MCDwarfCallFrameFragment>(it2)); |
| 778 | break; |
| 779 | case MCFragment::FT_LEB: |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 780 | relaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(it2)); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 781 | break; |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 782 | } |
| 783 | // Update the layout, and remember that we relaxed. |
| 784 | if (relaxedFrag && !FirstInvalidFragment) |
| 785 | FirstInvalidFragment = it2; |
| 786 | } |
| 787 | if (FirstInvalidFragment) { |
| 788 | Layout.Invalidate(FirstInvalidFragment); |
| 789 | return true; |
| 790 | } |
| 791 | return false; |
| 792 | } |
| 793 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 794 | bool MCAssembler::layoutOnce(MCAsmLayout &Layout) { |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 795 | ++stats::RelaxationSteps; |
| 796 | |
Daniel Dunbar | 0cc8bd4 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 797 | bool WasRelaxed = false; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 798 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 799 | MCSectionData &SD = *it; |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 800 | while(layoutSectionOnce(Layout, SD)) |
Rafael Espindola | 62b83b6 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 801 | WasRelaxed = true; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Daniel Dunbar | 0cc8bd4 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 804 | return WasRelaxed; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 805 | } |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 806 | |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 807 | void MCAssembler::finishLayout(MCAsmLayout &Layout) { |
Rafael Espindola | b4172fa | 2010-12-04 22:47:22 +0000 | [diff] [blame] | 808 | // The layout is done. Mark every fragment as valid. |
Rafael Espindola | 4f4363a | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 809 | for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) { |
| 810 | Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin()); |
| 811 | } |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 814 | // Debugging methods |
| 815 | |
| 816 | namespace llvm { |
| 817 | |
Daniel Dunbar | c90e30a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 818 | raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) { |
| 819 | OS << "<MCFixup" << " Offset:" << AF.getOffset() |
Daniel Dunbar | 482ad80 | 2010-05-26 15:18:31 +0000 | [diff] [blame] | 820 | << " Value:" << *AF.getValue() |
| 821 | << " Kind:" << AF.getKind() << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 822 | return OS; |
| 823 | } |
| 824 | |
| 825 | } |
| 826 | |
| 827 | void MCFragment::dump() { |
| 828 | raw_ostream &OS = llvm::errs(); |
| 829 | |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 830 | OS << "<"; |
| 831 | switch (getKind()) { |
| 832 | case MCFragment::FT_Align: OS << "MCAlignFragment"; break; |
| 833 | case MCFragment::FT_Data: OS << "MCDataFragment"; break; |
| 834 | case MCFragment::FT_Fill: OS << "MCFillFragment"; break; |
| 835 | case MCFragment::FT_Inst: OS << "MCInstFragment"; break; |
| 836 | case MCFragment::FT_Org: OS << "MCOrgFragment"; break; |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 837 | case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break; |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 838 | case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break; |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 839 | case MCFragment::FT_LEB: OS << "MCLEBFragment"; break; |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Daniel Dunbar | 337718e | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 842 | OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder |
Rafael Espindola | 2bf6afc | 2010-12-15 08:45:53 +0000 | [diff] [blame] | 843 | << " Offset:" << Offset << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 844 | |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 845 | switch (getKind()) { |
| 846 | case MCFragment::FT_Align: { |
| 847 | const MCAlignFragment *AF = cast<MCAlignFragment>(this); |
| 848 | if (AF->hasEmitNops()) |
| 849 | OS << " (emit nops)"; |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 850 | OS << "\n "; |
| 851 | OS << " Alignment:" << AF->getAlignment() |
| 852 | << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize() |
| 853 | << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">"; |
| 854 | break; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 855 | } |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 856 | case MCFragment::FT_Data: { |
| 857 | const MCDataFragment *DF = cast<MCDataFragment>(this); |
| 858 | OS << "\n "; |
| 859 | OS << " Contents:["; |
| 860 | const SmallVectorImpl<char> &Contents = DF->getContents(); |
| 861 | for (unsigned i = 0, e = Contents.size(); i != e; ++i) { |
| 862 | if (i) OS << ","; |
| 863 | OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 864 | } |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 865 | OS << "] (" << Contents.size() << " bytes)"; |
| 866 | |
| 867 | if (!DF->getFixups().empty()) { |
| 868 | OS << ",\n "; |
| 869 | OS << " Fixups:["; |
| 870 | for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(), |
| 871 | ie = DF->fixup_end(); it != ie; ++it) { |
| 872 | if (it != DF->fixup_begin()) OS << ",\n "; |
| 873 | OS << *it; |
| 874 | } |
| 875 | OS << "]"; |
| 876 | } |
| 877 | break; |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 878 | } |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 879 | case MCFragment::FT_Fill: { |
| 880 | const MCFillFragment *FF = cast<MCFillFragment>(this); |
| 881 | OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize() |
| 882 | << " Size:" << FF->getSize(); |
| 883 | break; |
| 884 | } |
| 885 | case MCFragment::FT_Inst: { |
| 886 | const MCInstFragment *IF = cast<MCInstFragment>(this); |
| 887 | OS << "\n "; |
| 888 | OS << " Inst:"; |
| 889 | IF->getInst().dump_pretty(OS); |
| 890 | break; |
| 891 | } |
| 892 | case MCFragment::FT_Org: { |
| 893 | const MCOrgFragment *OF = cast<MCOrgFragment>(this); |
| 894 | OS << "\n "; |
| 895 | OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue(); |
| 896 | break; |
| 897 | } |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 898 | case MCFragment::FT_Dwarf: { |
| 899 | const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this); |
| 900 | OS << "\n "; |
| 901 | OS << " AddrDelta:" << OF->getAddrDelta() |
| 902 | << " LineDelta:" << OF->getLineDelta(); |
| 903 | break; |
| 904 | } |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 905 | case MCFragment::FT_DwarfFrame: { |
| 906 | const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this); |
| 907 | OS << "\n "; |
| 908 | OS << " AddrDelta:" << CF->getAddrDelta(); |
| 909 | break; |
| 910 | } |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 911 | case MCFragment::FT_LEB: { |
| 912 | const MCLEBFragment *LF = cast<MCLEBFragment>(this); |
| 913 | OS << "\n "; |
| 914 | OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned(); |
| 915 | break; |
| 916 | } |
Daniel Dunbar | e614e39 | 2010-05-26 06:50:57 +0000 | [diff] [blame] | 917 | } |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 918 | OS << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 921 | void MCSectionData::dump() { |
| 922 | raw_ostream &OS = llvm::errs(); |
| 923 | |
| 924 | OS << "<MCSectionData"; |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 925 | OS << " Alignment:" << getAlignment() << " Fragments:[\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 926 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 927 | if (it != begin()) OS << ",\n "; |
| 928 | it->dump(); |
| 929 | } |
| 930 | OS << "]>"; |
| 931 | } |
| 932 | |
| 933 | void MCSymbolData::dump() { |
| 934 | raw_ostream &OS = llvm::errs(); |
| 935 | |
| 936 | OS << "<MCSymbolData Symbol:" << getSymbol() |
| 937 | << " Fragment:" << getFragment() << " Offset:" << getOffset() |
| 938 | << " Flags:" << getFlags() << " Index:" << getIndex(); |
| 939 | if (isCommon()) |
| 940 | OS << " (common, size:" << getCommonSize() |
| 941 | << " align: " << getCommonAlignment() << ")"; |
| 942 | if (isExternal()) |
| 943 | OS << " (external)"; |
| 944 | if (isPrivateExtern()) |
| 945 | OS << " (private extern)"; |
| 946 | OS << ">"; |
| 947 | } |
| 948 | |
| 949 | void MCAssembler::dump() { |
| 950 | raw_ostream &OS = llvm::errs(); |
| 951 | |
| 952 | OS << "<MCAssembler\n"; |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 953 | OS << " Sections:[\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 954 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 955 | if (it != begin()) OS << ",\n "; |
| 956 | it->dump(); |
| 957 | } |
| 958 | OS << "],\n"; |
| 959 | OS << " Symbols:["; |
| 960 | |
| 961 | for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 962 | if (it != symbol_begin()) OS << ",\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 963 | it->dump(); |
| 964 | } |
| 965 | OS << "]>\n"; |
| 966 | } |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 967 | |
| 968 | // anchors for MC*Fragment vtables |
| 969 | void MCDataFragment::anchor() { } |
| 970 | void MCInstFragment::anchor() { } |
| 971 | void MCAlignFragment::anchor() { } |
| 972 | void MCFillFragment::anchor() { } |
| 973 | void MCOrgFragment::anchor() { } |
| 974 | void MCLEBFragment::anchor() { } |
| 975 | void MCDwarfLineAddrFragment::anchor() { } |
| 976 | void MCDwarfCallFrameFragment::anchor() { } |