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