Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 1 | //===-- llvm/MC/WinCOFFStreamer.cpp -----------------------------*- C++ -*-===// |
| 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 | // |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 10 | // This file contains an implementation of a Windows COFF object file streamer. |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAsmBackend.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAsmLayout.h" |
| 16 | #include "llvm/MC/MCAssembler.h" |
| 17 | #include "llvm/MC/MCCodeEmitter.h" |
| 18 | #include "llvm/MC/MCContext.h" |
| 19 | #include "llvm/MC/MCExpr.h" |
Rafael Espindola | e308c0c | 2014-01-23 22:49:25 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCObjectFileInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCObjectStreamer.h" |
| 22 | #include "llvm/MC/MCSection.h" |
| 23 | #include "llvm/MC/MCSectionCOFF.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCStreamer.h" |
Pete Cooper | ad9f9c3 | 2015-06-08 17:17:12 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSymbolCOFF.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCValue.h" |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCWinCOFFStreamer.h" |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 28 | #include "llvm/Support/COFF.h" |
| 29 | #include "llvm/Support/Debug.h" |
| 30 | #include "llvm/Support/ErrorHandling.h" |
Saleem Abdulrasool | 64d491e | 2014-10-07 19:37:57 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 32 | #include "llvm/Support/TargetRegistry.h" |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | d3df3d3 | 2011-12-17 01:14:52 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Chandler Carruth | f58e376 | 2014-04-22 03:04:17 +0000 | [diff] [blame] | 37 | #define DEBUG_TYPE "WinCOFFStreamer" |
| 38 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 39 | namespace llvm { |
| 40 | MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB, |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 41 | MCCodeEmitter &CE, raw_pwrite_stream &OS) |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 42 | : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(nullptr) {} |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 43 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 44 | void MCWinCOFFStreamer::EmitInstToData(const MCInst &Inst, |
| 45 | const MCSubtargetInfo &STI) { |
| 46 | MCDataFragment *DF = getOrCreateDataFragment(); |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 47 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 48 | SmallVector<MCFixup, 4> Fixups; |
| 49 | SmallString<256> Code; |
| 50 | raw_svector_ostream VecOS(Code); |
Jim Grosbach | 91df21f | 2015-05-15 19:13:16 +0000 | [diff] [blame] | 51 | getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI); |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 52 | |
| 53 | // Add the fixups and data. |
| 54 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 55 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
| 56 | DF->getFixups().push_back(Fixups[i]); |
| 57 | } |
| 58 | |
| 59 | DF->getContents().append(Code.begin(), Code.end()); |
| 60 | } |
| 61 | |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 62 | void MCWinCOFFStreamer::InitSections(bool NoExecStack) { |
Rafael Espindola | f281253 | 2014-01-24 02:28:11 +0000 | [diff] [blame] | 63 | // FIXME: this is identical to the ELF one. |
| 64 | // This emulates the same behavior of GNU as. This makes it easier |
| 65 | // to compare the output as the major sections are in the same order. |
| 66 | SwitchSection(getContext().getObjectFileInfo()->getTextSection()); |
Rafael Espindola | 7b51496 | 2014-02-04 18:34:04 +0000 | [diff] [blame] | 67 | EmitCodeAlignment(4); |
Rafael Espindola | f281253 | 2014-01-24 02:28:11 +0000 | [diff] [blame] | 68 | |
| 69 | SwitchSection(getContext().getObjectFileInfo()->getDataSection()); |
Rafael Espindola | 7b51496 | 2014-02-04 18:34:04 +0000 | [diff] [blame] | 70 | EmitCodeAlignment(4); |
Rafael Espindola | f281253 | 2014-01-24 02:28:11 +0000 | [diff] [blame] | 71 | |
| 72 | SwitchSection(getContext().getObjectFileInfo()->getBSSSection()); |
Rafael Espindola | 7b51496 | 2014-02-04 18:34:04 +0000 | [diff] [blame] | 73 | EmitCodeAlignment(4); |
Rafael Espindola | f281253 | 2014-01-24 02:28:11 +0000 | [diff] [blame] | 74 | |
| 75 | SwitchSection(getContext().getObjectFileInfo()->getTextSection()); |
Rafael Espindola | f667d92 | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 76 | } |
| 77 | |
David Majnemer | 230bbfb | 2016-07-08 21:54:16 +0000 | [diff] [blame] | 78 | void MCWinCOFFStreamer::EmitLabel(MCSymbol *S) { |
| 79 | auto *Symbol = cast<MCSymbolCOFF>(S); |
Rafael Espindola | 1679580 | 2010-11-28 16:22:59 +0000 | [diff] [blame] | 80 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 81 | MCObjectStreamer::EmitLabel(Symbol); |
Rafael Espindola | 1679580 | 2010-11-28 16:22:59 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 84 | void MCWinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 85 | llvm_unreachable("not implemented"); |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 88 | void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) { |
Jim Grosbach | 5a2c68d | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 89 | llvm_unreachable("not implemented"); |
| 90 | } |
| 91 | |
David Majnemer | 230bbfb | 2016-07-08 21:54:16 +0000 | [diff] [blame] | 92 | bool MCWinCOFFStreamer::EmitSymbolAttribute(MCSymbol *S, |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 93 | MCSymbolAttr Attribute) { |
David Majnemer | 230bbfb | 2016-07-08 21:54:16 +0000 | [diff] [blame] | 94 | auto *Symbol = cast<MCSymbolCOFF>(S); |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 95 | getAssembler().registerSymbol(*Symbol); |
Saleem Abdulrasool | 8e4fee0 | 2014-04-27 03:48:01 +0000 | [diff] [blame] | 96 | |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 97 | switch (Attribute) { |
Saleem Abdulrasool | 8e4fee0 | 2014-04-27 03:48:01 +0000 | [diff] [blame] | 98 | default: return false; |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 99 | case MCSA_WeakReference: |
Saleem Abdulrasool | 8e4fee0 | 2014-04-27 03:48:01 +0000 | [diff] [blame] | 100 | case MCSA_Weak: |
David Majnemer | 230bbfb | 2016-07-08 21:54:16 +0000 | [diff] [blame] | 101 | Symbol->setIsWeakExternal(); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 102 | Symbol->setExternal(true); |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 103 | break; |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 104 | case MCSA_Global: |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 105 | Symbol->setExternal(true); |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 106 | break; |
Lang Hames | 6d22d8a | 2016-04-08 17:38:51 +0000 | [diff] [blame] | 107 | case MCSA_AltEntry: |
Lang Hames | f9033bb | 2016-04-11 18:33:45 +0000 | [diff] [blame] | 108 | llvm_unreachable("COFF doesn't support the .alt_entry attribute"); |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 109 | } |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 110 | |
| 111 | return true; |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 114 | void MCWinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 115 | llvm_unreachable("not implemented"); |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 116 | } |
| 117 | |
David Majnemer | 230bbfb | 2016-07-08 21:54:16 +0000 | [diff] [blame] | 118 | void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *S) { |
| 119 | auto *Symbol = cast<MCSymbolCOFF>(S); |
Saleem Abdulrasool | 6663f8f | 2014-05-22 02:18:10 +0000 | [diff] [blame] | 120 | if (CurSymbol) |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 121 | Error("starting a new symbol definition without completing the " |
| 122 | "previous one"); |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 123 | CurSymbol = Symbol; |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 126 | void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) { |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 127 | if (!CurSymbol) { |
| 128 | Error("storage class specified outside of symbol definition"); |
| 129 | return; |
| 130 | } |
Saleem Abdulrasool | 6663f8f | 2014-05-22 02:18:10 +0000 | [diff] [blame] | 131 | |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 132 | if (StorageClass & ~COFF::SSC_Invalid) { |
| 133 | Error("storage class value '" + Twine(StorageClass) + |
Saleem Abdulrasool | 6663f8f | 2014-05-22 02:18:10 +0000 | [diff] [blame] | 134 | "' out of range"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 135 | return; |
| 136 | } |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 137 | |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 138 | getAssembler().registerSymbol(*CurSymbol); |
Pete Cooper | 6bf1f30 | 2015-06-08 17:17:19 +0000 | [diff] [blame] | 139 | cast<MCSymbolCOFF>(CurSymbol)->setClass((uint16_t)StorageClass); |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 142 | void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) { |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 143 | if (!CurSymbol) { |
| 144 | Error("symbol type specified outside of a symbol definition"); |
| 145 | return; |
| 146 | } |
Saleem Abdulrasool | 6663f8f | 2014-05-22 02:18:10 +0000 | [diff] [blame] | 147 | |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 148 | if (Type & ~0xffff) { |
| 149 | Error("type value '" + Twine(Type) + "' out of range"); |
| 150 | return; |
| 151 | } |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 152 | |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 153 | getAssembler().registerSymbol(*CurSymbol); |
Pete Cooper | ad9f9c3 | 2015-06-08 17:17:12 +0000 | [diff] [blame] | 154 | cast<MCSymbolCOFF>(CurSymbol)->setType((uint16_t)Type); |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 157 | void MCWinCOFFStreamer::EndCOFFSymbolDef() { |
Saleem Abdulrasool | 6663f8f | 2014-05-22 02:18:10 +0000 | [diff] [blame] | 158 | if (!CurSymbol) |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 159 | Error("ending symbol definition without starting one"); |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 160 | CurSymbol = nullptr; |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 161 | } |
| 162 | |
David Majnemer | 4eecd30 | 2015-05-30 04:56:02 +0000 | [diff] [blame] | 163 | void MCWinCOFFStreamer::EmitCOFFSafeSEH(MCSymbol const *Symbol) { |
David Majnemer | 279306c | 2015-06-01 07:34:26 +0000 | [diff] [blame] | 164 | // SafeSEH is a feature specific to 32-bit x86. It does not exist (and is |
| 165 | // unnecessary) on all platforms which use table-based exception dispatch. |
| 166 | if (getContext().getObjectFileInfo()->getTargetTriple().getArch() != |
| 167 | Triple::x86) |
| 168 | return; |
| 169 | |
Reid Kleckner | 2bc93ca | 2015-06-10 01:02:30 +0000 | [diff] [blame] | 170 | const MCSymbolCOFF *CSymbol = cast<MCSymbolCOFF>(Symbol); |
| 171 | if (CSymbol->isSafeSEH()) |
David Majnemer | 4eecd30 | 2015-05-30 04:56:02 +0000 | [diff] [blame] | 172 | return; |
| 173 | |
| 174 | MCSection *SXData = getContext().getObjectFileInfo()->getSXDataSection(); |
| 175 | getAssembler().registerSection(*SXData); |
| 176 | if (SXData->getAlignment() < 4) |
| 177 | SXData->setAlignment(4); |
| 178 | |
| 179 | new MCSafeSEHFragment(Symbol, SXData); |
| 180 | |
| 181 | getAssembler().registerSymbol(*Symbol); |
Reid Kleckner | 2bc93ca | 2015-06-10 01:02:30 +0000 | [diff] [blame] | 182 | CSymbol->setIsSafeSEH(); |
| 183 | |
| 184 | // The Microsoft linker requires that the symbol type of a handler be |
| 185 | // function. Go ahead and oblige it here. |
| 186 | CSymbol->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION |
| 187 | << COFF::SCT_COMPLEX_TYPE_SHIFT); |
David Majnemer | 4eecd30 | 2015-05-30 04:56:02 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 190 | void MCWinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) { |
Timur Iskhodzhanov | c1fb2d6 | 2013-12-20 18:15:00 +0000 | [diff] [blame] | 191 | MCDataFragment *DF = getOrCreateDataFragment(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 192 | const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext()); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 193 | MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2); |
Saleem Abdulrasool | 8e4fee0 | 2014-04-27 03:48:01 +0000 | [diff] [blame] | 194 | DF->getFixups().push_back(Fixup); |
Timur Iskhodzhanov | 5fcaeeb | 2014-10-08 18:01:49 +0000 | [diff] [blame] | 195 | DF->getContents().resize(DF->getContents().size() + 2, 0); |
Timur Iskhodzhanov | c1fb2d6 | 2013-12-20 18:15:00 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Keno Fischer | f7d84ee | 2017-01-02 03:00:19 +0000 | [diff] [blame] | 198 | void MCWinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol, |
| 199 | uint64_t Offset) { |
Rafael Espindola | d3df3d3 | 2011-12-17 01:14:52 +0000 | [diff] [blame] | 200 | MCDataFragment *DF = getOrCreateDataFragment(); |
Keno Fischer | f7d84ee | 2017-01-02 03:00:19 +0000 | [diff] [blame] | 201 | // Create Symbol A for the relocation relative reference. |
| 202 | const MCExpr *MCE = MCSymbolRefExpr::create(Symbol, getContext()); |
| 203 | // Add the constant offset, if given. |
| 204 | if (Offset) |
| 205 | MCE = MCBinaryExpr::createAdd( |
| 206 | MCE, MCConstantExpr::create(Offset, getContext()), getContext()); |
| 207 | // Build the secrel32 relocation. |
| 208 | MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_SecRel_4); |
| 209 | // Record the relocation. |
Saleem Abdulrasool | 8e4fee0 | 2014-04-27 03:48:01 +0000 | [diff] [blame] | 210 | DF->getFixups().push_back(Fixup); |
Keno Fischer | f7d84ee | 2017-01-02 03:00:19 +0000 | [diff] [blame] | 211 | // Emit 4 bytes (zeros) to the object file. |
Rafael Espindola | d3df3d3 | 2011-12-17 01:14:52 +0000 | [diff] [blame] | 212 | DF->getContents().resize(DF->getContents().size() + 4, 0); |
| 213 | } |
| 214 | |
David Majnemer | 230bbfb | 2016-07-08 21:54:16 +0000 | [diff] [blame] | 215 | void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size, |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 216 | unsigned ByteAlignment) { |
David Majnemer | 230bbfb | 2016-07-08 21:54:16 +0000 | [diff] [blame] | 217 | auto *Symbol = cast<MCSymbolCOFF>(S); |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 218 | |
David Majnemer | 48227a3 | 2014-09-21 09:18:07 +0000 | [diff] [blame] | 219 | const Triple &T = getContext().getObjectFileInfo()->getTargetTriple(); |
David Majnemer | d758604 | 2014-10-08 06:38:53 +0000 | [diff] [blame] | 220 | if (T.isKnownWindowsMSVCEnvironment()) { |
David Majnemer | 48227a3 | 2014-09-21 09:18:07 +0000 | [diff] [blame] | 221 | if (ByteAlignment > 32) |
| 222 | report_fatal_error("alignment is limited to 32-bytes"); |
Saleem Abdulrasool | 64d491e | 2014-10-07 19:37:57 +0000 | [diff] [blame] | 223 | |
David Majnemer | d758604 | 2014-10-08 06:38:53 +0000 | [diff] [blame] | 224 | // Round size up to alignment so that we will honor the alignment request. |
| 225 | Size = std::max(Size, static_cast<uint64_t>(ByteAlignment)); |
| 226 | } |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 227 | |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 228 | getAssembler().registerSymbol(*Symbol); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 229 | Symbol->setExternal(true); |
Rafael Espindola | 1467250 | 2015-05-29 17:48:04 +0000 | [diff] [blame] | 230 | Symbol->setCommon(Size, ByteAlignment); |
Saleem Abdulrasool | 64d491e | 2014-10-07 19:37:57 +0000 | [diff] [blame] | 231 | |
| 232 | if (!T.isKnownWindowsMSVCEnvironment() && ByteAlignment > 1) { |
| 233 | SmallString<128> Directive; |
| 234 | raw_svector_ostream OS(Directive); |
| 235 | const MCObjectFileInfo *MFI = getContext().getObjectFileInfo(); |
| 236 | |
| 237 | OS << " -aligncomm:\"" << Symbol->getName() << "\"," |
| 238 | << Log2_32_Ceil(ByteAlignment); |
Saleem Abdulrasool | 64d491e | 2014-10-07 19:37:57 +0000 | [diff] [blame] | 239 | |
| 240 | PushSection(); |
| 241 | SwitchSection(MFI->getDrectveSection()); |
| 242 | EmitBytes(Directive); |
| 243 | PopSection(); |
| 244 | } |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 245 | } |
| 246 | |
David Majnemer | 230bbfb | 2016-07-08 21:54:16 +0000 | [diff] [blame] | 247 | void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size, |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 248 | unsigned ByteAlignment) { |
David Majnemer | 230bbfb | 2016-07-08 21:54:16 +0000 | [diff] [blame] | 249 | auto *Symbol = cast<MCSymbolCOFF>(S); |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 250 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 251 | MCSection *Section = getContext().getObjectFileInfo()->getBSSSection(); |
Rafael Espindola | bb9a71c | 2015-05-26 15:07:25 +0000 | [diff] [blame] | 252 | getAssembler().registerSection(*Section); |
Rafael Espindola | 967d6a6 | 2015-05-21 21:02:35 +0000 | [diff] [blame] | 253 | if (Section->getAlignment() < ByteAlignment) |
| 254 | Section->setAlignment(ByteAlignment); |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 255 | |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 256 | getAssembler().registerSymbol(*Symbol); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 257 | Symbol->setExternal(false); |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 258 | |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 259 | if (ByteAlignment != 1) |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 260 | new MCAlignFragment(ByteAlignment, /*Value=*/0, /*ValueSize=*/0, |
Rafael Espindola | bb9a71c | 2015-05-26 15:07:25 +0000 | [diff] [blame] | 261 | ByteAlignment, Section); |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 262 | |
Rafael Espindola | 8324c2f | 2015-05-25 23:48:22 +0000 | [diff] [blame] | 263 | MCFillFragment *Fragment = new MCFillFragment( |
Rafael Espindola | 1a7e8b4 | 2016-01-19 16:57:08 +0000 | [diff] [blame] | 264 | /*Value=*/0, Size, Section); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 265 | Symbol->setFragment(Fragment); |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 268 | void MCWinCOFFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, |
| 269 | uint64_t Size, unsigned ByteAlignment) { |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 270 | llvm_unreachable("not implemented"); |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 273 | void MCWinCOFFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, |
| 274 | uint64_t Size, unsigned ByteAlignment) { |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 275 | llvm_unreachable("not implemented"); |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 278 | void MCWinCOFFStreamer::EmitFileDirective(StringRef Filename) { |
Saleem Abdulrasool | a2bf05a | 2014-04-16 04:15:32 +0000 | [diff] [blame] | 279 | getAssembler().addFileName(Filename); |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Rafael Espindola | 5645bad | 2013-10-16 01:05:45 +0000 | [diff] [blame] | 282 | // TODO: Implement this if you want to emit .comment section in COFF obj files. |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 283 | void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) { |
Saleem Abdulrasool | 8e4fee0 | 2014-04-27 03:48:01 +0000 | [diff] [blame] | 284 | llvm_unreachable("not implemented"); |
Rafael Espindola | 5645bad | 2013-10-16 01:05:45 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Saleem Abdulrasool | 7206a52 | 2014-06-29 01:52:01 +0000 | [diff] [blame] | 287 | void MCWinCOFFStreamer::EmitWinEHHandlerData() { |
Saleem Abdulrasool | a8b1f72 | 2014-04-27 03:48:12 +0000 | [diff] [blame] | 288 | llvm_unreachable("not implemented"); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Saleem Abdulrasool | cf1a29f | 2014-04-27 03:48:05 +0000 | [diff] [blame] | 291 | void MCWinCOFFStreamer::FinishImpl() { |
Rafael Espindola | 0708209 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 292 | MCObjectStreamer::FinishImpl(); |
Chris Lattner | 56725be | 2010-07-11 22:05:00 +0000 | [diff] [blame] | 293 | } |
Saleem Abdulrasool | 6663f8f | 2014-05-22 02:18:10 +0000 | [diff] [blame] | 294 | |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 295 | void MCWinCOFFStreamer::Error(const Twine &Msg) const { |
| 296 | getContext().reportError(SMLoc(), Msg); |
Saleem Abdulrasool | 6663f8f | 2014-05-22 02:18:10 +0000 | [diff] [blame] | 297 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 298 | } |
Saleem Abdulrasool | 8e4fee0 | 2014-04-27 03:48:01 +0000 | [diff] [blame] | 299 | |