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