Coverage Report

Created: 2017-06-21 23:44

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/MC/WinCOFFStreamer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- llvm/MC/WinCOFFStreamer.cpp ----------------------------------------===//
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
//
10
// This file contains an implementation of a Windows COFF object file streamer.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/ADT/SmallString.h"
15
#include "llvm/ADT/SmallVector.h"
16
#include "llvm/ADT/Triple.h"
17
#include "llvm/ADT/Twine.h"
18
#include "llvm/BinaryFormat/COFF.h"
19
#include "llvm/MC/MCAsmBackend.h"
20
#include "llvm/MC/MCAssembler.h"
21
#include "llvm/MC/MCCodeEmitter.h"
22
#include "llvm/MC/MCContext.h"
23
#include "llvm/MC/MCExpr.h"
24
#include "llvm/MC/MCFixup.h"
25
#include "llvm/MC/MCFragment.h"
26
#include "llvm/MC/MCObjectFileInfo.h"
27
#include "llvm/MC/MCObjectStreamer.h"
28
#include "llvm/MC/MCSection.h"
29
#include "llvm/MC/MCSymbolCOFF.h"
30
#include "llvm/MC/MCWinCOFFStreamer.h"
31
#include "llvm/Support/Casting.h"
32
#include "llvm/Support/ErrorHandling.h"
33
#include "llvm/Support/MathExtras.h"
34
#include "llvm/Support/SMLoc.h"
35
#include "llvm/Support/raw_ostream.h"
36
#include <algorithm>
37
#include <cassert>
38
#include <cstdint>
39
40
using namespace llvm;
41
42
#define DEBUG_TYPE "WinCOFFStreamer"
43
44
MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
45
                                     MCCodeEmitter &CE, raw_pwrite_stream &OS)
46
254
    : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(nullptr) {}
47
48
void MCWinCOFFStreamer::EmitInstToData(const MCInst &Inst,
49
1.47k
                                       const MCSubtargetInfo &STI) {
50
1.47k
  MCDataFragment *DF = getOrCreateDataFragment();
51
1.47k
52
1.47k
  SmallVector<MCFixup, 4> Fixups;
53
1.47k
  SmallString<256> Code;
54
1.47k
  raw_svector_ostream VecOS(Code);
55
1.47k
  getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
56
1.47k
57
1.47k
  // Add the fixups and data.
58
1.75k
  for (unsigned i = 0, e = Fixups.size(); 
i != e1.75k
;
++i277
)
{277
59
277
    Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
60
277
    DF->getFixups().push_back(Fixups[i]);
61
277
  }
62
1.47k
63
1.47k
  DF->getContents().append(Code.begin(), Code.end());
64
1.47k
}
65
66
254
void MCWinCOFFStreamer::InitSections(bool NoExecStack) {
67
254
  // FIXME: this is identical to the ELF one.
68
254
  // This emulates the same behavior of GNU as. This makes it easier
69
254
  // to compare the output as the major sections are in the same order.
70
254
  SwitchSection(getContext().getObjectFileInfo()->getTextSection());
71
254
  EmitCodeAlignment(4);
72
254
73
254
  SwitchSection(getContext().getObjectFileInfo()->getDataSection());
74
254
  EmitCodeAlignment(4);
75
254
76
254
  SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
77
254
  EmitCodeAlignment(4);
78
254
79
254
  SwitchSection(getContext().getObjectFileInfo()->getTextSection());
80
254
}
81
82
3.55k
void MCWinCOFFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc) {
83
3.55k
  auto *Symbol = cast<MCSymbolCOFF>(S);
84
3.55k
  MCObjectStreamer::EmitLabel(Symbol, Loc);
85
3.55k
}
86
87
0
void MCWinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
88
0
  llvm_unreachable("not implemented");
89
0
}
90
91
0
void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
92
0
  llvm_unreachable("not implemented");
