Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/MC/MCELFStreamer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file assembles .s files and emits ELF .o object files.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/MC/MCELFStreamer.h"
14
#include "llvm/ADT/SmallString.h"
15
#include "llvm/ADT/SmallVector.h"
16
#include "llvm/BinaryFormat/ELF.h"
17
#include "llvm/MC/MCAsmBackend.h"
18
#include "llvm/MC/MCAsmInfo.h"
19
#include "llvm/MC/MCAssembler.h"
20
#include "llvm/MC/MCCodeEmitter.h"
21
#include "llvm/MC/MCContext.h"
22
#include "llvm/MC/MCExpr.h"
23
#include "llvm/MC/MCFixup.h"
24
#include "llvm/MC/MCFragment.h"
25
#include "llvm/MC/MCObjectFileInfo.h"
26
#include "llvm/MC/MCObjectWriter.h"
27
#include "llvm/MC/MCSection.h"
28
#include "llvm/MC/MCSectionELF.h"
29
#include "llvm/MC/MCStreamer.h"
30
#include "llvm/MC/MCSymbol.h"
31
#include "llvm/MC/MCSymbolELF.h"
32
#include "llvm/Support/Casting.h"
33
#include "llvm/Support/ErrorHandling.h"
34
#include "llvm/Support/TargetRegistry.h"
35
#include "llvm/Support/raw_ostream.h"
36
#include <cassert>
37
#include <cstdint>
38
39
using namespace llvm;
40
41
MCELFStreamer::MCELFStreamer(MCContext &Context,
42
                             std::unique_ptr<MCAsmBackend> TAB,
43
                             std::unique_ptr<MCObjectWriter> OW,
44
                             std::unique_ptr<MCCodeEmitter> Emitter)
45
    : MCObjectStreamer(Context, std::move(TAB), std::move(OW),
46
5.87k
                       std::move(Emitter)) {}
47
48
518k
bool MCELFStreamer::isBundleLocked() const {
49
518k
  return getCurrentSectionOnly()->isBundleLocked();
50
518k
}
51
52
void MCELFStreamer::mergeFragment(MCDataFragment *DF,
53
130
                                  MCDataFragment *EF) {
54
130
  MCAssembler &Assembler = getAssembler();
55
130
56
130
  if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
57
130
    uint64_t FSize = EF->getContents().size();
58
130
59
130
    if (FSize > Assembler.getBundleAlignSize())
60
1
      report_fatal_error("Fragment can't be larger than a bundle size");
61
129
62
129
    uint64_t RequiredBundlePadding = computeBundlePadding(
63
129
        Assembler, EF, DF->getContents().size(), FSize);
64
129
65
129
    if (RequiredBundlePadding > UINT8_MAX)
66
129
      
report_fatal_error("Padding cannot exceed 255 bytes")0
;
67
129
68
129
    if (RequiredBundlePadding > 0) {
69
23
      SmallString<256> Code;
70
23
      raw_svector_ostream VecOS(Code);
71
23
      EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
72
23
      Assembler.writeFragmentPadding(VecOS, *EF, FSize);
73
23
74
23
      DF->getContents().append(Code.begin(), Code.end());
75
23
    }
76
129
  }
77
130
78
130
  flushPendingLabels(DF, DF->getContents().size());
79
129
80
181
  for (unsigned i = 0, e = EF->getFixups().size(); i != e; 
++i52
) {
81
52
    EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
82
52
                                 DF->getContents().size());
83
52
    DF->getFixups().push_back(EF->getFixups()[i]);
84
52
  }
85
129
  if (DF->getSubtargetInfo() == nullptr && 
EF->getSubtargetInfo()22
)
86
22
    DF->setHasInstructions(*EF->getSubtargetInfo());
87
129
  DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
