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