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" |
Lang Hames | 1e923ec | 2015-01-09 18:55:42 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | cf55f96 | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCAsmLayout.h" |
Daniel Dunbar | eaa367f | 2010-03-19 10:43:23 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCCodeEmitter.h" |
Rafael Espindola | 0f8abeb | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCContext.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCDwarf.h" |
Daniel Dunbar | 8e6d889 | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCExpr.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCFixupKindInfo.h" |
Daniel Dunbar | f027abf | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCObjectWriter.h" |
Kevin Enderby | e46564a | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSection.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSectionELF.h" |
Daniel Dunbar | 8e6d889 | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSymbol.h" |
| 26 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | d821f4a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | bf387df | 2012-08-08 23:56:06 +0000 | [diff] [blame] | 29 | #include "llvm/Support/LEB128.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Support/TargetRegistry.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | b245927 | 2014-04-29 23:46:48 +0000 | [diff] [blame] | 32 | #include <tuple> |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Chandler Carruth | f58e376 | 2014-04-22 03:04:17 +0000 | [diff] [blame] | 35 | #define DEBUG_TYPE "assembler" |
| 36 | |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | namespace stats { |
Eli Bendersky | 2ccd044 | 2012-12-07 17:59:21 +0000 | [diff] [blame] | 39 | STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total"); |
Eli Bendersky | 0652dfd | 2013-01-08 17:41:59 +0000 | [diff] [blame] | 40 | STATISTIC(EmittedRelaxableFragments, |
| 41 | "Number of emitted assembler fragments - relaxable"); |
Eli Bendersky | c01322e | 2012-12-10 18:59:39 +0000 | [diff] [blame] | 42 | STATISTIC(EmittedDataFragments, |
| 43 | "Number of emitted assembler fragments - data"); |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 44 | STATISTIC(EmittedCompactEncodedInstFragments, |
| 45 | "Number of emitted assembler fragments - compact encoded inst"); |
Eli Bendersky | c01322e | 2012-12-10 18:59:39 +0000 | [diff] [blame] | 46 | STATISTIC(EmittedAlignFragments, |
| 47 | "Number of emitted assembler fragments - align"); |
| 48 | STATISTIC(EmittedFillFragments, |
| 49 | "Number of emitted assembler fragments - fill"); |
| 50 | STATISTIC(EmittedOrgFragments, |
| 51 | "Number of emitted assembler fragments - org"); |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 52 | STATISTIC(evaluateFixup, "Number of evaluated fixups"); |
Daniel Dunbar | d821f4a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 53 | STATISTIC(FragmentLayouts, "Number of fragment layouts"); |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 54 | STATISTIC(ObjectBytes, "Number of emitted object file bytes"); |
Daniel Dunbar | d821f4a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 55 | STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps"); |
| 56 | STATISTIC(RelaxedInstructions, "Number of relaxed instructions"); |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
Daniel Dunbar | 5a07d6a | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 59 | |
Daniel Dunbar | 2701eee | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 60 | // FIXME FIXME FIXME: There are number of places in this file where we convert |
| 61 | // what is a 64-bit assembler value used for computation into a value in the |
| 62 | // object file, which may truncate it. We should detect that truncation where |
| 63 | // invalid and report errors back. |
| 64 | |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 65 | /* *** */ |
| 66 | |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 67 | MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_, |
David Majnemer | 83c862a | 2015-09-01 23:19:38 +0000 | [diff] [blame] | 68 | MCCodeEmitter &Emitter_, MCObjectWriter &Writer_) |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 69 | : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_), |
David Majnemer | 83c862a | 2015-09-01 23:19:38 +0000 | [diff] [blame] | 70 | BundleAlignSize(0), RelaxAll(false), SubsectionsViaSymbols(false), |
David Majnemer | 03e2cc3 | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 71 | IncrementalLinkerCompatible(false), ELFHeaderEFlags(0) { |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 72 | VersionMinInfo.Major = 0; // Major version == 0 for "none specified" |
Daniel Dunbar | e269773 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 73 | } |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 74 | |
| 75 | MCAssembler::~MCAssembler() { |
| 76 | } |
| 77 | |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 78 | void MCAssembler::reset() { |
| 79 | Sections.clear(); |
| 80 | Symbols.clear(); |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 81 | IndirectSymbols.clear(); |
| 82 | DataRegions.clear(); |
Yaron Keren | 559b47d | 2014-09-17 09:25:36 +0000 | [diff] [blame] | 83 | LinkerOptions.clear(); |
| 84 | FileNames.clear(); |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 85 | ThumbFuncs.clear(); |
Yaron Keren | 559b47d | 2014-09-17 09:25:36 +0000 | [diff] [blame] | 86 | BundleAlignSize = 0; |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 87 | RelaxAll = false; |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 88 | SubsectionsViaSymbols = false; |
David Majnemer | 03e2cc3 | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 89 | IncrementalLinkerCompatible = false; |
Jack Carter | 1bd90ff | 2013-01-30 02:09:52 +0000 | [diff] [blame] | 90 | ELFHeaderEFlags = 0; |
Yaron Keren | 559b47d | 2014-09-17 09:25:36 +0000 | [diff] [blame] | 91 | LOHContainer.reset(); |
| 92 | VersionMinInfo.Major = 0; |
Pedro Artigas | b95c53e | 2012-12-14 18:52:11 +0000 | [diff] [blame] | 93 | |
| 94 | // reset objects owned by us |
| 95 | getBackend().reset(); |
| 96 | getEmitter().reset(); |
| 97 | getWriter().reset(); |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 98 | getLOHContainer().reset(); |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Rafael Espindola | 28de224 | 2015-10-03 18:28:40 +0000 | [diff] [blame] | 101 | bool MCAssembler::registerSection(MCSection &Section) { |
| 102 | if (Section.isRegistered()) |
| 103 | return false; |
| 104 | Sections.push_back(&Section); |
| 105 | Section.setIsRegistered(true); |
| 106 | return true; |
| 107 | } |
| 108 | |
Rafael Espindola | b60c829 | 2014-04-29 12:46:50 +0000 | [diff] [blame] | 109 | bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const { |
| 110 | if (ThumbFuncs.count(Symbol)) |
| 111 | return true; |
| 112 | |
| 113 | if (!Symbol->isVariable()) |
| 114 | return false; |
| 115 | |
Rafael Espindola | 5e09641 | 2014-04-30 12:42:22 +0000 | [diff] [blame] | 116 | // 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] | 117 | // is not clear if that is a bug or a feature. |
| 118 | const MCExpr *Expr = Symbol->getVariableValue(); |
| 119 | const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr); |
| 120 | if (!Ref) |
| 121 | return false; |
| 122 | |
| 123 | if (Ref->getKind() != MCSymbolRefExpr::VK_None) |
| 124 | return false; |
| 125 | |
| 126 | const MCSymbol &Sym = Ref->getSymbol(); |
| 127 | if (!isThumbFunc(&Sym)) |
| 128 | return false; |
| 129 | |
| 130 | ThumbFuncs.insert(Symbol); // Cache it. |
| 131 | return true; |
| 132 | } |
| 133 | |
Daniel Dunbar | aa627c3 | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 134 | bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const { |
Daniel Dunbar | c558ec2 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 135 | // Non-temporary labels should always be visible to the linker. |
Daniel Dunbar | aa627c3 | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 136 | if (!Symbol.isTemporary()) |
Daniel Dunbar | c558ec2 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 137 | return true; |
| 138 | |
| 139 | // Absolute temporary labels are never visible. |
Daniel Dunbar | aa627c3 | 2010-06-16 20:04:29 +0000 | [diff] [blame] | 140 | if (!Symbol.isInSection()) |
Daniel Dunbar | c558ec2 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 141 | return false; |
| 142 | |
Rafael Espindola | dfe2d35 | 2015-06-17 20:08:20 +0000 | [diff] [blame] | 143 | if (Symbol.isUsedInReloc()) |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 144 | return true; |
| 145 | |
| 146 | return false; |
Daniel Dunbar | c558ec2 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Duncan P. N. Exon Smith | fd27a1d | 2015-05-20 16:02:11 +0000 | [diff] [blame] | 149 | const MCSymbol *MCAssembler::getAtom(const MCSymbol &S) const { |
Daniel Dunbar | 6e13bb0 | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 150 | // Linker visible symbols define atoms. |
Duncan P. N. Exon Smith | fd27a1d | 2015-05-20 16:02:11 +0000 | [diff] [blame] | 151 | if (isSymbolLinkerVisible(S)) |
| 152 | return &S; |
Daniel Dunbar | 6e13bb0 | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 153 | |
| 154 | // Absolute and undefined symbols have no defining atom. |
Rafael Espindola | e3a20f5 | 2015-10-05 12:07:05 +0000 | [diff] [blame] | 155 | if (!S.isInSection()) |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 156 | return nullptr; |
Daniel Dunbar | 6e13bb0 | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 157 | |
Daniel Dunbar | ba2f4c3 | 2010-05-12 00:38:17 +0000 | [diff] [blame] | 158 | // Non-linker visible symbols in sections which can't be atomized have no |
| 159 | // defining atom. |
Lang Hames | 1e923ec | 2015-01-09 18:55:42 +0000 | [diff] [blame] | 160 | if (!getContext().getAsmInfo()->isSectionAtomizableBySymbols( |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 161 | *S.getFragment()->getParent())) |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 162 | return nullptr; |
Daniel Dunbar | ba2f4c3 | 2010-05-12 00:38:17 +0000 | [diff] [blame] | 163 | |
Daniel Dunbar | 3937e28 | 2010-05-11 17:22:50 +0000 | [diff] [blame] | 164 | // Otherwise, return the atom for the containing fragment. |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 165 | return S.getFragment()->getAtom(); |
Daniel Dunbar | 6e13bb0 | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 168 | bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout, |
Daniel Dunbar | b34440a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 169 | const MCFixup &Fixup, const MCFragment *DF, |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 170 | MCValue &Target, uint64_t &Value) const { |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 171 | ++stats::evaluateFixup; |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 172 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 173 | // FIXME: This code has some duplication with recordRelocation. We should |
Rafael Espindola | 83120cd | 2014-07-01 14:34:30 +0000 | [diff] [blame] | 174 | // probably merge the two into a single callback that tries to evaluate a |
| 175 | // fixup and records a relocation if one is needed. |
| 176 | const MCExpr *Expr = Fixup.getValue(); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 177 | if (!Expr->evaluateAsRelocatable(Target, &Layout, &Fixup)) { |
| 178 | getContext().reportError(Fixup.getLoc(), "expected relocatable expression"); |
| 179 | // Claim to have completely evaluated the fixup, to prevent any further |
| 180 | // processing from being done. |
| 181 | Value = 0; |
| 182 | return true; |
| 183 | } |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 184 | |
Rafael Espindola | 0f8abeb | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 185 | bool IsPCRel = Backend.getFixupKindInfo( |
| 186 | Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel; |
| 187 | |
| 188 | bool IsResolved; |
| 189 | if (IsPCRel) { |
| 190 | if (Target.getSymB()) { |
| 191 | IsResolved = false; |
| 192 | } else if (!Target.getSymA()) { |
| 193 | IsResolved = false; |
| 194 | } else { |
Rafael Espindola | 490d02a | 2011-02-16 03:25:55 +0000 | [diff] [blame] | 195 | const MCSymbolRefExpr *A = Target.getSymA(); |
| 196 | const MCSymbol &SA = A->getSymbol(); |
Rafael Espindola | 972756b | 2015-04-06 16:10:05 +0000 | [diff] [blame] | 197 | if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) { |
Rafael Espindola | 0f8abeb | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 198 | IsResolved = false; |
| 199 | } else { |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 200 | IsResolved = getWriter().isSymbolRefDifferenceFullyResolvedImpl( |
Duncan P. N. Exon Smith | d81ba53 | 2015-05-16 01:01:55 +0000 | [diff] [blame] | 201 | *this, SA, *DF, false, true); |
Rafael Espindola | 0f8abeb | 2010-12-24 21:22:02 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } else { |
| 205 | IsResolved = Target.isAbsolute(); |
| 206 | } |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 207 | |
| 208 | Value = Target.getConstant(); |
| 209 | |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 210 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
Rafael Espindola | 972756b | 2015-04-06 16:10:05 +0000 | [diff] [blame] | 211 | const MCSymbol &Sym = A->getSymbol(); |
Rafael Espindola | 8c3039b | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 212 | if (Sym.isDefined()) |
Duncan P. N. Exon Smith | 2a40483 | 2015-05-19 23:53:20 +0000 | [diff] [blame] | 213 | Value += Layout.getSymbolOffset(Sym); |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 214 | } |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 215 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
Rafael Espindola | 972756b | 2015-04-06 16:10:05 +0000 | [diff] [blame] | 216 | const MCSymbol &Sym = B->getSymbol(); |
Rafael Espindola | 8c3039b | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 217 | if (Sym.isDefined()) |
Duncan P. N. Exon Smith | 2a40483 | 2015-05-19 23:53:20 +0000 | [diff] [blame] | 218 | Value -= Layout.getSymbolOffset(Sym); |
Daniel Dunbar | 5ec4bdd | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 219 | } |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 220 | |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 221 | |
Daniel Dunbar | 0c9d9fd | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 222 | bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags & |
Owen Anderson | 622ad51 | 2010-12-15 18:48:27 +0000 | [diff] [blame] | 223 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits; |
| 224 | assert((ShouldAlignPC ? IsPCRel : true) && |
| 225 | "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!"); |
| 226 | |
Owen Anderson | 3ef19d9 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 227 | if (IsPCRel) { |
Owen Anderson | 7985529 | 2010-12-17 21:49:48 +0000 | [diff] [blame] | 228 | uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset(); |
Jim Grosbach | 6485a79 | 2011-10-26 22:44:41 +0000 | [diff] [blame] | 229 | |
Owen Anderson | 622ad51 | 2010-12-15 18:48:27 +0000 | [diff] [blame] | 230 | // A number of ARM fixups in Thumb mode require that the effective PC |
| 231 | // 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] | 232 | if (ShouldAlignPC) Offset &= ~0x3; |
Owen Anderson | 7985529 | 2010-12-17 21:49:48 +0000 | [diff] [blame] | 233 | Value -= Offset; |
Owen Anderson | 3ef19d9 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 234 | } |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 235 | |
Jim Grosbach | 7b811d3 | 2012-02-27 21:36:23 +0000 | [diff] [blame] | 236 | // Let the backend adjust the fixup value if necessary, including whether |
| 237 | // we need a relocation. |
| 238 | Backend.processFixupValue(*this, Layout, Fixup, DF, Target, Value, |
| 239 | IsResolved); |
Jim Grosbach | 41955ff | 2010-12-14 18:46:57 +0000 | [diff] [blame] | 240 | |
Daniel Dunbar | ea9e2f8 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 241 | return IsResolved; |
| 242 | } |
| 243 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 244 | uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout, |
Rafael Espindola | 6bdb49d | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 245 | const MCFragment &F) const { |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 246 | switch (F.getKind()) { |
| 247 | case MCFragment::FT_Data: |
Pete Cooper | e0d4037 | 2015-06-17 22:01:28 +0000 | [diff] [blame] | 248 | return cast<MCDataFragment>(F).getContents().size(); |
Eli Bendersky | 5277120 | 2013-01-08 22:05:10 +0000 | [diff] [blame] | 249 | case MCFragment::FT_Relaxable: |
Pete Cooper | e0d4037 | 2015-06-17 22:01:28 +0000 | [diff] [blame] | 250 | return cast<MCRelaxableFragment>(F).getContents().size(); |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 251 | case MCFragment::FT_CompactEncodedInst: |
Pete Cooper | e0d4037 | 2015-06-17 22:01:28 +0000 | [diff] [blame] | 252 | return cast<MCCompactEncodedInstFragment>(F).getContents().size(); |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 253 | case MCFragment::FT_Fill: |
| 254 | return cast<MCFillFragment>(F).getSize(); |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 255 | |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 256 | case MCFragment::FT_LEB: |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 257 | return cast<MCLEBFragment>(F).getContents().size(); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 258 | |
David Majnemer | 4eecd30 | 2015-05-30 04:56:02 +0000 | [diff] [blame] | 259 | case MCFragment::FT_SafeSEH: |
| 260 | return 4; |
| 261 | |
Rafael Espindola | 6bdb49d | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 262 | case MCFragment::FT_Align: { |
| 263 | const MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 264 | unsigned Offset = Layout.getFragmentOffset(&AF); |
| 265 | unsigned Size = OffsetToAlignment(Offset, AF.getAlignment()); |
Owen Anderson | 9d0f923 | 2012-08-29 22:18:56 +0000 | [diff] [blame] | 266 | // If we are padding with nops, force the padding to be larger than the |
| 267 | // minimum nop size. |
| 268 | if (Size > 0 && AF.hasEmitNops()) { |
| 269 | while (Size % getBackend().getMinimumNopSize()) |
| 270 | Size += AF.getAlignment(); |
| 271 | } |
Rafael Espindola | 6bdb49d | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 272 | if (Size > AF.getMaxBytesToEmit()) |
| 273 | return 0; |
| 274 | return Size; |
| 275 | } |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 276 | |
Rafael Espindola | 6bdb49d | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 277 | case MCFragment::FT_Org: { |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 278 | const MCOrgFragment &OF = cast<MCOrgFragment>(F); |
Rafael Espindola | 04d3926 | 2015-11-04 23:50:29 +0000 | [diff] [blame] | 279 | MCValue Value; |
| 280 | if (!OF.getOffset().evaluateAsValue(Value, Layout)) |
Rafael Espindola | 6bdb49d | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 281 | report_fatal_error("expected assembly-time absolute expression"); |
| 282 | |
| 283 | // FIXME: We need a way to communicate this error. |
| 284 | uint64_t FragmentOffset = Layout.getFragmentOffset(&OF); |
Rafael Espindola | 04d3926 | 2015-11-04 23:50:29 +0000 | [diff] [blame] | 285 | int64_t TargetLocation = Value.getConstant(); |
| 286 | if (const MCSymbolRefExpr *A = Value.getSymA()) { |
| 287 | uint64_t Val; |
| 288 | if (!Layout.getSymbolOffset(A->getSymbol(), Val)) |
| 289 | report_fatal_error("expected absolute expression"); |
| 290 | TargetLocation += Val; |
| 291 | } |
Rafael Espindola | 6bdb49d | 2010-12-21 20:35:18 +0000 | [diff] [blame] | 292 | int64_t Size = TargetLocation - FragmentOffset; |
| 293 | if (Size < 0 || Size >= 0x40000000) |
| 294 | report_fatal_error("invalid .org offset '" + Twine(TargetLocation) + |
| 295 | "' (at offset '" + Twine(FragmentOffset) + "')"); |
| 296 | return Size; |
| 297 | } |
Kevin Enderby | e46564a | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 298 | |
Rafael Espindola | 5665a93 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 299 | case MCFragment::FT_Dwarf: |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 300 | return cast<MCDwarfLineAddrFragment>(F).getContents().size(); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 301 | case MCFragment::FT_DwarfFrame: |
| 302 | return cast<MCDwarfCallFrameFragment>(F).getContents().size(); |
Rafael Espindola | e3a20f5 | 2015-10-05 12:07:05 +0000 | [diff] [blame] | 303 | case MCFragment::FT_Dummy: |
| 304 | llvm_unreachable("Should not have been added"); |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 307 | llvm_unreachable("invalid fragment kind"); |
Daniel Dunbar | 454ea71 | 2010-05-13 18:35:06 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 310 | void MCAsmLayout::layoutFragment(MCFragment *F) { |
Daniel Dunbar | 9abade1 | 2010-05-14 00:37:21 +0000 | [diff] [blame] | 311 | MCFragment *Prev = F->getPrevNode(); |
Daniel Dunbar | a7cc32a | 2010-05-12 21:35:25 +0000 | [diff] [blame] | 312 | |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 313 | // We should never try to recompute something which is valid. |
| 314 | assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!"); |
| 315 | // We should never try to compute the fragment layout if its predecessor |
| 316 | // isn't valid. |
| 317 | assert((!Prev || isFragmentValid(Prev)) && |
| 318 | "Attempt to compute fragment before its predecessor!"); |
Daniel Dunbar | a7cc32a | 2010-05-12 21:35:25 +0000 | [diff] [blame] | 319 | |
| 320 | ++stats::FragmentLayouts; |
| 321 | |
Daniel Dunbar | b9f6ac0 | 2010-05-13 20:40:12 +0000 | [diff] [blame] | 322 | // Compute fragment offset and size. |
Rafael Espindola | 93e3cf0 | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 323 | if (Prev) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 324 | F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev); |
| 325 | else |
| 326 | F->Offset = 0; |
Rafael Espindola | 5a1e80b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 327 | LastValidFragment[F->getParent()] = F; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 328 | |
| 329 | // If bundling is enabled and this fragment has instructions in it, it has to |
| 330 | // obey the bundling restrictions. With padding, we'll have: |
| 331 | // |
| 332 | // |
| 333 | // BundlePadding |
Derek Schuff | b76ec3b | 2013-01-31 17:00:03 +0000 | [diff] [blame] | 334 | // ||| |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 335 | // ------------------------------------- |
| 336 | // Prev |##########| F | |
| 337 | // ------------------------------------- |
| 338 | // ^ |
| 339 | // | |
| 340 | // F->Offset |
| 341 | // |
| 342 | // The fragment's offset will point to after the padding, and its computed |
| 343 | // size won't include the padding. |
| 344 | // |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 345 | // When the -mc-relax-all flag is used, we optimize bundling by writting the |
Petr Hosek | 4bbf563 | 2015-06-27 01:49:53 +0000 | [diff] [blame] | 346 | // padding directly into fragments when the instructions are emitted inside |
| 347 | // the streamer. When the fragment is larger than the bundle size, we need to |
| 348 | // ensure that it's bundle aligned. This means that if we end up with |
| 349 | // multiple fragments, we must emit bundle padding between fragments. |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 350 | // |
Petr Hosek | 4bbf563 | 2015-06-27 01:49:53 +0000 | [diff] [blame] | 351 | // ".align N" is an example of a directive that introduces multiple |
| 352 | // fragments. We could add a special case to handle ".align N" by emitting |
| 353 | // within-fragment padding (which would produce less padding when N is less |
| 354 | // than the bundle size), but for now we don't. |
| 355 | // |
| 356 | if (Assembler.isBundlingEnabled() && F->hasInstructions()) { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 357 | assert(isa<MCEncodedFragment>(F) && |
| 358 | "Only MCEncodedFragment implementations have instructions"); |
| 359 | uint64_t FSize = Assembler.computeFragmentSize(*this, *F); |
| 360 | |
Petr Hosek | 4bbf563 | 2015-06-27 01:49:53 +0000 | [diff] [blame] | 361 | if (!Assembler.getRelaxAll() && FSize > Assembler.getBundleAlignSize()) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 362 | report_fatal_error("Fragment can't be larger than a bundle size"); |
| 363 | |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 364 | uint64_t RequiredBundlePadding = computeBundlePadding(Assembler, F, |
| 365 | F->Offset, FSize); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 366 | if (RequiredBundlePadding > UINT8_MAX) |
| 367 | report_fatal_error("Padding cannot exceed 255 bytes"); |
| 368 | F->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding)); |
| 369 | F->Offset += RequiredBundlePadding; |
| 370 | } |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Rafael Espindola | 499c99c | 2015-06-01 14:34:40 +0000 | [diff] [blame] | 373 | void MCAssembler::registerSymbol(const MCSymbol &Symbol, bool *Created) { |
| 374 | bool New = !Symbol.isRegistered(); |
| 375 | if (Created) |
| 376 | *Created = New; |
| 377 | if (New) { |
| 378 | Symbol.setIsRegistered(true); |
| 379 | Symbols.push_back(&Symbol); |
| 380 | } |
| 381 | } |
| 382 | |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 383 | void MCAssembler::writeFragmentPadding(const MCFragment &F, uint64_t FSize, |
| 384 | MCObjectWriter *OW) const { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 385 | // Should NOP padding be written out before this fragment? |
| 386 | unsigned BundlePadding = F.getBundlePadding(); |
| 387 | if (BundlePadding > 0) { |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 388 | assert(isBundlingEnabled() && |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 389 | "Writing bundle padding with disabled bundling"); |
| 390 | assert(F.hasInstructions() && |
| 391 | "Writing bundle padding for a fragment without instructions"); |
| 392 | |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 393 | unsigned TotalLength = BundlePadding + static_cast<unsigned>(FSize); |
| 394 | if (F.alignToBundleEnd() && TotalLength > getBundleAlignSize()) { |
Derek Schuff | b76ec3b | 2013-01-31 17:00:03 +0000 | [diff] [blame] | 395 | // If the padding itself crosses a bundle boundary, it must be emitted |
| 396 | // in 2 pieces, since even nop instructions must not cross boundaries. |
| 397 | // v--------------v <- BundleAlignSize |
| 398 | // v---------v <- BundlePadding |
| 399 | // ---------------------------- |
| 400 | // | Prev |####|####| F | |
| 401 | // ---------------------------- |
| 402 | // ^-------------------^ <- TotalLength |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 403 | unsigned DistanceToBoundary = TotalLength - getBundleAlignSize(); |
| 404 | if (!getBackend().writeNopData(DistanceToBoundary, OW)) |
Derek Schuff | b76ec3b | 2013-01-31 17:00:03 +0000 | [diff] [blame] | 405 | report_fatal_error("unable to write NOP sequence of " + |
| 406 | Twine(DistanceToBoundary) + " bytes"); |
| 407 | BundlePadding -= DistanceToBoundary; |
| 408 | } |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 409 | if (!getBackend().writeNopData(BundlePadding, OW)) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 410 | report_fatal_error("unable to write NOP sequence of " + |
| 411 | Twine(BundlePadding) + " bytes"); |
| 412 | } |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /// \brief Write the fragment \p F to the output file. |
| 416 | static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout, |
| 417 | const MCFragment &F) { |
| 418 | MCObjectWriter *OW = &Asm.getWriter(); |
| 419 | |
| 420 | // FIXME: Embed in fragments instead? |
| 421 | uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F); |
| 422 | |
| 423 | Asm.writeFragmentPadding(F, FragmentSize, OW); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 424 | |
| 425 | // This variable (and its dummy usage) is to participate in the assert at |
| 426 | // the end of the function. |
Daniel Dunbar | f027abf | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 427 | uint64_t Start = OW->getStream().tell(); |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 428 | (void) Start; |
Daniel Dunbar | e024def | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 429 | |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 430 | ++stats::EmittedFragments; |
Daniel Dunbar | 5a07d6a | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 431 | |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 432 | switch (F.getKind()) { |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 433 | case MCFragment::FT_Align: { |
Eli Bendersky | c01322e | 2012-12-10 18:59:39 +0000 | [diff] [blame] | 434 | ++stats::EmittedAlignFragments; |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 435 | const MCAlignFragment &AF = cast<MCAlignFragment>(F); |
Daniel Dunbar | 51402b7 | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 436 | assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!"); |
| 437 | |
Eric Christopher | ed420bb | 2013-08-07 18:51:09 +0000 | [diff] [blame] | 438 | uint64_t Count = FragmentSize / AF.getValueSize(); |
| 439 | |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 440 | // FIXME: This error shouldn't actually occur (the front end should emit |
| 441 | // multiple .align directives to enforce the semantics it wants), but is |
| 442 | // severe enough that we want to report it. How to handle this? |
Daniel Dunbar | 2522dd1 | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 443 | if (Count * AF.getValueSize() != FragmentSize) |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 444 | report_fatal_error("undefined .align directive, value size '" + |
Daniel Dunbar | e024def | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 445 | Twine(AF.getValueSize()) + |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 446 | "' is not a divisor of padding size '" + |
Daniel Dunbar | 2522dd1 | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 447 | Twine(FragmentSize) + "'"); |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 448 | |
Kevin Enderby | e83d74f | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 449 | // See if we are aligning with nops, and if so do that first to try to fill |
| 450 | // 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] | 451 | // 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] | 452 | // 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] | 453 | if (AF.hasEmitNops()) { |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 454 | if (!Asm.getBackend().writeNopData(Count, OW)) |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 455 | report_fatal_error("unable to write nop sequence of " + |
Daniel Dunbar | a9ae3ae | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 456 | Twine(Count) + " bytes"); |
| 457 | break; |
Kevin Enderby | e83d74f | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Daniel Dunbar | a9ae3ae | 2010-03-23 02:36:58 +0000 | [diff] [blame] | 460 | // Otherwise, write out in multiples of the value size. |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 461 | for (uint64_t i = 0; i != Count; ++i) { |
| 462 | switch (AF.getValueSize()) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 463 | default: llvm_unreachable("Invalid size!"); |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 464 | case 1: OW->write8 (uint8_t (AF.getValue())); break; |
| 465 | case 2: OW->write16(uint16_t(AF.getValue())); break; |
| 466 | case 4: OW->write32(uint32_t(AF.getValue())); break; |
| 467 | case 8: OW->write64(uint64_t(AF.getValue())); break; |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | break; |
| 471 | } |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 472 | |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 473 | case MCFragment::FT_Data: |
Eli Bendersky | 2ccd044 | 2012-12-07 17:59:21 +0000 | [diff] [blame] | 474 | ++stats::EmittedDataFragments; |
Pete Cooper | e0d4037 | 2015-06-17 22:01:28 +0000 | [diff] [blame] | 475 | OW->writeBytes(cast<MCDataFragment>(F).getContents()); |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 476 | break; |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 477 | |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 478 | case MCFragment::FT_Relaxable: |
Eli Bendersky | 0652dfd | 2013-01-08 17:41:59 +0000 | [diff] [blame] | 479 | ++stats::EmittedRelaxableFragments; |
Pete Cooper | e0d4037 | 2015-06-17 22:01:28 +0000 | [diff] [blame] | 480 | OW->writeBytes(cast<MCRelaxableFragment>(F).getContents()); |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 481 | break; |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 482 | |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 483 | case MCFragment::FT_CompactEncodedInst: |
| 484 | ++stats::EmittedCompactEncodedInstFragments; |
Pete Cooper | e0d4037 | 2015-06-17 22:01:28 +0000 | [diff] [blame] | 485 | OW->writeBytes(cast<MCCompactEncodedInstFragment>(F).getContents()); |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 486 | break; |
| 487 | |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 488 | case MCFragment::FT_Fill: { |
Eli Bendersky | c01322e | 2012-12-10 18:59:39 +0000 | [diff] [blame] | 489 | ++stats::EmittedFillFragments; |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 490 | const MCFillFragment &FF = cast<MCFillFragment>(F); |
Rafael Espindola | a39d305 | 2016-01-19 17:47:48 +0000 | [diff] [blame^] | 491 | uint8_t V = FF.getValue(); |
| 492 | const unsigned MaxChunkSize = 16; |
| 493 | char Data[MaxChunkSize]; |
| 494 | memcpy(Data, &V, 1); |
| 495 | for (unsigned I = 1; I < MaxChunkSize; ++I) |
| 496 | Data[I] = Data[0]; |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 497 | |
Rafael Espindola | a39d305 | 2016-01-19 17:47:48 +0000 | [diff] [blame^] | 498 | uint64_t Size = FF.getSize(); |
| 499 | for (unsigned ChunkSize = MaxChunkSize; ChunkSize; ChunkSize /= 2) { |
| 500 | StringRef Ref(Data, ChunkSize); |
| 501 | for (uint64_t I = 0, E = Size / ChunkSize; I != E; ++I) |
| 502 | OW->writeBytes(Ref); |
| 503 | Size = Size % ChunkSize; |
| 504 | } |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 505 | break; |
| 506 | } |
Daniel Dunbar | e024def | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 507 | |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 508 | case MCFragment::FT_LEB: { |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 509 | const MCLEBFragment &LF = cast<MCLEBFragment>(F); |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 510 | OW->writeBytes(LF.getContents()); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 511 | break; |
| 512 | } |
| 513 | |
David Majnemer | 4eecd30 | 2015-05-30 04:56:02 +0000 | [diff] [blame] | 514 | case MCFragment::FT_SafeSEH: { |
| 515 | const MCSafeSEHFragment &SF = cast<MCSafeSEHFragment>(F); |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 516 | OW->write32(SF.getSymbol()->getIndex()); |
David Majnemer | 4eecd30 | 2015-05-30 04:56:02 +0000 | [diff] [blame] | 517 | break; |
| 518 | } |
| 519 | |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 520 | case MCFragment::FT_Org: { |
Eli Bendersky | c01322e | 2012-12-10 18:59:39 +0000 | [diff] [blame] | 521 | ++stats::EmittedOrgFragments; |
David Blaikie | b78e9e5 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 522 | const MCOrgFragment &OF = cast<MCOrgFragment>(F); |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 523 | |
Daniel Dunbar | 2522dd1 | 2010-03-25 02:00:02 +0000 | [diff] [blame] | 524 | for (uint64_t i = 0, e = FragmentSize; i != e; ++i) |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 525 | OW->write8(uint8_t(OF.getValue())); |
Daniel Dunbar | b1068e4 | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 526 | |
| 527 | break; |
| 528 | } |
Kevin Enderby | e46564a | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 529 | |
| 530 | case MCFragment::FT_Dwarf: { |
| 531 | const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F); |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 532 | OW->writeBytes(OF.getContents()); |
Kevin Enderby | e46564a | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 533 | break; |
| 534 | } |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 535 | case MCFragment::FT_DwarfFrame: { |
| 536 | const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F); |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 537 | OW->writeBytes(CF.getContents()); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 538 | break; |
| 539 | } |
Rafael Espindola | e3a20f5 | 2015-10-05 12:07:05 +0000 | [diff] [blame] | 540 | case MCFragment::FT_Dummy: |
| 541 | llvm_unreachable("Should not have been added"); |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 544 | assert(OW->getStream().tell() - Start == FragmentSize && |
| 545 | "The stream should advance by fragment size"); |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 548 | void MCAssembler::writeSectionData(const MCSection *Sec, |
Daniel Dunbar | 5026928 | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 549 | const MCAsmLayout &Layout) const { |
Daniel Dunbar | 42a39d0 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 550 | // Ignore virtual sections. |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 551 | if (Sec->isVirtualSection()) { |
| 552 | assert(Layout.getSectionFileSize(Sec) == 0 && "Invalid size for section!"); |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 553 | |
| 554 | // Check that contents are only things legal inside a virtual section. |
Craig Topper | 1cd693c | 2015-08-02 22:34:02 +0000 | [diff] [blame] | 555 | for (const MCFragment &F : *Sec) { |
| 556 | switch (F.getKind()) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 557 | default: llvm_unreachable("Invalid fragment in virtual section!"); |
Daniel Dunbar | 8e92d9b | 2010-08-18 18:22:37 +0000 | [diff] [blame] | 558 | case MCFragment::FT_Data: { |
| 559 | // Check that we aren't trying to write a non-zero contents (or fixups) |
| 560 | // into a virtual section. This is to support clients which use standard |
| 561 | // directives to fill the contents of virtual sections. |
Craig Topper | 1cd693c | 2015-08-02 22:34:02 +0000 | [diff] [blame] | 562 | const MCDataFragment &DF = cast<MCDataFragment>(F); |
Daniel Dunbar | 8e92d9b | 2010-08-18 18:22:37 +0000 | [diff] [blame] | 563 | assert(DF.fixup_begin() == DF.fixup_end() && |
| 564 | "Cannot have fixups in virtual section!"); |
| 565 | for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i) |
Weiming Zhao | 58eb5ab | 2014-06-22 00:33:44 +0000 | [diff] [blame] | 566 | if (DF.getContents()[i]) { |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 567 | if (auto *ELFSec = dyn_cast<const MCSectionELF>(Sec)) |
Weiming Zhao | 58eb5ab | 2014-06-22 00:33:44 +0000 | [diff] [blame] | 568 | report_fatal_error("non-zero initializer found in section '" + |
| 569 | ELFSec->getSectionName() + "'"); |
| 570 | else |
| 571 | report_fatal_error("non-zero initializer found in virtual section"); |
| 572 | } |
Daniel Dunbar | 8e92d9b | 2010-08-18 18:22:37 +0000 | [diff] [blame] | 573 | break; |
| 574 | } |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 575 | case MCFragment::FT_Align: |
Daniel Dunbar | 8e92d9b | 2010-08-18 18:22:37 +0000 | [diff] [blame] | 576 | // Check that we aren't trying to write a non-zero value into a virtual |
| 577 | // section. |
Craig Topper | 1cd693c | 2015-08-02 22:34:02 +0000 | [diff] [blame] | 578 | assert((cast<MCAlignFragment>(F).getValueSize() == 0 || |
| 579 | cast<MCAlignFragment>(F).getValue() == 0) && |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 580 | "Invalid align in virtual section!"); |
| 581 | break; |
| 582 | case MCFragment::FT_Fill: |
Rafael Espindola | 1a7e8b4 | 2016-01-19 16:57:08 +0000 | [diff] [blame] | 583 | assert((cast<MCFillFragment>(F).getValue() == 0) && |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 584 | "Invalid fill in virtual section!"); |
| 585 | break; |
Daniel Dunbar | 7cd309f | 2010-05-12 22:51:35 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
Daniel Dunbar | 42a39d0 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | |
Daniel Dunbar | 5026928 | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 592 | uint64_t Start = getWriter().getStream().tell(); |
Jim Grosbach | 0aac6ce | 2012-09-18 23:05:18 +0000 | [diff] [blame] | 593 | (void)Start; |
Daniel Dunbar | e024def | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 594 | |
Craig Topper | 1cd693c | 2015-08-02 22:34:02 +0000 | [diff] [blame] | 595 | for (const MCFragment &F : *Sec) |
| 596 | writeFragment(*this, Layout, F); |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 597 | |
Daniel Dunbar | 5026928 | 2010-12-17 02:45:59 +0000 | [diff] [blame] | 598 | assert(getWriter().getStream().tell() - Start == |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 599 | Layout.getSectionAddressSize(Sec)); |
Daniel Dunbar | 4ef7fb9 | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 602 | std::pair<uint64_t, bool> MCAssembler::handleFixup(const MCAsmLayout &Layout, |
| 603 | MCFragment &F, |
| 604 | const MCFixup &Fixup) { |
Joerg Sonnenberger | 808df67 | 2014-01-13 15:50:36 +0000 | [diff] [blame] | 605 | // Evaluate the fixup. |
| 606 | MCValue Target; |
| 607 | uint64_t FixedValue; |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 608 | bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags & |
| 609 | MCFixupKindInfo::FKF_IsPCRel; |
Joerg Sonnenberger | 808df67 | 2014-01-13 15:50:36 +0000 | [diff] [blame] | 610 | if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) { |
| 611 | // The fixup was unresolved, we need a relocation. Inform the object |
| 612 | // writer of the relocation, and give it an opportunity to adjust the |
| 613 | // fixup value if need be. |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 614 | getWriter().recordRelocation(*this, Layout, &F, Fixup, Target, IsPCRel, |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 615 | FixedValue); |
Joerg Sonnenberger | 808df67 | 2014-01-13 15:50:36 +0000 | [diff] [blame] | 616 | } |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 617 | return std::make_pair(FixedValue, IsPCRel); |
Joerg Sonnenberger | 808df67 | 2014-01-13 15:50:36 +0000 | [diff] [blame] | 618 | } |
Rafael Espindola | 0f30fec | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 619 | |
Frederic Riss | 74b9882 | 2015-08-26 05:09:49 +0000 | [diff] [blame] | 620 | void MCAssembler::layout(MCAsmLayout &Layout) { |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 621 | DEBUG_WITH_TYPE("mc-dump", { |
| 622 | llvm::errs() << "assembler backend - pre-layout\n--\n"; |
| 623 | dump(); }); |
| 624 | |
Daniel Dunbar | 8f9d226 | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 625 | // Create dummy fragments and assign section ordinals. |
Daniel Dunbar | aa8bd69 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 626 | unsigned SectionIndex = 0; |
Craig Topper | 1cd693c | 2015-08-02 22:34:02 +0000 | [diff] [blame] | 627 | for (MCSection &Sec : *this) { |
Daniel Dunbar | aa8bd69 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 628 | // Create dummy fragments to eliminate any empty sections, this simplifies |
| 629 | // layout. |
Craig Topper | 1cd693c | 2015-08-02 22:34:02 +0000 | [diff] [blame] | 630 | if (Sec.getFragmentList().empty()) |
| 631 | new MCDataFragment(&Sec); |
Daniel Dunbar | aa8bd69 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 632 | |
Craig Topper | 1cd693c | 2015-08-02 22:34:02 +0000 | [diff] [blame] | 633 | Sec.setOrdinal(SectionIndex++); |
Daniel Dunbar | 8f9d226 | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 634 | } |
Daniel Dunbar | aa8bd69 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 635 | |
Daniel Dunbar | 8f9d226 | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 636 | // Assign layout order indices to sections and fragments. |
Daniel Dunbar | 8f9d226 | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 637 | for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) { |
Rafael Espindola | 5a1e80b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 638 | MCSection *Sec = Layout.getSectionOrder()[i]; |
| 639 | Sec->setLayoutOrder(i); |
Daniel Dunbar | 8f9d226 | 2010-05-14 00:37:14 +0000 | [diff] [blame] | 640 | |
Rafael Espindola | 6624f47 | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 641 | unsigned FragmentIndex = 0; |
Craig Topper | 1cd693c | 2015-08-02 22:34:02 +0000 | [diff] [blame] | 642 | for (MCFragment &Frag : *Sec) |
| 643 | Frag.setLayoutOrder(FragmentIndex++); |
Daniel Dunbar | aa8bd69 | 2010-05-13 08:43:37 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Daniel Dunbar | c7c53ea | 2010-05-13 02:34:14 +0000 | [diff] [blame] | 646 | // Layout until everything fits. |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 647 | while (layoutOnce(Layout)) |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 648 | continue; |
| 649 | |
| 650 | DEBUG_WITH_TYPE("mc-dump", { |
Daniel Dunbar | b31c49a | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 651 | llvm::errs() << "assembler backend - post-relaxation\n--\n"; |
| 652 | dump(); }); |
| 653 | |
| 654 | // Finalize the layout, including fragment lowering. |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 655 | finishLayout(Layout); |
Daniel Dunbar | b31c49a | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 656 | |
| 657 | DEBUG_WITH_TYPE("mc-dump", { |
| 658 | llvm::errs() << "assembler backend - final-layout\n--\n"; |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 659 | dump(); }); |
| 660 | |
Daniel Dunbar | d84d196 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 661 | // Allow the object writer a chance to perform post-layout binding (for |
| 662 | // example, to set the index fields in the symbol data). |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 663 | getWriter().executePostLayoutBinding(*this, Layout); |
Daniel Dunbar | d84d196 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 664 | |
Daniel Dunbar | 3cab275 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 665 | // Evaluate and apply the fixups, generating relocation entries as necessary. |
Craig Topper | 1cd693c | 2015-08-02 22:34:02 +0000 | [diff] [blame] | 666 | for (MCSection &Sec : *this) { |
| 667 | for (MCFragment &Frag : Sec) { |
| 668 | MCEncodedFragment *F = dyn_cast<MCEncodedFragment>(&Frag); |
Pete Cooper | e0d4037 | 2015-06-17 22:01:28 +0000 | [diff] [blame] | 669 | // Data and relaxable fragments both have fixups. So only process |
| 670 | // those here. |
| 671 | // FIXME: Is there a better way to do this? MCEncodedFragmentWithFixups |
| 672 | // being templated makes this tricky. |
| 673 | if (!F || isa<MCCompactEncodedInstFragment>(F)) |
| 674 | continue; |
| 675 | ArrayRef<MCFixup> Fixups; |
| 676 | MutableArrayRef<char> Contents; |
| 677 | if (auto *FragWithFixups = dyn_cast<MCDataFragment>(F)) { |
| 678 | Fixups = FragWithFixups->getFixups(); |
| 679 | Contents = FragWithFixups->getContents(); |
| 680 | } else if (auto *FragWithFixups = dyn_cast<MCRelaxableFragment>(F)) { |
| 681 | Fixups = FragWithFixups->getFixups(); |
| 682 | Contents = FragWithFixups->getContents(); |
| 683 | } else |
Yaron Keren | fffc068 | 2015-07-04 05:48:52 +0000 | [diff] [blame] | 684 | llvm_unreachable("Unknown fragment with fixups!"); |
Pete Cooper | e0d4037 | 2015-06-17 22:01:28 +0000 | [diff] [blame] | 685 | for (const MCFixup &Fixup : Fixups) { |
| 686 | uint64_t FixedValue; |
| 687 | bool IsPCRel; |
| 688 | std::tie(FixedValue, IsPCRel) = handleFixup(Layout, *F, Fixup); |
| 689 | getBackend().applyFixup(Fixup, Contents.data(), |
| 690 | Contents.size(), FixedValue, IsPCRel); |
Daniel Dunbar | 3cab275 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | } |
Frederic Riss | 74b9882 | 2015-08-26 05:09:49 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void MCAssembler::Finish() { |
| 697 | // Create the layout object. |
| 698 | MCAsmLayout Layout(*this); |
| 699 | layout(Layout); |
| 700 | |
David Majnemer | 83c862a | 2015-09-01 23:19:38 +0000 | [diff] [blame] | 701 | raw_ostream &OS = getWriter().getStream(); |
Frederic Riss | 74b9882 | 2015-08-26 05:09:49 +0000 | [diff] [blame] | 702 | uint64_t StartOffset = OS.tell(); |
Daniel Dunbar | 3cab275 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 703 | |
Daniel Dunbar | d84d196 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 704 | // Write the object file. |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 705 | getWriter().writeObject(*this, Layout); |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 706 | |
| 707 | stats::ObjectBytes += OS.tell() - StartOffset; |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 710 | bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup, |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 711 | const MCRelaxableFragment *DF, |
Daniel Dunbar | 32ffc58 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 712 | const MCAsmLayout &Layout) const { |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 713 | MCValue Target; |
| 714 | uint64_t Value; |
Colin LeMahieu | a01780f | 2015-05-30 18:42:22 +0000 | [diff] [blame] | 715 | bool Resolved = evaluateFixup(Layout, Fixup, DF, Target, Value); |
| 716 | return getBackend().fixupNeedsRelaxationAdvanced(Fixup, Resolved, Value, DF, |
| 717 | Layout); |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 720 | bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F, |
Daniel Dunbar | de04b3f | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 721 | const MCAsmLayout &Layout) const { |
| 722 | // If this inst doesn't ever need relaxation, ignore it. This occurs when we |
| 723 | // are intentionally pushing out inst fragments, or because we relaxed a |
| 724 | // previous instruction to one that doesn't need relaxation. |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 725 | if (!getBackend().mayNeedRelaxation(F->getInst())) |
Daniel Dunbar | de04b3f | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 726 | return false; |
| 727 | |
Craig Topper | 1cd693c | 2015-08-02 22:34:02 +0000 | [diff] [blame] | 728 | for (const MCFixup &Fixup : F->getFixups()) |
| 729 | if (fixupNeedsRelaxation(Fixup, F, Layout)) |
Daniel Dunbar | de04b3f | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 730 | return true; |
| 731 | |
| 732 | return false; |
| 733 | } |
| 734 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 735 | bool MCAssembler::relaxInstruction(MCAsmLayout &Layout, |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 736 | MCRelaxableFragment &F) { |
| 737 | if (!fragmentNeedsRelaxation(&F, Layout)) |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 738 | return false; |
| 739 | |
| 740 | ++stats::RelaxedInstructions; |
| 741 | |
| 742 | // FIXME-PERF: We could immediately lower out instructions if we can tell |
| 743 | // they are fully resolved, to avoid retesting on later passes. |
| 744 | |
| 745 | // Relax the fragment. |
| 746 | |
| 747 | MCInst Relaxed; |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 748 | getBackend().relaxInstruction(F.getInst(), Relaxed); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 749 | |
| 750 | // Encode the new instruction. |
| 751 | // |
| 752 | // FIXME-PERF: If it matters, we could let the target do this. It can |
| 753 | // probably do so more efficiently in many cases. |
| 754 | SmallVector<MCFixup, 4> Fixups; |
| 755 | SmallString<256> Code; |
| 756 | raw_svector_ostream VecOS(Code); |
Jim Grosbach | 91df21f | 2015-05-15 19:13:16 +0000 | [diff] [blame] | 757 | getEmitter().encodeInstruction(Relaxed, VecOS, Fixups, F.getSubtargetInfo()); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 758 | |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 759 | // Update the fragment. |
| 760 | F.setInst(Relaxed); |
| 761 | F.getContents() = Code; |
| 762 | F.getFixups() = Fixups; |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 763 | |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 764 | return true; |
| 765 | } |
| 766 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 767 | bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) { |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 768 | uint64_t OldSize = LF.getContents().size(); |
Rafael Espindola | dbb4021 | 2015-03-25 00:25:37 +0000 | [diff] [blame] | 769 | int64_t Value; |
| 770 | bool Abs = LF.getValue().evaluateKnownAbsolute(Value, Layout); |
| 771 | if (!Abs) |
| 772 | report_fatal_error("sleb128 and uleb128 expressions must be absolute"); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 773 | SmallString<8> &Data = LF.getContents(); |
| 774 | Data.clear(); |
| 775 | raw_svector_ostream OSE(Data); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 776 | if (LF.isSigned()) |
Jim Grosbach | bf387df | 2012-08-08 23:56:06 +0000 | [diff] [blame] | 777 | encodeSLEB128(Value, OSE); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 778 | else |
Jim Grosbach | bf387df | 2012-08-08 23:56:06 +0000 | [diff] [blame] | 779 | encodeULEB128(Value, OSE); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 780 | return OldSize != LF.getContents().size(); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 783 | bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout, |
Jim Grosbach | 46be301 | 2011-12-06 00:13:09 +0000 | [diff] [blame] | 784 | MCDwarfLineAddrFragment &DF) { |
Ulrich Weigand | 32d725b | 2013-06-12 14:46:54 +0000 | [diff] [blame] | 785 | MCContext &Context = Layout.getAssembler().getContext(); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 786 | uint64_t OldSize = DF.getContents().size(); |
Rafael Espindola | dbb4021 | 2015-03-25 00:25:37 +0000 | [diff] [blame] | 787 | int64_t AddrDelta; |
| 788 | bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout); |
| 789 | assert(Abs && "We created a line delta with an invalid expression"); |
Rafael Espindola | 44cc654 | 2015-03-25 00:45:41 +0000 | [diff] [blame] | 790 | (void) Abs; |
Rafael Espindola | 5665a93 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 791 | int64_t LineDelta; |
| 792 | LineDelta = DF.getLineDelta(); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 793 | SmallString<8> &Data = DF.getContents(); |
| 794 | Data.clear(); |
| 795 | raw_svector_ostream OSE(Data); |
Frederic Riss | a5ab844 | 2015-08-07 15:14:08 +0000 | [diff] [blame] | 796 | MCDwarfLineAddr::Encode(Context, getDWARFLinetableParams(), LineDelta, |
| 797 | AddrDelta, OSE); |
Rafael Espindola | 99e026d | 2010-12-04 21:58:52 +0000 | [diff] [blame] | 798 | return OldSize != Data.size(); |
Rafael Espindola | 5665a93 | 2010-11-07 02:07:12 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 801 | bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout, |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 802 | MCDwarfCallFrameFragment &DF) { |
Ulrich Weigand | 32d725b | 2013-06-12 14:46:54 +0000 | [diff] [blame] | 803 | MCContext &Context = Layout.getAssembler().getContext(); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 804 | uint64_t OldSize = DF.getContents().size(); |
Rafael Espindola | dbb4021 | 2015-03-25 00:25:37 +0000 | [diff] [blame] | 805 | int64_t AddrDelta; |
| 806 | bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout); |
| 807 | assert(Abs && "We created call frame with an invalid expression"); |
Rafael Espindola | 44cc654 | 2015-03-25 00:45:41 +0000 | [diff] [blame] | 808 | (void) Abs; |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 809 | SmallString<8> &Data = DF.getContents(); |
| 810 | Data.clear(); |
| 811 | raw_svector_ostream OSE(Data); |
Ulrich Weigand | 32d725b | 2013-06-12 14:46:54 +0000 | [diff] [blame] | 812 | MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OSE); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 813 | return OldSize != Data.size(); |
| 814 | } |
| 815 | |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 816 | bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec) { |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 817 | // Holds the first fragment which needed relaxing during this layout. It will |
| 818 | // remain NULL if none were relaxed. |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 819 | // When a fragment is relaxed, all the fragments following it should get |
| 820 | // invalidated because their offset is going to change. |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 821 | MCFragment *FirstRelaxedFragment = nullptr; |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 822 | |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 823 | // Attempt to relax all the fragments in the section. |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 824 | for (MCSection::iterator I = Sec.begin(), IE = Sec.end(); I != IE; ++I) { |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 825 | // Check if this is a fragment that needs relaxation. |
| 826 | bool RelaxedFrag = false; |
| 827 | switch(I->getKind()) { |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 828 | default: |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 829 | break; |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 830 | case MCFragment::FT_Relaxable: |
Eli Bendersky | 0f74f17 | 2012-12-11 17:16:00 +0000 | [diff] [blame] | 831 | assert(!getRelaxAll() && |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 832 | "Did not expect a MCRelaxableFragment in RelaxAll mode"); |
| 833 | RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I)); |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 834 | break; |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 835 | case MCFragment::FT_Dwarf: |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 836 | RelaxedFrag = relaxDwarfLineAddr(Layout, |
| 837 | *cast<MCDwarfLineAddrFragment>(I)); |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 838 | break; |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 839 | case MCFragment::FT_DwarfFrame: |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 840 | RelaxedFrag = |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 841 | relaxDwarfCallFrameFragment(Layout, |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 842 | *cast<MCDwarfCallFrameFragment>(I)); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 843 | break; |
| 844 | case MCFragment::FT_LEB: |
Eli Bendersky | 4c7296f | 2012-12-10 20:13:43 +0000 | [diff] [blame] | 845 | RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I)); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 846 | break; |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 847 | } |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 848 | if (RelaxedFrag && !FirstRelaxedFragment) |
Duncan P. N. Exon Smith | a5f45da | 2015-10-10 00:13:11 +0000 | [diff] [blame] | 849 | FirstRelaxedFragment = &*I; |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 850 | } |
Eli Bendersky | e11ab3a | 2012-12-12 19:54:05 +0000 | [diff] [blame] | 851 | if (FirstRelaxedFragment) { |
Derek Schuff | 90aa1d8 | 2013-02-05 17:55:27 +0000 | [diff] [blame] | 852 | Layout.invalidateFragmentsFrom(FirstRelaxedFragment); |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 853 | return true; |
| 854 | } |
| 855 | return false; |
| 856 | } |
| 857 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 858 | bool MCAssembler::layoutOnce(MCAsmLayout &Layout) { |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 859 | ++stats::RelaxationSteps; |
| 860 | |
Daniel Dunbar | 6432bd7 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 861 | bool WasRelaxed = false; |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 862 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
Rafael Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 863 | MCSection &Sec = *it; |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 864 | while (layoutSectionOnce(Layout, Sec)) |
Rafael Espindola | 98d93c5 | 2010-12-21 04:22:09 +0000 | [diff] [blame] | 865 | WasRelaxed = true; |
Daniel Dunbar | 12f1e32 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Daniel Dunbar | 6432bd7 | 2010-03-25 19:35:56 +0000 | [diff] [blame] | 868 | return WasRelaxed; |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 869 | } |
Daniel Dunbar | bedf1d4 | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 870 | |
Jim Grosbach | 18e2fe4 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 871 | void MCAssembler::finishLayout(MCAsmLayout &Layout) { |
Rafael Espindola | 8867390 | 2010-12-04 22:47:22 +0000 | [diff] [blame] | 872 | // The layout is done. Mark every fragment as valid. |
Rafael Espindola | 6624f47 | 2010-12-07 23:32:26 +0000 | [diff] [blame] | 873 | for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) { |
| 874 | Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin()); |
| 875 | } |
Daniel Dunbar | b31c49a | 2010-03-22 23:16:48 +0000 | [diff] [blame] | 876 | } |