88
129
}
89
90
5.87k
void MCELFStreamer::InitSections(bool NoExecStack) {
91
5.87k
  MCContext &Ctx = getContext();
92
5.87k
  SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
93
5.87k
  EmitCodeAlignment(4);
94
5.87k
95
5.87k
  if (NoExecStack)
96
1
    SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
97
5.87k
}
98
99
115k
void MCELFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc) {
100
115k
  auto *Symbol = cast<MCSymbolELF>(S);
101
115k
  MCObjectStreamer::EmitLabel(Symbol, Loc);
102
115k
103
115k
  const MCSectionELF &Section =
104
115k
      static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
105
115k
  if (Section.getFlags() & ELF::SHF_TLS)
106
265
    Symbol->setType(ELF::STT_TLS);
107
115k
}
108
109
12
void MCELFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc, MCFragment *F) {
110
12
  auto *Symbol = cast<MCSymbolELF>(S);
111
12
  MCObjectStreamer::EmitLabel(Symbol, Loc, F);
112
12
113
12
  const MCSectionELF &Section =
114
12
      static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
115
12
  if (Section.getFlags() & ELF::SHF_TLS)
116
0
    Symbol->setType(ELF::STT_TLS);
117
12
}
118
119
963
void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
120
963
  // Let the target do whatever target specific stuff it needs to do.
121
963
  getAssembler().getBackend().handleAssemblerFlag(Flag);
122
963
  // Do any generic stuff we need to do.
123
963
  switch (Flag) {
124
963
  
case MCAF_SyntaxUnified: return76
; // no-op here.
125
963
  
case MCAF_Code16: return610
; // Change parsing mode; no-op here.
126
963
  
case MCAF_Code32: return275
; // Change parsing mode; no-op here.
127
963
  
case MCAF_Code64: return2
; // Change parsing mode; no-op here.
128
963
  case MCAF_SubsectionsViaSymbols:
129
0
    getAssembler().setSubsectionsViaSymbols(true);
130
0
    return;
131
0
  }
132
0
133
0
  llvm_unreachable("invalid assembler flag!");
134
0
}
135
136
// If bundle alignment is used and there are any instructions in the section, it
137
// needs to be aligned to at least the bundle size.
138
static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
139
490k
                                           MCSection *Section) {
140
490k
  if (Section && 
Assembler.isBundlingEnabled()485k
&&
Section->hasInstructions()60
&&
141
490k
      
Section->getAlignment() < Assembler.getBundleAlignSize()46
)
142
23
    Section->setAlignment(Assembler.getBundleAlignSize());
143
490k
}
144
145
void MCELFStreamer::ChangeSection(MCSection *Section,
146
485k
                                  const MCExpr *Subsection) {
147
485k
  MCSection *CurSection = getCurrentSectionOnly();
148
485k
  if (CurSection && 
isBundleLocked()479k
)
149
1
    report_fatal_error("Unterminated .bundle_lock when changing a section");
150
485k
151
485k
  MCAssembler &Asm = getAssembler();
152
485k
  // Ensure the previous section gets aligned if necessary.
153
485k
  setSectionAlignmentForBundling(Asm, CurSection);
154
485k
  auto *SectionELF = static_cast<const MCSectionELF *>(Section);
155
485k
  const MCSymbol *Grp = SectionELF->getGroup();
156
485k
  if (Grp)
157
224
    Asm.registerSymbol(*Grp);
158
485k
159
485k
  changeSectionImpl(Section, Subsection);
160
485k
  Asm.registerSymbol(*Section->getBeginSymbol());
161
485k
}
162
163
20
void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
164
20
  getAssembler().registerSymbol(*Symbol);
165
20
  const MCExpr *Value = MCSymbolRefExpr::create(
166
20
      Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
167
20
  Alias->setVariableValue(Value);
168
20
}
169
170
// When GNU as encounters more than one .type declaration for an object it seems
171
// to use a mechanism similar to the one below to decide which type is actually
172
// used in the object file.  The greater of T1 and T2 is selected based on the
173
// following ordering:
174
//  STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
175
// If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
176
// provided type).
177
5.69k
static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
178
5.69k
  for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
