Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "assembler" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAssembler.h" |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmLayout.h" |
Daniel Dunbar | b36052f | 2010-03-19 10:43:23 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCCodeEmitter.h" |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCObjectWriter.h" |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCSymbol.h" |
| 17 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | 1a9158c | 2010-03-19 10:43:26 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/OwningPtr.h" |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ee0d892 | 2010-03-13 22:10:17 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetRegistry.h" |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetAsmBackend.h" |
Daniel Dunbar | f634676 | 2010-02-13 09:29:02 +0000 | [diff] [blame] | 27 | |
| 28 | // FIXME: Gross. |
| 29 | #include "../Target/X86/X86FixupKinds.h" |
| 30 | |
Chris Lattner | 23132b1 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 31 | #include <vector> |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 34 | STATISTIC(EmittedFragments, "Number of emitted assembler fragments"); |
| 35 | |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 36 | // FIXME FIXME FIXME: There are number of places in this file where we convert |
| 37 | // what is a 64-bit assembler value used for computation into a value in the |
| 38 | // object file, which may truncate it. We should detect that truncation where |
| 39 | // invalid and report errors back. |
| 40 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 41 | /* *** */ |
| 42 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 43 | MCFragment::MCFragment() : Kind(FragmentType(~0)) { |
| 44 | } |
| 45 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 46 | MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent) |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 47 | : Kind(_Kind), |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 48 | Parent(_Parent), |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 49 | FileSize(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 50 | { |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 51 | if (Parent) |
| 52 | Parent->getFragmentList().push_back(this); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 55 | MCFragment::~MCFragment() { |
| 56 | } |
| 57 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 58 | uint64_t MCFragment::getAddress() const { |
| 59 | assert(getParent() && "Missing Section!"); |
| 60 | return getParent()->getAddress() + Offset; |
| 61 | } |
| 62 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 63 | /* *** */ |
| 64 | |
Daniel Dunbar | 81e4000 | 2009-08-27 00:38:04 +0000 | [diff] [blame] | 65 | MCSectionData::MCSectionData() : Section(0) {} |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 66 | |
| 67 | MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A) |
Daniel Dunbar | 81e4000 | 2009-08-27 00:38:04 +0000 | [diff] [blame] | 68 | : Section(&_Section), |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 69 | Alignment(1), |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 70 | Address(~UINT64_C(0)), |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 71 | Size(~UINT64_C(0)), |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 72 | FileSize(~UINT64_C(0)), |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 73 | HasInstructions(false) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 74 | { |
| 75 | if (A) |
| 76 | A->getSectionList().push_back(this); |
| 77 | } |
| 78 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 79 | /* *** */ |
| 80 | |
Daniel Dunbar | efbb533 | 2009-09-01 04:09:03 +0000 | [diff] [blame] | 81 | MCSymbolData::MCSymbolData() : Symbol(0) {} |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 82 | |
Daniel Dunbar | cb579b3 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 83 | MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 84 | uint64_t _Offset, MCAssembler *A) |
Daniel Dunbar | efbb533 | 2009-09-01 04:09:03 +0000 | [diff] [blame] | 85 | : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset), |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 86 | IsExternal(false), IsPrivateExtern(false), |
| 87 | CommonSize(0), CommonAlign(0), Flags(0), Index(0) |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 88 | { |
| 89 | if (A) |
| 90 | A->getSymbolList().push_back(this); |
| 91 | } |
| 92 | |
| 93 | /* *** */ |
| 94 | |
Daniel Dunbar | 1f3e445 | 2010-03-11 01:34:27 +0000 | [diff] [blame] | 95 | MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend, |
Daniel Dunbar | cf871e5 | 2010-03-19 10:43:18 +0000 | [diff] [blame] | 96 | MCCodeEmitter &_Emitter, raw_ostream &_OS) |
| 97 | : Context(_Context), Backend(_Backend), Emitter(_Emitter), |
| 98 | OS(_OS), SubsectionsViaSymbols(false) |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 99 | { |
| 100 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 101 | |
| 102 | MCAssembler::~MCAssembler() { |
| 103 | } |
| 104 | |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 105 | static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm, |
| 106 | const MCAsmFixup &Fixup, |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 107 | const MCValue Target, |
| 108 | const MCSection *BaseSection) { |
| 109 | // The effective fixup address is |
| 110 | // addr(atom(A)) + offset(A) |
| 111 | // - addr(atom(B)) - offset(B) |
| 112 | // - addr(<base symbol>) + <fixup offset from base symbol> |
| 113 | // and the offsets are not relocatable, so the fixup is fully resolved when |
| 114 | // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0. |
| 115 | // |
| 116 | // The simple (Darwin, except on x86_64) way of dealing with this was to |
| 117 | // assume that any reference to a temporary symbol *must* be a temporary |
| 118 | // symbol in the same atom, unless the sections differ. Therefore, any PCrel |
| 119 | // relocation to a temporary symbol (in the same section) is fully |
| 120 | // resolved. This also works in conjunction with absolutized .set, which |
| 121 | // requires the compiler to use .set to absolutize the differences between |
| 122 | // symbols which the compiler knows to be assembly time constants, so we don't |
| 123 | // need to worry about consider symbol differences fully resolved. |
| 124 | |
| 125 | // Non-relative fixups are only resolved if constant. |
| 126 | if (!BaseSection) |
| 127 | return Target.isAbsolute(); |
| 128 | |
| 129 | // Otherwise, relative fixups are only resolved if not a difference and the |
| 130 | // target is a temporary in the same section. |
| 131 | if (Target.isAbsolute() || Target.getSymB()) |
| 132 | return false; |
| 133 | |
| 134 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
| 135 | if (!A->isTemporary() || !A->isInSection() || |
| 136 | &A->getSection() != BaseSection) |
| 137 | return false; |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
Daniel Dunbar | 034843a | 2010-03-19 03:18:18 +0000 | [diff] [blame] | 142 | static bool isScatteredFixupFullyResolved(const MCAssembler &Asm, |
| 143 | const MCAsmFixup &Fixup, |
Daniel Dunbar | 034843a | 2010-03-19 03:18:18 +0000 | [diff] [blame] | 144 | const MCValue Target, |
| 145 | const MCSymbolData *BaseSymbol) { |
| 146 | // The effective fixup address is |
| 147 | // addr(atom(A)) + offset(A) |
| 148 | // - addr(atom(B)) - offset(B) |
| 149 | // - addr(BaseSymbol) + <fixup offset from base symbol> |
| 150 | // and the offsets are not relocatable, so the fixup is fully resolved when |
| 151 | // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0. |
| 152 | // |
| 153 | // Note that "false" is almost always conservatively correct (it means we emit |
| 154 | // a relocation which is unnecessary), except when it would force us to emit a |
| 155 | // relocation which the target cannot encode. |
| 156 | |
| 157 | const MCSymbolData *A_Base = 0, *B_Base = 0; |
| 158 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
| 159 | // Modified symbol references cannot be resolved. |
| 160 | if (A->getKind() != MCSymbolRefExpr::VK_None) |
| 161 | return false; |
| 162 | |
| 163 | A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol())); |
| 164 | if (!A_Base) |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
| 169 | // Modified symbol references cannot be resolved. |
| 170 | if (B->getKind() != MCSymbolRefExpr::VK_None) |
| 171 | return false; |
| 172 | |
| 173 | B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol())); |
| 174 | if (!B_Base) |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // If there is no base, A and B have to be the same atom for this fixup to be |
| 179 | // fully resolved. |
| 180 | if (!BaseSymbol) |
| 181 | return A_Base == B_Base; |
| 182 | |
| 183 | // Otherwise, B must be missing and A must be the base. |
| 184 | return !B_Base && BaseSymbol == A_Base; |
| 185 | } |
| 186 | |
Daniel Dunbar | 2386985 | 2010-03-19 03:18:09 +0000 | [diff] [blame] | 187 | bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const { |
| 188 | // Non-temporary labels should always be visible to the linker. |
| 189 | if (!SD->getSymbol().isTemporary()) |
| 190 | return true; |
| 191 | |
| 192 | // Absolute temporary labels are never visible. |
| 193 | if (!SD->getFragment()) |
| 194 | return false; |
| 195 | |
| 196 | // Otherwise, check if the section requires symbols even for temporary labels. |
| 197 | return getBackend().doesSectionRequireSymbols( |
| 198 | SD->getFragment()->getParent()->getSection()); |
| 199 | } |
| 200 | |
Daniel Dunbar | 8ad0dcc | 2010-03-19 03:18:15 +0000 | [diff] [blame] | 201 | const MCSymbolData *MCAssembler::getAtomForAddress(const MCSectionData *Section, |
| 202 | uint64_t Address) const { |
| 203 | const MCSymbolData *Best = 0; |
| 204 | for (MCAssembler::const_symbol_iterator it = symbol_begin(), |
| 205 | ie = symbol_end(); it != ie; ++it) { |
| 206 | // Ignore non-linker visible symbols. |
| 207 | if (!isSymbolLinkerVisible(it)) |
| 208 | continue; |
| 209 | |
| 210 | // Ignore symbols not in the same section. |
| 211 | if (!it->getFragment() || it->getFragment()->getParent() != Section) |
| 212 | continue; |
| 213 | |
| 214 | // Otherwise, find the closest symbol preceding this address (ties are |
| 215 | // resolved in favor of the last defined symbol). |
| 216 | if (it->getAddress() <= Address && |
| 217 | (!Best || it->getAddress() >= Best->getAddress())) |
| 218 | Best = it; |
| 219 | } |
| 220 | |
| 221 | return Best; |
| 222 | } |
| 223 | |
| 224 | const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const { |
| 225 | // Linker visible symbols define atoms. |
| 226 | if (isSymbolLinkerVisible(SD)) |
| 227 | return SD; |
| 228 | |
| 229 | // Absolute and undefined symbols have no defining atom. |
| 230 | if (!SD->getFragment()) |
| 231 | return 0; |
| 232 | |
| 233 | // Otherwise, search by address. |
| 234 | return getAtomForAddress(SD->getFragment()->getParent(), SD->getAddress()); |
| 235 | } |
| 236 | |
Daniel Dunbar | 9d39e61 | 2010-03-22 21:49:41 +0000 | [diff] [blame] | 237 | bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, |
| 238 | const MCAsmFixup &Fixup, const MCFragment *DF, |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 239 | MCValue &Target, uint64_t &Value) const { |
| 240 | if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout)) |
| 241 | llvm_report_error("expected relocatable expression"); |
| 242 | |
| 243 | // FIXME: How do non-scattered symbols work in ELF? I presume the linker |
| 244 | // doesn't support small relocations, but then under what criteria does the |
| 245 | // assembler allow symbol differences? |
| 246 | |
| 247 | Value = Target.getConstant(); |
| 248 | |
Daniel Dunbar | b36052f | 2010-03-19 10:43:23 +0000 | [diff] [blame] | 249 | bool IsPCRel = |
| 250 | Emitter.getFixupKindInfo(Fixup.Kind).Flags & MCFixupKindInfo::FKF_IsPCRel; |
| 251 | bool IsResolved = true; |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 252 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
| 253 | if (A->getSymbol().isDefined()) |
| 254 | Value += getSymbolData(A->getSymbol()).getAddress(); |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 255 | else |
| 256 | IsResolved = false; |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 257 | } |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 258 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
| 259 | if (B->getSymbol().isDefined()) |
| 260 | Value -= getSymbolData(B->getSymbol()).getAddress(); |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 261 | else |
| 262 | IsResolved = false; |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 263 | } |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 264 | |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 265 | // If we are using scattered symbols, determine whether this value is actually |
| 266 | // resolved; scattering may cause atoms to move. |
| 267 | if (IsResolved && getBackend().hasScatteredSymbols()) { |
| 268 | if (getBackend().hasReliableSymbolDifference()) { |
Daniel Dunbar | 034843a | 2010-03-19 03:18:18 +0000 | [diff] [blame] | 269 | // If this is a PCrel relocation, find the base atom (identified by its |
| 270 | // symbol) that the fixup value is relative to. |
| 271 | const MCSymbolData *BaseSymbol = 0; |
| 272 | if (IsPCRel) { |
| 273 | BaseSymbol = getAtomForAddress( |
| 274 | DF->getParent(), DF->getAddress() + Fixup.Offset); |
| 275 | if (!BaseSymbol) |
| 276 | IsResolved = false; |
| 277 | } |
| 278 | |
| 279 | if (IsResolved) |
Daniel Dunbar | c6f5982 | 2010-03-22 21:49:38 +0000 | [diff] [blame] | 280 | IsResolved = isScatteredFixupFullyResolved(*this, Fixup, Target, |
Daniel Dunbar | 034843a | 2010-03-19 03:18:18 +0000 | [diff] [blame] | 281 | BaseSymbol); |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 282 | } else { |
| 283 | const MCSection *BaseSection = 0; |
| 284 | if (IsPCRel) |
| 285 | BaseSection = &DF->getParent()->getSection(); |
| 286 | |
Daniel Dunbar | c6f5982 | 2010-03-22 21:49:38 +0000 | [diff] [blame] | 287 | IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target, |
Daniel Dunbar | 939f8d7 | 2010-03-19 03:18:12 +0000 | [diff] [blame] | 288 | BaseSection); |
| 289 | } |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | if (IsPCRel) |
Daniel Dunbar | da3e9f7 | 2010-03-13 02:38:00 +0000 | [diff] [blame] | 293 | Value -= DF->getAddress() + Fixup.Offset; |
Daniel Dunbar | df3c8f2 | 2010-03-12 21:00:49 +0000 | [diff] [blame] | 294 | |
| 295 | return IsResolved; |
| 296 | } |
| 297 | |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 298 | void MCAssembler::LayoutSection(MCSectionData &SD, |
| 299 | MCAsmLayout &Layout) { |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 300 | uint64_t Address = SD.getAddress(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 301 | |
| 302 | for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) { |
| 303 | MCFragment &F = *it; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 304 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 305 | F.setOffset(Address - SD.getAddress()); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 306 | |
| 307 | // Evaluate fragment size. |
| 308 | switch (F.getKind()) { |
| 309 | case MCFragment::FT_Align: { |
| 310 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 311 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 312 | uint64_t Size = OffsetToAlignment(Address, AF.getAlignment()); |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 313 | if (Size > AF.getMaxBytesToEmit()) |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 314 | AF.setFileSize(0); |
| 315 | else |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 316 | AF.setFileSize(Size); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | |
| 320 | case MCFragment::FT_Data: |
Daniel Dunbar | 2a6e3f5 | 2010-03-22 20:35:43 +0000 | [diff] [blame] | 321 | F.setFileSize(cast<MCDataFragment>(F).getContents().size()); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 322 | break; |
| 323 | |
Daniel Dunbar | 2a6e3f5 | 2010-03-22 20:35:43 +0000 | [diff] [blame] | 324 | case MCFragment::FT_Fill: { |
| 325 | MCFillFragment &FF = cast<MCFillFragment>(F); |
| 326 | F.setFileSize(FF.getValueSize() * FF.getCount()); |
| 327 | break; |
| 328 | } |
| 329 | |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame^] | 330 | case MCFragment::FT_Inst: |
| 331 | F.setFileSize(cast<MCInstFragment>(F).getInstSize()); |
| 332 | break; |
| 333 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 334 | case MCFragment::FT_Org: { |
| 335 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 336 | |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 337 | int64_t TargetLocation; |
| 338 | if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout)) |
| 339 | llvm_report_error("expected assembly-time absolute expression"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 340 | |
| 341 | // FIXME: We need a way to communicate this error. |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 342 | int64_t Offset = TargetLocation - F.getOffset(); |
| 343 | if (Offset < 0) |
| 344 | llvm_report_error("invalid .org offset '" + Twine(TargetLocation) + |
| 345 | "' (at offset '" + Twine(F.getOffset()) + "'"); |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 346 | |
Daniel Dunbar | 18ff2cc | 2010-03-11 05:53:33 +0000 | [diff] [blame] | 347 | F.setFileSize(Offset); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 348 | break; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 349 | } |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 350 | |
| 351 | case MCFragment::FT_ZeroFill: { |
| 352 | MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F); |
| 353 | |
| 354 | // Align the fragment offset; it is safe to adjust the offset freely since |
| 355 | // this is only in virtual sections. |
Daniel Dunbar | 37fad5c | 2010-03-08 21:10:42 +0000 | [diff] [blame] | 356 | Address = RoundUpToAlignment(Address, ZFF.getAlignment()); |
| 357 | F.setOffset(Address - SD.getAddress()); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 358 | |
| 359 | // FIXME: This is misnamed. |
| 360 | F.setFileSize(ZFF.getSize()); |
| 361 | break; |
| 362 | } |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 365 | Address += F.getFileSize(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 368 | // Set the section sizes. |
| 369 | SD.setSize(Address - SD.getAddress()); |
Daniel Dunbar | cc5b84c | 2010-03-19 09:29:03 +0000 | [diff] [blame] | 370 | if (getBackend().isVirtualSection(SD.getSection())) |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 371 | SD.setFileSize(0); |
| 372 | else |
| 373 | SD.setFileSize(Address - SD.getAddress()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 376 | /// WriteNopData - Write optimal nops to the output file for the \arg Count |
| 377 | /// bytes. This returns the number of bytes written. It may return 0 if |
| 378 | /// the \arg Count is more than the maximum optimal nops. |
| 379 | /// |
| 380 | /// FIXME this is X86 32-bit specific and should move to a better place. |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 381 | static uint64_t WriteNopData(uint64_t Count, MCObjectWriter *OW) { |
Kevin Enderby | 7668708 | 2010-02-23 21:41:24 +0000 | [diff] [blame] | 382 | static const uint8_t Nops[16][16] = { |
| 383 | // nop |
| 384 | {0x90}, |
| 385 | // xchg %ax,%ax |
| 386 | {0x66, 0x90}, |
| 387 | // nopl (%[re]ax) |
| 388 | {0x0f, 0x1f, 0x00}, |
| 389 | // nopl 0(%[re]ax) |
| 390 | {0x0f, 0x1f, 0x40, 0x00}, |
| 391 | // nopl 0(%[re]ax,%[re]ax,1) |
| 392 | {0x0f, 0x1f, 0x44, 0x00, 0x00}, |
| 393 | // nopw 0(%[re]ax,%[re]ax,1) |
| 394 | {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00}, |
| 395 | // nopl 0L(%[re]ax) |
| 396 | {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00}, |
| 397 | // nopl 0L(%[re]ax,%[re]ax,1) |
| 398 | {0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 399 | // nopw 0L(%[re]ax,%[re]ax,1) |
| 400 | {0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 401 | // nopw %cs:0L(%[re]ax,%[re]ax,1) |
| 402 | {0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 403 | // nopl 0(%[re]ax,%[re]ax,1) |
| 404 | // nopw 0(%[re]ax,%[re]ax,1) |
| 405 | {0x0f, 0x1f, 0x44, 0x00, 0x00, |
| 406 | 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00}, |
| 407 | // nopw 0(%[re]ax,%[re]ax,1) |
| 408 | // nopw 0(%[re]ax,%[re]ax,1) |
| 409 | {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00, |
| 410 | 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00}, |
| 411 | // nopw 0(%[re]ax,%[re]ax,1) |
| 412 | // nopl 0L(%[re]ax) */ |
| 413 | {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00, |
| 414 | 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00}, |
| 415 | // nopl 0L(%[re]ax) |
| 416 | // nopl 0L(%[re]ax) |
| 417 | {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00, |
| 418 | 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00}, |
| 419 | // nopl 0L(%[re]ax) |
| 420 | // nopl 0L(%[re]ax,%[re]ax,1) |
| 421 | {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00, |
| 422 | 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00} |
| 423 | }; |
| 424 | |
| 425 | if (Count > 15) |
| 426 | return 0; |
| 427 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 428 | for (uint64_t i = 0; i < Count; i++) |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 429 | OW->Write8(uint8_t(Nops[Count - 1][i])); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 430 | |
| 431 | return Count; |
| 432 | } |
| 433 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 434 | /// WriteFragmentData - Write the \arg F data to the output file. |
| 435 | static void WriteFragmentData(const MCFragment &F, MCObjectWriter *OW) { |
| 436 | uint64_t Start = OW->getStream().tell(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 437 | (void) Start; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 438 | |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 439 | ++EmittedFragments; |
| 440 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 441 | // FIXME: Embed in fragments instead? |
| 442 | switch (F.getKind()) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 443 | case MCFragment::FT_Align: { |
| 444 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 445 | uint64_t Count = AF.getFileSize() / AF.getValueSize(); |
| 446 | |
| 447 | // FIXME: This error shouldn't actually occur (the front end should emit |
| 448 | // multiple .align directives to enforce the semantics it wants), but is |
| 449 | // severe enough that we want to report it. How to handle this? |
| 450 | if (Count * AF.getValueSize() != AF.getFileSize()) |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 451 | llvm_report_error("undefined .align directive, value size '" + |
| 452 | Twine(AF.getValueSize()) + |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 453 | "' is not a divisor of padding size '" + |
| 454 | Twine(AF.getFileSize()) + "'"); |
| 455 | |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 456 | // See if we are aligning with nops, and if so do that first to try to fill |
| 457 | // the Count bytes. Then if that did not fill any bytes or there are any |
| 458 | // bytes left to fill use the the Value and ValueSize to fill the rest. |
| 459 | if (AF.getEmitNops()) { |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 460 | uint64_t NopByteCount = WriteNopData(Count, OW); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 461 | Count -= NopByteCount; |
| 462 | } |
| 463 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 464 | for (uint64_t i = 0; i != Count; ++i) { |
| 465 | switch (AF.getValueSize()) { |
| 466 | default: |
| 467 | assert(0 && "Invalid size!"); |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 468 | case 1: OW->Write8 (uint8_t (AF.getValue())); break; |
| 469 | case 2: OW->Write16(uint16_t(AF.getValue())); break; |
| 470 | case 4: OW->Write32(uint32_t(AF.getValue())); break; |
| 471 | case 8: OW->Write64(uint64_t(AF.getValue())); break; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | break; |
| 475 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 476 | |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 477 | case MCFragment::FT_Data: { |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame^] | 478 | MCDataFragment &DF = cast<MCDataFragment>(F); |
| 479 | assert(DF.getFileSize() == DF.getContents().size() && "Invalid size!"); |
| 480 | OW->WriteBytes(DF.getContents().str()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 481 | break; |
Daniel Dunbar | 3a30b82 | 2010-02-13 09:28:15 +0000 | [diff] [blame] | 482 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 483 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 484 | case MCFragment::FT_Fill: { |
| 485 | MCFillFragment &FF = cast<MCFillFragment>(F); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 486 | for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) { |
| 487 | switch (FF.getValueSize()) { |
| 488 | default: |
| 489 | assert(0 && "Invalid size!"); |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 490 | case 1: OW->Write8 (uint8_t (FF.getValue())); break; |
| 491 | case 2: OW->Write16(uint16_t(FF.getValue())); break; |
| 492 | case 4: OW->Write32(uint32_t(FF.getValue())); break; |
| 493 | case 8: OW->Write64(uint64_t(FF.getValue())); break; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | break; |
| 497 | } |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 498 | |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame^] | 499 | case MCFragment::FT_Inst: |
| 500 | llvm_unreachable("unexpected inst fragment after lowering"); |
| 501 | break; |
| 502 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 503 | case MCFragment::FT_Org: { |
| 504 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 505 | |
| 506 | for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i) |
Daniel Dunbar | bdd9281 | 2010-03-19 09:28:55 +0000 | [diff] [blame] | 507 | OW->Write8(uint8_t(OF.getValue())); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 508 | |
| 509 | break; |
| 510 | } |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 511 | |
| 512 | case MCFragment::FT_ZeroFill: { |
| 513 | assert(0 && "Invalid zero fill fragment in concrete section!"); |
| 514 | break; |
| 515 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 518 | assert(OW->getStream().tell() - Start == F.getFileSize()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 521 | void MCAssembler::WriteSectionData(const MCSectionData *SD, |
| 522 | MCObjectWriter *OW) const { |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 523 | // Ignore virtual sections. |
Daniel Dunbar | cc5b84c | 2010-03-19 09:29:03 +0000 | [diff] [blame] | 524 | if (getBackend().isVirtualSection(SD->getSection())) { |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 525 | assert(SD->getFileSize() == 0); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 529 | uint64_t Start = OW->getStream().tell(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 530 | (void) Start; |
Daniel Dunbar | 7eb8519 | 2009-10-16 01:58:15 +0000 | [diff] [blame] | 531 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 532 | for (MCSectionData::const_iterator it = SD->begin(), |
| 533 | ie = SD->end(); it != ie; ++it) |
| 534 | WriteFragmentData(*it, OW); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 535 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 536 | // Add section padding. |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 537 | assert(SD->getFileSize() >= SD->getSize() && "Invalid section sizes!"); |
| 538 | OW->WriteZeros(SD->getFileSize() - SD->getSize()); |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 539 | |
Daniel Dunbar | 53b2338 | 2010-03-19 09:28:59 +0000 | [diff] [blame] | 540 | assert(OW->getStream().tell() - Start == SD->getFileSize()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 543 | void MCAssembler::Finish() { |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 544 | DEBUG_WITH_TYPE("mc-dump", { |
| 545 | llvm::errs() << "assembler backend - pre-layout\n--\n"; |
| 546 | dump(); }); |
| 547 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 548 | // Layout until everything fits. |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 549 | MCAsmLayout Layout(*this); |
| 550 | while (LayoutOnce(Layout)) |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 551 | continue; |
| 552 | |
| 553 | DEBUG_WITH_TYPE("mc-dump", { |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame^] | 554 | llvm::errs() << "assembler backend - post-relaxation\n--\n"; |
| 555 | dump(); }); |
| 556 | |
| 557 | // Finalize the layout, including fragment lowering. |
| 558 | FinishLayout(Layout); |
| 559 | |
| 560 | DEBUG_WITH_TYPE("mc-dump", { |
| 561 | llvm::errs() << "assembler backend - final-layout\n--\n"; |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 562 | dump(); }); |
| 563 | |
Daniel Dunbar | 1a9158c | 2010-03-19 10:43:26 +0000 | [diff] [blame] | 564 | llvm::OwningPtr<MCObjectWriter> Writer(getBackend().createObjectWriter(OS)); |
| 565 | if (!Writer) |
| 566 | llvm_report_error("unable to create object writer!"); |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 567 | |
| 568 | // Allow the object writer a chance to perform post-layout binding (for |
| 569 | // example, to set the index fields in the symbol data). |
Daniel Dunbar | 1a9158c | 2010-03-19 10:43:26 +0000 | [diff] [blame] | 570 | Writer->ExecutePostLayoutBinding(*this); |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 571 | |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 572 | // Evaluate and apply the fixups, generating relocation entries as necessary. |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 573 | for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) { |
| 574 | for (MCSectionData::iterator it2 = it->begin(), |
| 575 | ie2 = it->end(); it2 != ie2; ++it2) { |
| 576 | MCDataFragment *DF = dyn_cast<MCDataFragment>(it2); |
| 577 | if (!DF) |
| 578 | continue; |
| 579 | |
| 580 | for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(), |
| 581 | ie3 = DF->fixup_end(); it3 != ie3; ++it3) { |
| 582 | MCAsmFixup &Fixup = *it3; |
| 583 | |
| 584 | // Evaluate the fixup. |
| 585 | MCValue Target; |
| 586 | uint64_t FixedValue; |
| 587 | if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) { |
| 588 | // The fixup was unresolved, we need a relocation. Inform the object |
| 589 | // writer of the relocation, and give it an opportunity to adjust the |
| 590 | // fixup value if need be. |
Daniel Dunbar | b751418 | 2010-03-22 20:35:50 +0000 | [diff] [blame] | 591 | Writer->RecordRelocation(*this, DF, Fixup, Target, FixedValue); |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Daniel Dunbar | 87190c4 | 2010-03-19 09:28:12 +0000 | [diff] [blame] | 594 | getBackend().ApplyFixup(Fixup, *DF, FixedValue); |
Daniel Dunbar | b1e9894 | 2010-03-19 07:09:47 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
Daniel Dunbar | bacba99 | 2010-03-19 07:09:33 +0000 | [diff] [blame] | 599 | // Write the object file. |
Daniel Dunbar | 1a9158c | 2010-03-19 10:43:26 +0000 | [diff] [blame] | 600 | Writer->WriteObject(*this); |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 601 | OS.flush(); |
| 602 | } |
| 603 | |
Daniel Dunbar | 9d39e61 | 2010-03-22 21:49:41 +0000 | [diff] [blame] | 604 | bool MCAssembler::FixupNeedsRelaxation(const MCAsmFixup &Fixup, |
| 605 | const MCFragment *DF, |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 606 | const MCAsmLayout &Layout) const { |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 607 | // Currently we only need to relax X86::reloc_pcrel_1byte. |
| 608 | if (unsigned(Fixup.Kind) != X86::reloc_pcrel_1byte) |
| 609 | return false; |
| 610 | |
| 611 | // If we cannot resolve the fixup value, it requires relaxation. |
| 612 | MCValue Target; |
| 613 | uint64_t Value; |
| 614 | if (!EvaluateFixup(Layout, Fixup, DF, Target, Value)) |
| 615 | return true; |
| 616 | |
| 617 | // Otherwise, relax if the value is too big for a (signed) i8. |
| 618 | return int64_t(Value) != int64_t(int8_t(Value)); |
| 619 | } |
| 620 | |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 621 | bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) { |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 622 | // Layout the concrete sections and fragments. |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 623 | uint64_t Address = 0; |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 624 | MCSectionData *Prev = 0; |
| 625 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 626 | MCSectionData &SD = *it; |
| 627 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 628 | // Skip virtual sections. |
Daniel Dunbar | cc5b84c | 2010-03-19 09:29:03 +0000 | [diff] [blame] | 629 | if (getBackend().isVirtualSection(SD.getSection())) |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 630 | continue; |
| 631 | |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 632 | // Align this section if necessary by adding padding bytes to the previous |
| 633 | // section. |
| 634 | if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) { |
| 635 | assert(Prev && "Missing prev section!"); |
| 636 | Prev->setFileSize(Prev->getFileSize() + Pad); |
| 637 | Address += Pad; |
| 638 | } |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 639 | |
| 640 | // Layout the section fragments and its size. |
| 641 | SD.setAddress(Address); |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 642 | LayoutSection(SD, Layout); |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 643 | Address += SD.getFileSize(); |
Daniel Dunbar | edc670f | 2009-08-28 05:49:04 +0000 | [diff] [blame] | 644 | |
| 645 | Prev = &SD; |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 646 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 647 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 648 | // Layout the virtual sections. |
| 649 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 650 | MCSectionData &SD = *it; |
| 651 | |
Daniel Dunbar | cc5b84c | 2010-03-19 09:29:03 +0000 | [diff] [blame] | 652 | if (!getBackend().isVirtualSection(SD.getSection())) |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 653 | continue; |
| 654 | |
Daniel Dunbar | b2b4acd | 2010-03-08 22:03:42 +0000 | [diff] [blame] | 655 | // Align this section if necessary by adding padding bytes to the previous |
| 656 | // section. |
Daniel Dunbar | f8b8ad7 | 2010-03-09 01:12:20 +0000 | [diff] [blame] | 657 | if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) |
Daniel Dunbar | b2b4acd | 2010-03-08 22:03:42 +0000 | [diff] [blame] | 658 | Address += Pad; |
Daniel Dunbar | b2b4acd | 2010-03-08 22:03:42 +0000 | [diff] [blame] | 659 | |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 660 | SD.setAddress(Address); |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 661 | LayoutSection(SD, Layout); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 662 | Address += SD.getSize(); |
| 663 | } |
| 664 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 665 | // Scan the fixups in order and relax any that don't fit. |
| 666 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 667 | MCSectionData &SD = *it; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 668 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 669 | for (MCSectionData::iterator it2 = SD.begin(), |
| 670 | ie2 = SD.end(); it2 != ie2; ++it2) { |
| 671 | MCDataFragment *DF = dyn_cast<MCDataFragment>(it2); |
| 672 | if (!DF) |
| 673 | continue; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 674 | |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 675 | for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(), |
| 676 | ie3 = DF->fixup_end(); it3 != ie3; ++it3) { |
| 677 | MCAsmFixup &Fixup = *it3; |
| 678 | |
| 679 | // Check whether we need to relax this fixup. |
Daniel Dunbar | 8d39eb4 | 2010-03-22 20:35:35 +0000 | [diff] [blame] | 680 | if (!FixupNeedsRelaxation(Fixup, DF, Layout)) |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 681 | continue; |
| 682 | |
| 683 | // Relax the instruction. |
| 684 | // |
| 685 | // FIXME: This is a huge temporary hack which just looks for x86 |
| 686 | // branches; the only thing we need to relax on x86 is |
| 687 | // 'X86::reloc_pcrel_1byte'. Once we have MCInst fragments, this will be |
| 688 | // replaced by a TargetAsmBackend hook (most likely tblgen'd) to relax |
| 689 | // an individual MCInst. |
| 690 | SmallVectorImpl<char> &C = DF->getContents(); |
| 691 | uint64_t PrevOffset = Fixup.Offset; |
| 692 | unsigned Amt = 0; |
| 693 | |
| 694 | // jcc instructions |
| 695 | if (unsigned(C[Fixup.Offset-1]) >= 0x70 && |
| 696 | unsigned(C[Fixup.Offset-1]) <= 0x7f) { |
| 697 | C[Fixup.Offset] = C[Fixup.Offset-1] + 0x10; |
| 698 | C[Fixup.Offset-1] = char(0x0f); |
| 699 | ++Fixup.Offset; |
| 700 | Amt = 4; |
| 701 | |
| 702 | // jmp rel8 |
| 703 | } else if (C[Fixup.Offset-1] == char(0xeb)) { |
| 704 | C[Fixup.Offset-1] = char(0xe9); |
| 705 | Amt = 3; |
| 706 | |
| 707 | } else |
| 708 | llvm_unreachable("unknown 1 byte pcrel instruction!"); |
| 709 | |
| 710 | Fixup.Value = MCBinaryExpr::Create( |
| 711 | MCBinaryExpr::Sub, Fixup.Value, |
| 712 | MCConstantExpr::Create(3, getContext()), |
| 713 | getContext()); |
| 714 | C.insert(C.begin() + Fixup.Offset, Amt, char(0)); |
| 715 | Fixup.Kind = MCFixupKind(X86::reloc_pcrel_4byte); |
| 716 | |
| 717 | // Update the remaining fixups, which have slid. |
| 718 | // |
| 719 | // FIXME: This is bad for performance, but will be eliminated by the |
| 720 | // move to MCInst specific fragments. |
| 721 | ++it3; |
| 722 | for (; it3 != ie3; ++it3) |
| 723 | it3->Offset += Amt; |
| 724 | |
| 725 | // Update all the symbols for this fragment, which may have slid. |
| 726 | // |
| 727 | // FIXME: This is really really bad for performance, but will be |
| 728 | // eliminated by the move to MCInst specific fragments. |
| 729 | for (MCAssembler::symbol_iterator it = symbol_begin(), |
| 730 | ie = symbol_end(); it != ie; ++it) { |
| 731 | MCSymbolData &SD = *it; |
| 732 | |
| 733 | if (it->getFragment() != DF) |
| 734 | continue; |
| 735 | |
| 736 | if (SD.getOffset() > PrevOffset) |
| 737 | SD.setOffset(SD.getOffset() + Amt); |
| 738 | } |
| 739 | |
| 740 | // Restart layout. |
| 741 | // |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame^] | 742 | // FIXME-PERF: This is O(N^2), but will be eliminated once we have a |
| 743 | // smart MCAsmLayout object. |
Daniel Dunbar | f08fde4 | 2010-03-12 22:07:14 +0000 | [diff] [blame] | 744 | return true; |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | return false; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 750 | } |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 751 | |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame^] | 752 | void MCAssembler::FinishLayout(MCAsmLayout &Layout) { |
| 753 | // Lower out any instruction fragments, to simplify the fixup application and |
| 754 | // output. |
| 755 | // |
| 756 | // FIXME-PERF: We don't have to do this, but the assumption is that it is |
| 757 | // cheap (we will mostly end up eliminating fragments and appending on to data |
| 758 | // fragments), so the extra complexity downstream isn't worth it. Evaluate |
| 759 | // this assumption. |
| 760 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 761 | MCSectionData &SD = *it; |
| 762 | |
| 763 | for (MCSectionData::iterator it2 = SD.begin(), |
| 764 | ie2 = SD.end(); it2 != ie2; ++it2) { |
| 765 | MCInstFragment *IF = dyn_cast<MCInstFragment>(it2); |
| 766 | if (!IF) |
| 767 | continue; |
| 768 | |
| 769 | // Create a new data fragment for the instruction. |
| 770 | // |
| 771 | // FIXME: Reuse previous data fragment if possible. |
| 772 | MCDataFragment *DF = new MCDataFragment(); |
| 773 | SD.getFragmentList().insert(it2, DF); |
| 774 | |
| 775 | // Update the data fragments layout data. |
| 776 | DF->setOffset(IF->getOffset()); |
| 777 | DF->setFileSize(IF->getInstSize()); |
| 778 | |
| 779 | // Encode the final instruction. |
| 780 | SmallVector<MCFixup, 4> Fixups; |
| 781 | raw_svector_ostream VecOS(DF->getContents()); |
| 782 | getEmitter().EncodeInstruction(IF->getInst(), VecOS, Fixups); |
| 783 | |
| 784 | // Copy over the fixups. |
| 785 | // |
| 786 | // FIXME-PERF: Encode fixups directly into the data fragment as well. |
| 787 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 788 | MCFixup &F = Fixups[i]; |
| 789 | DF->addFixup(MCAsmFixup(DF->getContents().size()+F.getOffset(), |
| 790 | *F.getValue(), F.getKind())); |
| 791 | } |
| 792 | |
| 793 | // Delete the instruction fragment and update the iterator. |
| 794 | SD.getFragmentList().erase(IF); |
| 795 | it2 = DF; |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 800 | // Debugging methods |
| 801 | |
| 802 | namespace llvm { |
| 803 | |
| 804 | raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) { |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 805 | OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value |
| 806 | << " Kind:" << AF.Kind << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 807 | return OS; |
| 808 | } |
| 809 | |
| 810 | } |
| 811 | |
| 812 | void MCFragment::dump() { |
| 813 | raw_ostream &OS = llvm::errs(); |
| 814 | |
| 815 | OS << "<MCFragment " << (void*) this << " Offset:" << Offset |
| 816 | << " FileSize:" << FileSize; |
| 817 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 818 | OS << ">"; |
| 819 | } |
| 820 | |
| 821 | void MCAlignFragment::dump() { |
| 822 | raw_ostream &OS = llvm::errs(); |
| 823 | |
| 824 | OS << "<MCAlignFragment "; |
| 825 | this->MCFragment::dump(); |
| 826 | OS << "\n "; |
| 827 | OS << " Alignment:" << getAlignment() |
| 828 | << " Value:" << getValue() << " ValueSize:" << getValueSize() |
| 829 | << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">"; |
| 830 | } |
| 831 | |
| 832 | void MCDataFragment::dump() { |
| 833 | raw_ostream &OS = llvm::errs(); |
| 834 | |
| 835 | OS << "<MCDataFragment "; |
| 836 | this->MCFragment::dump(); |
| 837 | OS << "\n "; |
| 838 | OS << " Contents:["; |
| 839 | for (unsigned i = 0, e = getContents().size(); i != e; ++i) { |
| 840 | if (i) OS << ","; |
| 841 | OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); |
| 842 | } |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 843 | OS << "] (" << getContents().size() << " bytes)"; |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 844 | |
| 845 | if (!getFixups().empty()) { |
| 846 | OS << ",\n "; |
| 847 | OS << " Fixups:["; |
| 848 | for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) { |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 849 | if (it != fixup_begin()) OS << ",\n "; |
Daniel Dunbar | 0bcf074 | 2010-02-13 09:28:43 +0000 | [diff] [blame] | 850 | OS << *it; |
| 851 | } |
| 852 | OS << "]"; |
| 853 | } |
| 854 | |
| 855 | OS << ">"; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | void MCFillFragment::dump() { |
| 859 | raw_ostream &OS = llvm::errs(); |
| 860 | |
| 861 | OS << "<MCFillFragment "; |
| 862 | this->MCFragment::dump(); |
| 863 | OS << "\n "; |
| 864 | OS << " Value:" << getValue() << " ValueSize:" << getValueSize() |
| 865 | << " Count:" << getCount() << ">"; |
| 866 | } |
| 867 | |
Daniel Dunbar | 3f4dcd9 | 2010-03-22 23:16:48 +0000 | [diff] [blame^] | 868 | void MCInstFragment::dump() { |
| 869 | raw_ostream &OS = llvm::errs(); |
| 870 | |
| 871 | OS << "<MCInstFragment "; |
| 872 | this->MCFragment::dump(); |
| 873 | OS << "\n "; |
| 874 | OS << " Inst:"; |
| 875 | getInst().dump_pretty(OS); |
| 876 | OS << ">"; |
| 877 | } |
| 878 | |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 879 | void MCOrgFragment::dump() { |
| 880 | raw_ostream &OS = llvm::errs(); |
| 881 | |
| 882 | OS << "<MCOrgFragment "; |
| 883 | this->MCFragment::dump(); |
| 884 | OS << "\n "; |
| 885 | OS << " Offset:" << getOffset() << " Value:" << getValue() << ">"; |
| 886 | } |
| 887 | |
| 888 | void MCZeroFillFragment::dump() { |
| 889 | raw_ostream &OS = llvm::errs(); |
| 890 | |
| 891 | OS << "<MCZeroFillFragment "; |
| 892 | this->MCFragment::dump(); |
| 893 | OS << "\n "; |
| 894 | OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">"; |
| 895 | } |
| 896 | |
| 897 | void MCSectionData::dump() { |
| 898 | raw_ostream &OS = llvm::errs(); |
| 899 | |
| 900 | OS << "<MCSectionData"; |
| 901 | OS << " Alignment:" << getAlignment() << " Address:" << Address |
| 902 | << " Size:" << Size << " FileSize:" << FileSize |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 903 | << " Fragments:[\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 904 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 905 | if (it != begin()) OS << ",\n "; |
| 906 | it->dump(); |
| 907 | } |
| 908 | OS << "]>"; |
| 909 | } |
| 910 | |
| 911 | void MCSymbolData::dump() { |
| 912 | raw_ostream &OS = llvm::errs(); |
| 913 | |
| 914 | OS << "<MCSymbolData Symbol:" << getSymbol() |
| 915 | << " Fragment:" << getFragment() << " Offset:" << getOffset() |
| 916 | << " Flags:" << getFlags() << " Index:" << getIndex(); |
| 917 | if (isCommon()) |
| 918 | OS << " (common, size:" << getCommonSize() |
| 919 | << " align: " << getCommonAlignment() << ")"; |
| 920 | if (isExternal()) |
| 921 | OS << " (external)"; |
| 922 | if (isPrivateExtern()) |
| 923 | OS << " (private extern)"; |
| 924 | OS << ">"; |
| 925 | } |
| 926 | |
| 927 | void MCAssembler::dump() { |
| 928 | raw_ostream &OS = llvm::errs(); |
| 929 | |
| 930 | OS << "<MCAssembler\n"; |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 931 | OS << " Sections:[\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 932 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 933 | if (it != begin()) OS << ",\n "; |
| 934 | it->dump(); |
| 935 | } |
| 936 | OS << "],\n"; |
| 937 | OS << " Symbols:["; |
| 938 | |
| 939 | for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { |
Daniel Dunbar | 45aefff | 2010-03-09 01:12:23 +0000 | [diff] [blame] | 940 | if (it != symbol_begin()) OS << ",\n "; |
Daniel Dunbar | b7c3a4b | 2010-02-13 09:28:03 +0000 | [diff] [blame] | 941 | it->dump(); |
| 942 | } |
| 943 | OS << "]>\n"; |
| 944 | } |