93
0
}
94
95
bool MCWinCOFFStreamer::EmitSymbolAttribute(MCSymbol *S,
96
460
                                            MCSymbolAttr Attribute) {
97
460
  auto *Symbol = cast<MCSymbolCOFF>(S);
98
460
  getAssembler().registerSymbol(*Symbol);
99
460
100
460
  switch (Attribute) {
101
2
  default: return false;
102
8
  case MCSA_WeakReference:
103
8
  case MCSA_Weak:
104
8
    Symbol->setIsWeakExternal();
105
8
    Symbol->setExternal(true);
106
8
    break;
107
451
  case MCSA_Global:
108
451
    Symbol->setExternal(true);
109
451
    break;
110
0
  case MCSA_AltEntry:
111
0
    llvm_unreachable("COFF doesn't support the .alt_entry attribute");
112
460
  }
113
460
114
459
  return true;
115
460
}
116
117
0
void MCWinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
118
0
  llvm_unreachable("not implemented");
119
0
}
120
121
285
void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *S) {
122
285
  auto *Symbol = cast<MCSymbolCOFF>(S);
123
285
  if (CurSymbol)
124
2
    Error("starting a new symbol definition without completing the "
125
2
          "previous one");
126
285
  CurSymbol = Symbol;
127
285
}
128
129
283
void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
130
283
  if (
!CurSymbol283
)
{2
131
2
    Error("storage class specified outside of symbol definition");
132
2
    return;
133
2
  }
134
283
135
281
  
if (281
StorageClass & ~COFF::SSC_Invalid281
)
{2
136
2
    Error("storage class value '" + Twine(StorageClass) +
137
2
               "' out of range");
138
2
    return;
139
2
  }
140
281
141
279
  getAssembler().registerSymbol(*CurSymbol);
142
279
  cast<MCSymbolCOFF>(CurSymbol)->setClass((uint16_t)StorageClass);
143
279
}
144
145
284
void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) {
146
284
  if (
!CurSymbol284
)
{2
147
2
    Error("symbol type specified outside of a symbol definition");
148
2
    return;
149
2
  }
150
284
151
282
  
if (282
Type & ~0xffff282
)
{1
152
1
    Error("type value '" + Twine(Type) + "' out of range");
153
1
    return;
154
1
  }
155
282
156
281
  getAssembler().registerSymbol(*CurSymbol);
157
281
  cast<MCSymbolCOFF>(CurSymbol)->setType((uint16_t)Type);
158
281
}
159
160
285
void MCWinCOFFStreamer::EndCOFFSymbolDef() {
161
285
  if (!CurSymbol)
162
2
    Error("ending symbol definition without starting one");
163
285
  CurSymbol = nullptr;
164
285
}
165
166
0
void MCWinCOFFStreamer::EmitCOFFSafeSEH(MCSymbol const *Symbol) {
167
0
  // SafeSEH is a feature specific to 32-bit x86.  It does not exist (and is
168
0
  // unnecessary) on all platforms which use table-based exception dispatch.
169
0
  if (getContext().getObjectFileInfo()->getTargetTriple().getArch() !=
170
0
      Triple::x86)
171
0
    return;
172
0
173
0
  const MCSymbolCOFF *CSymbol = cast<MCSymbolCOFF>(Symbol);
174
0
  if (CSymbol->isSafeSEH())
175
0
    return;
176
0
177
0
  MCSection *SXData = getContext().getObjectFileInfo()->getSXDataSection();
178
0
  getAssembler().registerSection(*SXData);
179
0
  if (SXData->getAlignment() < 4)
180
0
    SXData->setAlignment(4);
181
0
182
0
  new MCSafeSEHFragment(Symbol, SXData);
183
0
184
0
  getAssembler().registerSymbol(*Symbol);
185
0
  CSymbol->setIsSafeSEH();
186
0
187
0
  // The Microsoft linker requires that the symbol type of a handler be
188
0
  // function. Go ahead and oblige it here.
189
0
  CSymbol->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION
190
0
                   << COFF::SCT_COMPLEX_TYPE_SHIFT);
191
0
}
192
193
219
void MCWinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
194
219
  MCDataFragment *DF = getOrCreateDataFragment();
195
219
  const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
196
219
  MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2);
197
219
  DF->getFixups().push_back(Fixup);
198
219
  DF->getContents().resize(DF->getContents().size() + 2, 0);