179
7.48k
                        ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
180
7.48k
    if (T1 == Type)
181
5.60k
      return T2;
182
1.87k
    if (T2 == Type)
183
92
      return T1;
184
1.87k
  }
185
5.69k
186
18.4E
  return T2;
187
5.69k
}
188
189
92.6k
bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
190
92.6k
  auto *Symbol = cast<MCSymbolELF>(S);
191
92.6k
192
92.6k
  // Adding a symbol attribute always introduces the symbol, note that an
193
92.6k
  // important side effect of calling registerSymbol here is to register
194
92.6k
  // the symbol with the assembler.
195
92.6k
  getAssembler().registerSymbol(*Symbol);
196
92.6k
197
92.6k
  // The implementation of symbol attributes is designed to match 'as', but it
198
92.6k
  // leaves much to desired. It doesn't really make sense to arbitrarily add and
199
92.6k
  // remove flags, but 'as' allows this (in particular, see .desc).
200
92.6k
  //
201
92.6k
  // In the future it might be worth trying to make these operations more well
202
92.6k
  // defined.
203
92.6k
  switch (Attribute) {
204
92.6k
  case MCSA_Cold:
205
2
  case MCSA_LazyReference:
206
2
  case MCSA_Reference:
207
2
  case MCSA_SymbolResolver:
208
2
  case MCSA_PrivateExtern:
209
2
  case MCSA_WeakDefinition:
210
2
  case MCSA_WeakDefAutoPrivate:
211
2
  case MCSA_Invalid:
212
2
  case MCSA_IndirectSymbol:
213
2
    return false;
214
2
215
105
  case MCSA_NoDeadStrip:
216
105
    // Ignore for now.
217
105
    break;
218
2
219
2
  case MCSA_ELF_TypeGnuUniqueObject:
220
2
    Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
221
2
    Symbol->setBinding(ELF::STB_GNU_UNIQUE);
222
2
    Symbol->setExternal(true);
223
2
    break;
224
2
225
86.1k
  case MCSA_Global:
226
86.1k
    Symbol->setBinding(ELF::STB_GLOBAL);
227
86.1k
    Symbol->setExternal(true);
228
86.1k
    break;
229
2
230
311
  case MCSA_WeakReference:
231
311
  case MCSA_Weak:
232
311
    Symbol->setBinding(ELF::STB_WEAK);
233
311
    Symbol->setExternal(true);
234
311
    break;
235
311
236
311
  case MCSA_Local:
237
45
    Symbol->setBinding(ELF::STB_LOCAL);
238
45
    Symbol->setExternal(false);
239
45
    break;
240
311
241
4.69k
  case MCSA_ELF_TypeFunction:
242
4.69k
    Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
243
4.69k
    break;
244
311
245
311
  case MCSA_ELF_TypeIndFunction:
246
65
    Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
247
65
    break;
248
311
249
922
  case MCSA_ELF_TypeObject:
250
922
    Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
251
922
    break;
252
311
253
311
  case MCSA_ELF_TypeTLS:
254
11
    Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
255
11
    break;
256
311
257
311
  case MCSA_ELF_TypeCommon:
258
0
    // TODO: Emit these as a common symbol.
259
0
    Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
260
0
    break;
261
311
262
311
  case MCSA_ELF_TypeNoType:
263
5
    Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
264
5
    break;
265
311
266
311
  case MCSA_Protected:
267
39
    Symbol->setVisibility(ELF::STV_PROTECTED);
268
39
    break;
269
311
270
311
  case MCSA_Hidden:
271
295
    Symbol->setVisibility(ELF::STV_HIDDEN);
272
295
    break;
273
311
274
311
  case MCSA_Internal:
275
4
    Symbol->setVisibility(ELF::STV_INTERNAL);
276
4
    break;
277
311
278
311
  case MCSA_AltEntry:
279
0
    llvm_unreachable("ELF doesn't support the .alt_entry attribute");
280
92.6k
  }
