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