199
219
}
200
201
void MCWinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol,
202
376
                                         uint64_t Offset) {
203
376
  MCDataFragment *DF = getOrCreateDataFragment();
204
376
  // Create Symbol A for the relocation relative reference.
205
376
  const MCExpr *MCE = MCSymbolRefExpr::create(Symbol, getContext());
206
376
  // Add the constant offset, if given.
207
376
  if (Offset)
208
3
    MCE = MCBinaryExpr::createAdd(
209
3
        MCE, MCConstantExpr::create(Offset, getContext()), getContext());
210
376
  // Build the secrel32 relocation.
211
376
  MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_SecRel_4);
212
376
  // Record the relocation.
213
376
  DF->getFixups().push_back(Fixup);
214
376
  // Emit 4 bytes (zeros) to the object file.
215
376
  DF->getContents().resize(DF->getContents().size() + 4, 0);
216
376
}
217
218
void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
219
15
                                         unsigned ByteAlignment) {
220
15
  auto *Symbol = cast<MCSymbolCOFF>(S);
221
15
222
15
  const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
223
15
  if (
T.isKnownWindowsMSVCEnvironment()15
)
{10
224
10
    if (ByteAlignment > 32)
225
0
      report_fatal_error("alignment is limited to 32-bytes");
226
10
227
10
    // Round size up to alignment so that we will honor the alignment request.
228
10
    Size = std::max(Size, static_cast<uint64_t>(ByteAlignment));
229
10
  }
230
15
231
15
  getAssembler().registerSymbol(*Symbol);
232
15
  Symbol->setExternal(true);
233
15
  Symbol->setCommon(Size, ByteAlignment);
234
15
235
15
  if (
!T.isKnownWindowsMSVCEnvironment() && 15
ByteAlignment > 15
)
{4
236
4
    SmallString<128> Directive;
237
4
    raw_svector_ostream OS(Directive);
238
4
    const MCObjectFileInfo *MFI = getContext().getObjectFileInfo();
239
4
240
4
    OS << " -aligncomm:\"" << Symbol->getName() << "\","
241
4
       << Log2_32_Ceil(ByteAlignment);
242
4
243
4
    PushSection();
244
4
    SwitchSection(MFI->getDrectveSection());
245
4
    EmitBytes(Directive);
246
4
    PopSection();
247
4
  }
248
15
}
249
250
void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
251
4
                                              unsigned ByteAlignment) {
252
4
  auto *Symbol = cast<MCSymbolCOFF>(S);
253
4
254
4
  MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
255
4
  getAssembler().registerSection(*Section);
256
4
  if (Section->getAlignment() < ByteAlignment)
257
0
    Section->setAlignment(ByteAlignment);
258
4
259
4
  getAssembler().registerSymbol(*Symbol);
260
4
  Symbol->setExternal(false);
261
4
262
4
  if (ByteAlignment != 1)
263
4
    new MCAlignFragment(ByteAlignment, /*Value=*/0, /*ValueSize=*/0,
264
4
                        ByteAlignment, Section);
265
4
266
4
  MCFillFragment *Fragment = new MCFillFragment(
267
4
      /*Value=*/0, Size, Section);
268
4
  Symbol->setFragment(Fragment);
269
4
}
270
271
void MCWinCOFFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
272
0
                                     uint64_t Size, unsigned ByteAlignment) {
273
0
  llvm_unreachable("not implemented");
274
0
}
275
276
void MCWinCOFFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
277
0
                                       uint64_t Size, unsigned ByteAlignment) {
278
0
  llvm_unreachable("not implemented");
279
0
}
280
281
// TODO: Implement this if you want to emit .comment section in COFF obj files.
282
0
void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) {
283
0
  llvm_unreachable("not implemented");
284
0
}
285
286
0
void MCWinCOFFStreamer::EmitWinEHHandlerData() {
287
0
  llvm_unreachable("not implemented");
288
0
}
289
290
249
void MCWinCOFFStreamer::FinishImpl() {
291
249
  MCObjectStreamer::FinishImpl();
292
249
}
293
294
11
void MCWinCOFFStreamer::Error(const Twine &Msg) const {
295
11
  getContext().reportError(SMLoc(), Msg);
296
11
}