281
92.6k
282
92.6k
  return true;
283
92.6k
}
284
285
void MCELFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
286
251
                                     unsigned ByteAlignment) {
287
251
  auto *Symbol = cast<MCSymbolELF>(S);
288
251
  getAssembler().registerSymbol(*Symbol);
289
251
290
251
  if (!Symbol->isBindingSet()) {
291
185
    Symbol->setBinding(ELF::STB_GLOBAL);
292
185
    Symbol->setExternal(true);
293
185
  }
294
251
295
251
  Symbol->setType(ELF::STT_OBJECT);
296
251
297
251
  if (Symbol->getBinding() == ELF::STB_LOCAL) {
298
46
    MCSection &Section = *getAssembler().getContext().getELFSection(
299
46
        ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
300
46
    MCSectionSubPair P = getCurrentSection();
301
46
    SwitchSection(&Section);
302
46
303
46
    EmitValueToAlignment(ByteAlignment, 0, 1, 0);
304
46
    EmitLabel(Symbol);
305
46
    EmitZeros(Size);
306
46
307
46
    // Update the maximum alignment of the section if necessary.
308
46
    if (ByteAlignment > Section.getAlignment())
309
0
      Section.setAlignment(ByteAlignment);
310
46
311
46
    SwitchSection(P.first, P.second);
312
205
  } else {
313
205
    if(Symbol->declareCommon(Size, ByteAlignment))
314
1
      report_fatal_error("Symbol: " + Symbol->getName() +
315
1
                         " redeclared as different type");
316
250
  }
317
250
318
250
  cast<MCSymbolELF>(Symbol)
319
250
      ->setSize(MCConstantExpr::create(Size, getContext()));
320
250
}
321
322
3.35k
void MCELFStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
323
3.35k
  cast<MCSymbolELF>(Symbol)->setSize(Value);
324
3.35k
}
325
326
void MCELFStreamer::emitELFSymverDirective(StringRef AliasName,
327
75
                                           const MCSymbol *Aliasee) {
328
75
  getAssembler().Symvers.push_back({AliasName, Aliasee});
329
75
}
330
331
void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
332
20
                                          unsigned ByteAlignment) {
333
20
  auto *Symbol = cast<MCSymbolELF>(S);
334
20
  // FIXME: Should this be caught and done earlier?
335
20
  getAssembler().registerSymbol(*Symbol);
336
20
  Symbol->setBinding(ELF::STB_LOCAL);
337
20
  Symbol->setExternal(false);
338
20
  EmitCommonSymbol(Symbol, Size, ByteAlignment);
339
20
}
340
341
void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
342
17.9k
                                  SMLoc Loc) {
343
17.9k
  if (isBundleLocked())
344
0
    report_fatal_error("Emitting values inside a locked bundle is forbidden");
345
17.9k
  fixSymbolsInTLSFixups(Value);
346
17.9k
  MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
347
17.9k
}
348
349
void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
350
                                         int64_t Value,
351
                                         unsigned ValueSize,
352
14.0k
                                         unsigned MaxBytesToEmit) {
353
14.0k
  if (isBundleLocked())
354
0
    report_fatal_error("Emitting values inside a locked bundle is forbidden");
355
14.0k
  MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
356
14.0k
                                         ValueSize, MaxBytesToEmit);
357
14.0k
}
358
359
void MCELFStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From,
360
                                       const MCSymbolRefExpr *To,
361
22
                                       uint64_t Count) {
362
22
  getAssembler().CGProfile.push_back({From, To, Count});
363
22
}
364
365
296
void MCELFStreamer::EmitIdent(StringRef IdentString) {
366
296
  MCSection *Comment = getAssembler().getContext().getELFSection(
367
296
      ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
368
296
  PushSection();
369
296
  SwitchSection(Comment);
370
296
  if (!SeenIdent) {
371
274
    EmitIntValue(0, 1);
372
274
    SeenIdent = true;
373
274
  }
374
296
  EmitBytes(IdentString);
375
296
  EmitIntValue(0, 1);
376
296
  PopSection();
377
296
}
378
379
65.8k
void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
380
65.8k
  switch (expr->getKind()) {
381
65.8k
  case MCExpr::Target:
382
12.6k
    cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
383
12.6k
    break;
384
65.8k
  case MCExpr::Constant:
385
9.30k
    break;
386
65.8k
387
65.8k
  case MCExpr::Binary: {
388
14.8k
    const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
389
14.8k
    fixSymbolsInTLSFixups(be->getLHS());
390
14.8k
    fixSymbolsInTLSFixups(be->getRHS());
391
14.8k
    break;
392
65.8k
  }
393
65.8k
394
65.8k
  case MCExpr::SymbolRef: {
395
29.0k
    const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
396
29.0k
    switch (symRef.getKind()) {
397
29.0k
    default:
398
28.3k
      return;
399
29.0k
    case MCSymbolRefExpr::VK_GOTTPOFF:
400
714
    case MCSymbolRefExpr::VK_INDNTPOFF:
401
714
    case MCSymbolRefExpr::VK_NTPOFF:
402
714
    case MCSymbolRefExpr::VK_GOTNTPOFF:
403
714
    case MCSymbolRefExpr::VK_TLSCALL:
404
714
    case MCSymbolRefExpr::VK_TLSDESC:
405
714
    case MCSymbolRefExpr::VK_TLSGD:
406
714
    case MCSymbolRefExpr::VK_TLSLD:
407
714
    case MCSymbolRefExpr::VK_TLSLDM:
408
714
    case MCSymbolRefExpr::VK_TPOFF:
409
714
    case MCSymbolRefExpr::VK_TPREL:
410
714
    case MCSymbolRefExpr::VK_DTPOFF:
411
714
    case MCSymbolRefExpr::VK_DTPREL:
412
714
    case MCSymbolRefExpr::VK_PPC_DTPMOD:
413
714
    case MCSymbolRefExpr::VK_PPC_TPREL_LO:
414
714
    case MCSymbolRefExpr::VK_PPC_TPREL_HI:
415
714
    case MCSymbolRefExpr::VK_PPC_TPREL_HA:
416
714
    case MCSymbolRefExpr::VK_PPC_TPREL_HIGH:
417
714
    case MCSymbolRefExpr::VK_PPC_TPREL_HIGHA:
418
714
    case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
419
714
    case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
420
714
    case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
421
714
    case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
422
714
    case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
423
714
    case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
424
714
    case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
425
714
    case MCSymbolRefExpr::VK_PPC_DTPREL_HIGH:
426
714
    case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHA:
427
714
    case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
428
714
    case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
429
714
    case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
430
714
    case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
431
714
    case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
432
714
    case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
433
714
    case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
434
714
    case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
435
714
    case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
436
714
    case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
437
714
    case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
438
714
    case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
439
714
    case MCSymbolRefExpr::VK_PPC_TLS:
440
714
    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
441
714
    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
442
714
    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
443
714
    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
444
714
    case MCSymbolRefExpr::VK_PPC_TLSGD:
445
714
    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
446
714
    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
447
714
    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
448
714
    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
449
714
    case MCSymbolRefExpr::VK_PPC_TLSLD:
450
714
      break;
451
714
    }
452
714
    getAssembler().registerSymbol(symRef.getSymbol());
453
714
    cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
454
714
    break;
455
714
  }
456
714
457
714
  case MCExpr::Unary:
458
17
    fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
459
17
    break;
460
65.8k
  }
461
65.8k
}
462
463
44
void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) {
464
44
  const MCSymbol *S = &SRE->getSymbol();
465
44
  if (S->isTemporary()) {
466
2
    if (!S->isInSection()) {
467
1
      getContext().reportError(
468
1
          SRE->getLoc(), Twine("Reference to undefined temporary symbol ") +
469
1
                             "`" + S->getName() + "`");
470
1
      return;
471
1
    }
472
1
    S = S->getSection().getBeginSymbol();
473
1
    S->setUsedInReloc();
474
1
    SRE =
475
1
        MCSymbolRefExpr::create(S, SRE->getKind(), getContext(), SRE->getLoc());
476
1
    return;
477
1
  }
478
42
  // Not a temporary, referece it as a weak undefined.
479
42
  bool Created;
480
42
  getAssembler().registerSymbol(*S, &Created);
481
42
  if (Created) {
482
2
    cast<MCSymbolELF>(S)->setBinding(ELF::STB_WEAK);
483
2
    cast<MCSymbolELF>(S)->setExternal(true);
484
2
  }
485
42
}
486
487
5.80k
void MCELFStreamer::finalizeCGProfile() {
488
5.80k
  for (MCAssembler::CGProfileEntry &E : getAssembler().CGProfile) {
489
22
    finalizeCGProfileEntry(E.From);
490
22
    finalizeCGProfileEntry(E.To);
491
22
  }
492
5.80k
}
493
494
void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
495
3.46k
                                       const MCSubtargetInfo &STI) {
496
3.46k
  this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
497
3.46k
  MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
498
3.46k
499
4.55k
  for (unsigned i = 0, e = F.getFixups().size(); i != e; 
++i1.09k
)
500
1.09k
    fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
501
3.46k
}
502
503
// A fragment can only have one Subtarget, and when bundling is enabled we
504
// sometimes need to use the same fragment. We give an error if there
505
// are conflicting Subtargets.
506
static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI,
507
3.93k
                                  const MCSubtargetInfo *NewSTI) {
508
3.93k
  if (OldSTI && 
NewSTI3.91k
&&
OldSTI != NewSTI3.91k
)
509
3
    report_fatal_error("A Bundle can only have one Subtarget.");
510
3.93k
}
511
512
void MCELFStreamer::EmitInstToData(const MCInst &Inst,
513
74.0k
                                   const MCSubtargetInfo &STI) {
514
74.0k
  MCAssembler &Assembler = getAssembler();
515
74.0k
  SmallVector<MCFixup, 4> Fixups;
516
74.0k
  SmallString<256> Code;
517
74.0k
  raw_svector_ostream VecOS(Code);
518
74.0k
  Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
519
74.0k
520
91.1k
  for (unsigned i = 0, e = Fixups.size(); i != e; 
++i17.0k
)
521
17.0k
    fixSymbolsInTLSFixups(Fixups[i].getValue());
522
74.0k
523
74.0k
  // There are several possibilities here:
524
74.0k
  //
525
74.0k
  // If bundling is disabled, append the encoded instruction to the current data
526
74.0k
  // fragment (or create a new such fragment if the current fragment is not a
527
74.0k
  // data fragment, or the Subtarget has changed).
528
74.0k
  //
529
74.0k
  // If bundling is enabled:
530
74.0k
  // - If we're not in a bundle-locked group, emit the instruction into a
531
74.0k
  //   fragment of its own. If there are no fixups registered for the
532
74.0k
  //   instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
533
74.0k
  //   MCDataFragment.
534
74.0k
  // - If we're in a bundle-locked group, append the instruction to the current
535
74.0k
  //   data fragment because we want all the instructions in a group to get into
536
74.0k
  //   the same fragment. Be careful not to do that for the first instruction in
537
74.0k
  //   the group, though.
538
74.0k
  MCDataFragment *DF;
539
74.0k
540
74.0k
  if (Assembler.isBundlingEnabled()) {
541
4.78k
    MCSection &Sec = *getCurrentSectionOnly();
542
4.78k
    if (Assembler.getRelaxAll() && 
isBundleLocked()147
) {
543
33
      // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
544
33
      // the current bundle group.
545
33
      DF = BundleGroups.back();
546
33
      CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
547
33
    }
548
4.75k
    else if (Assembler.getRelaxAll() && 
!isBundleLocked()114
)
549
114
      // When not in a bundle-locked group and the -mc-relax-all flag is used,
550
114
      // we create a new temporary fragment which will be later merged into
551
114
      // the current fragment.
552
114
      DF = new MCDataFragment();
553
4.64k
    else if (isBundleLocked() && 
!Sec.isBundleGroupBeforeFirstInst()4.47k
) {
554
3.90k
      // If we are bundle-locked, we re-use the current fragment.
555
3.90k
      // The bundle-locking directive ensures this is a new data fragment.
556
3.90k
      DF = cast<MCDataFragment>(getCurrentFragment());
557
3.90k
      CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
558
3.90k
    }
559
742
    else if (!isBundleLocked() && 
Fixups.size() == 0170
) {
560
141
      // Optimize memory usage by emitting the instruction to a
561
141
      // MCCompactEncodedInstFragment when not in a bundle-locked group and
562
141
      // there are no fixups registered.
563
141
      MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
564
141
      insert(CEIF);
565
141
      CEIF->getContents().append(Code.begin(), Code.end());
566
141
      CEIF->setHasInstructions(STI);
567
141
      return;
568
601
    } else {
569
601
      DF = new MCDataFragment();
570
601
      insert(DF);
571
601
    }
572
4.78k
    
if (4.64k
Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd4.64k
) {
573
2.22k
      // If this fragment is for a group marked "align_to_end", set a flag
574
2.22k
      // in the fragment. This can happen after the fragment has already been
575
2.22k
      // created if there are nested bundle_align groups and an inner one
576
2.22k
      // is the one marked align_to_end.
577
2.22k
      DF->setAlignToBundleEnd(true);
578
2.22k
    }
579
4.64k
580
4.64k
    // We're now emitting an instruction in a bundle group, so this flag has
581
4.64k
    // to be turned off.
582
4.64k
    Sec.setBundleGroupBeforeFirstInst(false);
583
69.3k
  } else {
584
69.3k
    DF = getOrCreateDataFragment(&STI);
585
69.3k
  }
586
74.0k
587
74.0k
  // Add the fixups and data.
588
91.0k
  
for (unsigned i = 0, e = Fixups.size(); 73.9k
i != e;
++i17.0k
) {
589
17.0k
    Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
590
17.0k
    DF->getFixups().push_back(Fixups[i]);
591
17.0k
  }
592
73.9k
  DF->setHasInstructions(STI);
593
73.9k
  DF->getContents().append(Code.begin(), Code.end());
594
73.9k
595
73.9k
  if (Assembler.isBundlingEnabled() && 
Assembler.getRelaxAll()4.64k
) {
596
146
    if (!isBundleLocked()) {
597
114
      mergeFragment(getOrCreateDataFragment(&STI), DF);
598
114
      delete DF;
599
114
    }
600
146
  }
601
73.9k
}
602
603
42
void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
604
42
  assert(AlignPow2 <= 30 && "Invalid bundle alignment");
605
42
  MCAssembler &Assembler = getAssembler();
606
42
  if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
607
42
                        
Assembler.getBundleAlignSize() == 1U << AlignPow22
))
608
42
    Assembler.setBundleAlignSize(1U << AlignPow2);
609
0
  else
610
0
    report_fatal_error(".bundle_align_mode cannot be changed once set");
611
42
}
612
613
598
void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
614
598
  MCSection &Sec = *getCurrentSectionOnly();
615
598
616
598
  // Sanity checks
617
598
  //
618
598
  if (!getAssembler().isBundlingEnabled())
619
1
    report_fatal_error(".bundle_lock forbidden when bundling is disabled");
620
597
621
597
  if (!isBundleLocked())
622
589
    Sec.setBundleGroupBeforeFirstInst(true);
623
597
624
597
  if (getAssembler().getRelaxAll() && 
!isBundleLocked()21
) {
625
17
    // TODO: drop the lock state and set directly in the fragment
626
17
    MCDataFragment *DF = new MCDataFragment();
627
17
    BundleGroups.push_back(DF);
628
17
  }
629
597
630
597
  Sec.setBundleLockState(AlignToEnd ? 
MCSection::BundleLockedAlignToEnd288
631
597
                                    : 
MCSection::BundleLocked309
);
632
597
}
633
634
595
void MCELFStreamer::EmitBundleUnlock() {
635
595
  MCSection &Sec = *getCurrentSectionOnly();
636
595
637
595
  // Sanity checks
638
595
  if (!getAssembler().isBundlingEnabled())
639
0
    report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
640
595
  else if (!isBundleLocked())
641
2
    report_fatal_error(".bundle_unlock without matching lock");
642
593
  else if (Sec.isBundleGroupBeforeFirstInst())
643
0
    report_fatal_error("Empty bundle-locked group is forbidden");
644
593
645
593
  // When the -mc-relax-all flag is used, we emit instructions to fragments
646
593
  // stored on a stack. When the bundle unlock is emitted, we pop a fragment
647
593
  // from the stack a merge it to the one below.
648
593
  if (getAssembler().getRelaxAll()) {
649
20
    assert(!BundleGroups.empty() && "There are no bundle groups");
650
20
    MCDataFragment *DF = BundleGroups.back();
651
20
652
20
    // FIXME: Use BundleGroups to track the lock state instead.
653
20
    Sec.setBundleLockState(MCSection::NotBundleLocked);
654
20
655
20
    // FIXME: Use more separate fragments for nested groups.
656
20
    if (!isBundleLocked()) {
657
16
      mergeFragment(getOrCreateDataFragment(DF->getSubtargetInfo()), DF);
658
16
      BundleGroups.pop_back();
659
16
      delete DF;
660
16
    }
661
20
662
20
    if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
663
17
      getOrCreateDataFragment()->setAlignToBundleEnd(false);
664
20
  } else
665
573
    Sec.setBundleLockState(MCSection::NotBundleLocked);
666
593
}
667
668
5.80k
void MCELFStreamer::FinishImpl() {
669
5.80k
  // Ensure the last section gets aligned if necessary.
670
5.80k
  MCSection *CurSection = getCurrentSectionOnly();
671
5.80k
  setSectionAlignmentForBundling(getAssembler(), CurSection);
672
5.80k
673
5.80k
  finalizeCGProfile();
674
5.80k
  EmitFrames(nullptr);
675
5.80k
676
5.80k
  this->MCObjectStreamer::FinishImpl();
677
5.80k
}
678
679
0
void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
680
0
  llvm_unreachable("Generic ELF doesn't support this directive");
681
0
}
682
683
0
void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
684
0
  llvm_unreachable("ELF doesn't support this directive");
685
0
}
686
687
void MCELFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
688
                                 uint64_t Size, unsigned ByteAlignment,
689
0
                                 SMLoc Loc) {
690
0
  llvm_unreachable("ELF doesn't support this directive");
691
0
}
692
693
void MCELFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
694
0
                                   uint64_t Size, unsigned ByteAlignment) {
695
0
  llvm_unreachable("ELF doesn't support this directive");
696
0
}
697
698
MCStreamer *llvm::createELFStreamer(MCContext &Context,
699
                                    std::unique_ptr<MCAsmBackend> &&MAB,
700
                                    std::unique_ptr<MCObjectWriter> &&OW,
701
                                    std::unique_ptr<MCCodeEmitter> &&CE,
702
3.25k
                                    bool RelaxAll) {
703
3.25k
  MCELFStreamer *S =
704
3.25k
      new MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
705
3.25k
  if (RelaxAll)
706
38
    S->getAssembler().setRelaxAll(true);
707
3.25k
  return S;
708
3.25k
}