Bug Summary

File:lib/MC/MCAssembler.cpp
Warning:line 589, column 15
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name MCAssembler.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-9/lib/clang/9.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-9~svn361465/build-llvm/lib/MC -I /build/llvm-toolchain-snapshot-9~svn361465/lib/MC -I /build/llvm-toolchain-snapshot-9~svn361465/build-llvm/include -I /build/llvm-toolchain-snapshot-9~svn361465/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/9.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-9/lib/clang/9.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-9~svn361465/build-llvm/lib/MC -fdebug-prefix-map=/build/llvm-toolchain-snapshot-9~svn361465=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2019-05-24-031927-21217-1 -x c++ /build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp -faddrsig
1//===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
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#include "llvm/MC/MCAssembler.h"
10#include "llvm/ADT/ArrayRef.h"
11#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/SmallVector.h"
13#include "llvm/ADT/Statistic.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/MC/MCAsmBackend.h"
17#include "llvm/MC/MCAsmInfo.h"
18#include "llvm/MC/MCAsmLayout.h"
19#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCCodeView.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCDwarf.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCFixup.h"
25#include "llvm/MC/MCFixupKindInfo.h"
26#include "llvm/MC/MCFragment.h"
27#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCObjectWriter.h"
29#include "llvm/MC/MCSection.h"
30#include "llvm/MC/MCSectionELF.h"
31#include "llvm/MC/MCSymbol.h"
32#include "llvm/MC/MCValue.h"
33#include "llvm/Support/Casting.h"
34#include "llvm/Support/Debug.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/LEB128.h"
37#include "llvm/Support/MathExtras.h"
38#include "llvm/Support/raw_ostream.h"
39#include <cassert>
40#include <cstdint>
41#include <cstring>
42#include <tuple>
43#include <utility>
44
45using namespace llvm;
46
47#define DEBUG_TYPE"assembler" "assembler"
48
49namespace {
50namespace stats {
51
52STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total")static llvm::Statistic EmittedFragments = {"assembler", "EmittedFragments"
, "Number of emitted assembler fragments - total", {0}, {false
}}
;
53STATISTIC(EmittedRelaxableFragments,static llvm::Statistic EmittedRelaxableFragments = {"assembler"
, "EmittedRelaxableFragments", "Number of emitted assembler fragments - relaxable"
, {0}, {false}}
54 "Number of emitted assembler fragments - relaxable")static llvm::Statistic EmittedRelaxableFragments = {"assembler"
, "EmittedRelaxableFragments", "Number of emitted assembler fragments - relaxable"
, {0}, {false}}
;
55STATISTIC(EmittedDataFragments,static llvm::Statistic EmittedDataFragments = {"assembler", "EmittedDataFragments"
, "Number of emitted assembler fragments - data", {0}, {false
}}
56 "Number of emitted assembler fragments - data")static llvm::Statistic EmittedDataFragments = {"assembler", "EmittedDataFragments"
, "Number of emitted assembler fragments - data", {0}, {false
}}
;
57STATISTIC(EmittedCompactEncodedInstFragments,static llvm::Statistic EmittedCompactEncodedInstFragments = {
"assembler", "EmittedCompactEncodedInstFragments", "Number of emitted assembler fragments - compact encoded inst"
, {0}, {false}}
58 "Number of emitted assembler fragments - compact encoded inst")static llvm::Statistic EmittedCompactEncodedInstFragments = {
"assembler", "EmittedCompactEncodedInstFragments", "Number of emitted assembler fragments - compact encoded inst"
, {0}, {false}}
;
59STATISTIC(EmittedAlignFragments,static llvm::Statistic EmittedAlignFragments = {"assembler", "EmittedAlignFragments"
, "Number of emitted assembler fragments - align", {0}, {false
}}
60 "Number of emitted assembler fragments - align")static llvm::Statistic EmittedAlignFragments = {"assembler", "EmittedAlignFragments"
, "Number of emitted assembler fragments - align", {0}, {false
}}
;
61STATISTIC(EmittedFillFragments,static llvm::Statistic EmittedFillFragments = {"assembler", "EmittedFillFragments"
, "Number of emitted assembler fragments - fill", {0}, {false
}}
62 "Number of emitted assembler fragments - fill")static llvm::Statistic EmittedFillFragments = {"assembler", "EmittedFillFragments"
, "Number of emitted assembler fragments - fill", {0}, {false
}}
;
63STATISTIC(EmittedOrgFragments,static llvm::Statistic EmittedOrgFragments = {"assembler", "EmittedOrgFragments"
, "Number of emitted assembler fragments - org", {0}, {false}
}
64 "Number of emitted assembler fragments - org")static llvm::Statistic EmittedOrgFragments = {"assembler", "EmittedOrgFragments"
, "Number of emitted assembler fragments - org", {0}, {false}
}
;
65STATISTIC(evaluateFixup, "Number of evaluated fixups")static llvm::Statistic evaluateFixup = {"assembler", "evaluateFixup"
, "Number of evaluated fixups", {0}, {false}}
;
66STATISTIC(FragmentLayouts, "Number of fragment layouts")static llvm::Statistic FragmentLayouts = {"assembler", "FragmentLayouts"
, "Number of fragment layouts", {0}, {false}}
;
67STATISTIC(ObjectBytes, "Number of emitted object file bytes")static llvm::Statistic ObjectBytes = {"assembler", "ObjectBytes"
, "Number of emitted object file bytes", {0}, {false}}
;
68STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps")static llvm::Statistic RelaxationSteps = {"assembler", "RelaxationSteps"
, "Number of assembler layout and relaxation steps", {0}, {false
}}
;
69STATISTIC(RelaxedInstructions, "Number of relaxed instructions")static llvm::Statistic RelaxedInstructions = {"assembler", "RelaxedInstructions"
, "Number of relaxed instructions", {0}, {false}}
;
70STATISTIC(PaddingFragmentsRelaxations,static llvm::Statistic PaddingFragmentsRelaxations = {"assembler"
, "PaddingFragmentsRelaxations", "Number of Padding Fragments relaxations"
, {0}, {false}}
71 "Number of Padding Fragments relaxations")static llvm::Statistic PaddingFragmentsRelaxations = {"assembler"
, "PaddingFragmentsRelaxations", "Number of Padding Fragments relaxations"
, {0}, {false}}
;
72STATISTIC(PaddingFragmentsBytes,static llvm::Statistic PaddingFragmentsBytes = {"assembler", "PaddingFragmentsBytes"
, "Total size of all padding from adding Fragments", {0}, {false
}}
73 "Total size of all padding from adding Fragments")static llvm::Statistic PaddingFragmentsBytes = {"assembler", "PaddingFragmentsBytes"
, "Total size of all padding from adding Fragments", {0}, {false
}}
;
74
75} // end namespace stats
76} // end anonymous namespace
77
78// FIXME FIXME FIXME: There are number of places in this file where we convert
79// what is a 64-bit assembler value used for computation into a value in the
80// object file, which may truncate it. We should detect that truncation where
81// invalid and report errors back.
82
83/* *** */
84
85MCAssembler::MCAssembler(MCContext &Context,
86 std::unique_ptr<MCAsmBackend> Backend,
87 std::unique_ptr<MCCodeEmitter> Emitter,
88 std::unique_ptr<MCObjectWriter> Writer)
89 : Context(Context), Backend(std::move(Backend)),
90 Emitter(std::move(Emitter)), Writer(std::move(Writer)),
91 BundleAlignSize(0), RelaxAll(false), SubsectionsViaSymbols(false),
92 IncrementalLinkerCompatible(false), ELFHeaderEFlags(0) {
93 VersionInfo.Major = 0; // Major version == 0 for "none specified"
94}
95
96MCAssembler::~MCAssembler() = default;
97
98void MCAssembler::reset() {
99 Sections.clear();
100 Symbols.clear();
101 IndirectSymbols.clear();
102 DataRegions.clear();
103 LinkerOptions.clear();
104 FileNames.clear();
105 ThumbFuncs.clear();
106 BundleAlignSize = 0;
107 RelaxAll = false;
108 SubsectionsViaSymbols = false;
109 IncrementalLinkerCompatible = false;
110 ELFHeaderEFlags = 0;
111 LOHContainer.reset();
112 VersionInfo.Major = 0;
113 VersionInfo.SDKVersion = VersionTuple();
114
115 // reset objects owned by us
116 if (getBackendPtr())
117 getBackendPtr()->reset();
118 if (getEmitterPtr())
119 getEmitterPtr()->reset();
120 if (getWriterPtr())
121 getWriterPtr()->reset();
122 getLOHContainer().reset();
123}
124
125bool MCAssembler::registerSection(MCSection &Section) {
126 if (Section.isRegistered())
127 return false;
128 Sections.push_back(&Section);
129 Section.setIsRegistered(true);
130 return true;
131}
132
133bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
134 if (ThumbFuncs.count(Symbol))
135 return true;
136
137 if (!Symbol->isVariable())
138 return false;
139
140 const MCExpr *Expr = Symbol->getVariableValue();
141
142 MCValue V;
143 if (!Expr->evaluateAsRelocatable(V, nullptr, nullptr))
144 return false;
145
146 if (V.getSymB() || V.getRefKind() != MCSymbolRefExpr::VK_None)
147 return false;
148
149 const MCSymbolRefExpr *Ref = V.getSymA();
150 if (!Ref)
151 return false;
152
153 if (Ref->getKind() != MCSymbolRefExpr::VK_None)
154 return false;
155
156 const MCSymbol &Sym = Ref->getSymbol();
157 if (!isThumbFunc(&Sym))
158 return false;
159
160 ThumbFuncs.insert(Symbol); // Cache it.
161 return true;
162}
163
164bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
165 // Non-temporary labels should always be visible to the linker.
166 if (!Symbol.isTemporary())
167 return true;
168
169 // Absolute temporary labels are never visible.
170 if (!Symbol.isInSection())
171 return false;
172
173 if (Symbol.isUsedInReloc())
174 return true;
175
176 return false;
177}
178
179const MCSymbol *MCAssembler::getAtom(const MCSymbol &S) const {
180 // Linker visible symbols define atoms.
181 if (isSymbolLinkerVisible(S))
182 return &S;
183
184 // Absolute and undefined symbols have no defining atom.
185 if (!S.isInSection())
186 return nullptr;
187
188 // Non-linker visible symbols in sections which can't be atomized have no
189 // defining atom.
190 if (!getContext().getAsmInfo()->isSectionAtomizableBySymbols(
191 *S.getFragment()->getParent()))
192 return nullptr;
193
194 // Otherwise, return the atom for the containing fragment.
195 return S.getFragment()->getAtom();
196}
197
198bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
199 const MCFixup &Fixup, const MCFragment *DF,
200 MCValue &Target, uint64_t &Value,
201 bool &WasForced) const {
202 ++stats::evaluateFixup;
203
204 // FIXME: This code has some duplication with recordRelocation. We should
205 // probably merge the two into a single callback that tries to evaluate a
206 // fixup and records a relocation if one is needed.
207
208 // On error claim to have completely evaluated the fixup, to prevent any
209 // further processing from being done.
210 const MCExpr *Expr = Fixup.getValue();
211 MCContext &Ctx = getContext();
212 Value = 0;
213 WasForced = false;
214 if (!Expr->evaluateAsRelocatable(Target, &Layout, &Fixup)) {
215 Ctx.reportError(Fixup.getLoc(), "expected relocatable expression");
216 return true;
217 }
218 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
219 if (RefB->getKind() != MCSymbolRefExpr::VK_None) {
220 Ctx.reportError(Fixup.getLoc(),
221 "unsupported subtraction of qualified symbol");
222 return true;
223 }
224 }
225
226 assert(getBackendPtr() && "Expected assembler backend")((getBackendPtr() && "Expected assembler backend") ? static_cast
<void> (0) : __assert_fail ("getBackendPtr() && \"Expected assembler backend\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 226, __PRETTY_FUNCTION__))
;
227 bool IsPCRel = getBackendPtr()->getFixupKindInfo(Fixup.getKind()).Flags &
228 MCFixupKindInfo::FKF_IsPCRel;
229
230 bool IsResolved = false;
231 if (IsPCRel) {
232 if (Target.getSymB()) {
233 IsResolved = false;
234 } else if (!Target.getSymA()) {
235 IsResolved = false;
236 } else {
237 const MCSymbolRefExpr *A = Target.getSymA();
238 const MCSymbol &SA = A->getSymbol();
239 if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) {
240 IsResolved = false;
241 } else if (auto *Writer = getWriterPtr()) {
242 IsResolved = Writer->isSymbolRefDifferenceFullyResolvedImpl(
243 *this, SA, *DF, false, true);
244 }
245 }
246 } else {
247 IsResolved = Target.isAbsolute();
248 }
249
250 Value = Target.getConstant();
251
252 if (const MCSymbolRefExpr *A = Target.getSymA()) {
253 const MCSymbol &Sym = A->getSymbol();
254 if (Sym.isDefined())
255 Value += Layout.getSymbolOffset(Sym);
256 }
257 if (const MCSymbolRefExpr *B = Target.getSymB()) {
258 const MCSymbol &Sym = B->getSymbol();
259 if (Sym.isDefined())
260 Value -= Layout.getSymbolOffset(Sym);
261 }
262
263 bool ShouldAlignPC = getBackend().getFixupKindInfo(Fixup.getKind()).Flags &
264 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
265 assert((ShouldAlignPC ? IsPCRel : true) &&(((ShouldAlignPC ? IsPCRel : true) && "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!"
) ? static_cast<void> (0) : __assert_fail ("(ShouldAlignPC ? IsPCRel : true) && \"FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 266, __PRETTY_FUNCTION__))
266 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!")(((ShouldAlignPC ? IsPCRel : true) && "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!"
) ? static_cast<void> (0) : __assert_fail ("(ShouldAlignPC ? IsPCRel : true) && \"FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 266, __PRETTY_FUNCTION__))
;
267
268 if (IsPCRel) {
269 uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
270
271 // A number of ARM fixups in Thumb mode require that the effective PC
272 // address be determined as the 32-bit aligned version of the actual offset.
273 if (ShouldAlignPC) Offset &= ~0x3;
274 Value -= Offset;
275 }
276
277 // Let the backend force a relocation if needed.
278 if (IsResolved && getBackend().shouldForceRelocation(*this, Fixup, Target)) {
279 IsResolved = false;
280 WasForced = true;
281 }
282
283 return IsResolved;
284}
285
286uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
287 const MCFragment &F) const {
288 assert(getBackendPtr() && "Requires assembler backend")((getBackendPtr() && "Requires assembler backend") ? static_cast
<void> (0) : __assert_fail ("getBackendPtr() && \"Requires assembler backend\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 288, __PRETTY_FUNCTION__))
;
289 switch (F.getKind()) {
290 case MCFragment::FT_Data:
291 return cast<MCDataFragment>(F).getContents().size();
292 case MCFragment::FT_Relaxable:
293 return cast<MCRelaxableFragment>(F).getContents().size();
294 case MCFragment::FT_CompactEncodedInst:
295 return cast<MCCompactEncodedInstFragment>(F).getContents().size();
296 case MCFragment::FT_Fill: {
297 auto &FF = cast<MCFillFragment>(F);
298 int64_t NumValues = 0;
299 if (!FF.getNumValues().evaluateAsAbsolute(NumValues, Layout)) {
300 getContext().reportError(FF.getLoc(),
301 "expected assembly-time absolute expression");
302 return 0;
303 }
304 int64_t Size = NumValues * FF.getValueSize();
305 if (Size < 0) {
306 getContext().reportError(FF.getLoc(), "invalid number of bytes");
307 return 0;
308 }
309 return Size;
310 }
311
312 case MCFragment::FT_LEB:
313 return cast<MCLEBFragment>(F).getContents().size();
314
315 case MCFragment::FT_Padding:
316 return cast<MCPaddingFragment>(F).getSize();
317
318 case MCFragment::FT_SymbolId:
319 return 4;
320
321 case MCFragment::FT_Align: {
322 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
323 unsigned Offset = Layout.getFragmentOffset(&AF);
324 unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
325
326 // Insert extra Nops for code alignment if the target define
327 // shouldInsertExtraNopBytesForCodeAlign target hook.
328 if (AF.getParent()->UseCodeAlign() && AF.hasEmitNops() &&
329 getBackend().shouldInsertExtraNopBytesForCodeAlign(AF, Size))
330 return Size;
331
332 // If we are padding with nops, force the padding to be larger than the
333 // minimum nop size.
334 if (Size > 0 && AF.hasEmitNops()) {
335 while (Size % getBackend().getMinimumNopSize())
336 Size += AF.getAlignment();
337 }
338 if (Size > AF.getMaxBytesToEmit())
339 return 0;
340 return Size;
341 }
342
343 case MCFragment::FT_Org: {
344 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
345 MCValue Value;
346 if (!OF.getOffset().evaluateAsValue(Value, Layout)) {
347 getContext().reportError(OF.getLoc(),
348 "expected assembly-time absolute expression");
349 return 0;
350 }
351
352 uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
353 int64_t TargetLocation = Value.getConstant();
354 if (const MCSymbolRefExpr *A = Value.getSymA()) {
355 uint64_t Val;
356 if (!Layout.getSymbolOffset(A->getSymbol(), Val)) {
357 getContext().reportError(OF.getLoc(), "expected absolute expression");
358 return 0;
359 }
360 TargetLocation += Val;
361 }
362 int64_t Size = TargetLocation - FragmentOffset;
363 if (Size < 0 || Size >= 0x40000000) {
364 getContext().reportError(
365 OF.getLoc(), "invalid .org offset '" + Twine(TargetLocation) +
366 "' (at offset '" + Twine(FragmentOffset) + "')");
367 return 0;
368 }
369 return Size;
370 }
371
372 case MCFragment::FT_Dwarf:
373 return cast<MCDwarfLineAddrFragment>(F).getContents().size();
374 case MCFragment::FT_DwarfFrame:
375 return cast<MCDwarfCallFrameFragment>(F).getContents().size();
376 case MCFragment::FT_CVInlineLines:
377 return cast<MCCVInlineLineTableFragment>(F).getContents().size();
378 case MCFragment::FT_CVDefRange:
379 return cast<MCCVDefRangeFragment>(F).getContents().size();
380 case MCFragment::FT_Dummy:
381 llvm_unreachable("Should not have been added")::llvm::llvm_unreachable_internal("Should not have been added"
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 381)
;
382 }
383
384 llvm_unreachable("invalid fragment kind")::llvm::llvm_unreachable_internal("invalid fragment kind", "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 384)
;
385}
386
387void MCAsmLayout::layoutFragment(MCFragment *F) {
388 MCFragment *Prev = F->getPrevNode();
389
390 // We should never try to recompute something which is valid.
391 assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!")((!isFragmentValid(F) && "Attempt to recompute a valid fragment!"
) ? static_cast<void> (0) : __assert_fail ("!isFragmentValid(F) && \"Attempt to recompute a valid fragment!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 391, __PRETTY_FUNCTION__))
;
392 // We should never try to compute the fragment layout if its predecessor
393 // isn't valid.
394 assert((!Prev || isFragmentValid(Prev)) &&(((!Prev || isFragmentValid(Prev)) && "Attempt to compute fragment before its predecessor!"
) ? static_cast<void> (0) : __assert_fail ("(!Prev || isFragmentValid(Prev)) && \"Attempt to compute fragment before its predecessor!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 395, __PRETTY_FUNCTION__))
395 "Attempt to compute fragment before its predecessor!")(((!Prev || isFragmentValid(Prev)) && "Attempt to compute fragment before its predecessor!"
) ? static_cast<void> (0) : __assert_fail ("(!Prev || isFragmentValid(Prev)) && \"Attempt to compute fragment before its predecessor!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 395, __PRETTY_FUNCTION__))
;
396
397 ++stats::FragmentLayouts;
398
399 // Compute fragment offset and size.
400 if (Prev)
401 F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
402 else
403 F->Offset = 0;
404 LastValidFragment[F->getParent()] = F;
405
406 // If bundling is enabled and this fragment has instructions in it, it has to
407 // obey the bundling restrictions. With padding, we'll have:
408 //
409 //
410 // BundlePadding
411 // |||
412 // -------------------------------------
413 // Prev |##########| F |
414 // -------------------------------------
415 // ^
416 // |
417 // F->Offset
418 //
419 // The fragment's offset will point to after the padding, and its computed
420 // size won't include the padding.
421 //
422 // When the -mc-relax-all flag is used, we optimize bundling by writting the
423 // padding directly into fragments when the instructions are emitted inside
424 // the streamer. When the fragment is larger than the bundle size, we need to
425 // ensure that it's bundle aligned. This means that if we end up with
426 // multiple fragments, we must emit bundle padding between fragments.
427 //
428 // ".align N" is an example of a directive that introduces multiple
429 // fragments. We could add a special case to handle ".align N" by emitting
430 // within-fragment padding (which would produce less padding when N is less
431 // than the bundle size), but for now we don't.
432 //
433 if (Assembler.isBundlingEnabled() && F->hasInstructions()) {
434 assert(isa<MCEncodedFragment>(F) &&((isa<MCEncodedFragment>(F) && "Only MCEncodedFragment implementations have instructions"
) ? static_cast<void> (0) : __assert_fail ("isa<MCEncodedFragment>(F) && \"Only MCEncodedFragment implementations have instructions\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 435, __PRETTY_FUNCTION__))
435 "Only MCEncodedFragment implementations have instructions")((isa<MCEncodedFragment>(F) && "Only MCEncodedFragment implementations have instructions"
) ? static_cast<void> (0) : __assert_fail ("isa<MCEncodedFragment>(F) && \"Only MCEncodedFragment implementations have instructions\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 435, __PRETTY_FUNCTION__))
;
436 MCEncodedFragment *EF = cast<MCEncodedFragment>(F);
437 uint64_t FSize = Assembler.computeFragmentSize(*this, *EF);
438
439 if (!Assembler.getRelaxAll() && FSize > Assembler.getBundleAlignSize())
440 report_fatal_error("Fragment can't be larger than a bundle size");
441
442 uint64_t RequiredBundlePadding =
443 computeBundlePadding(Assembler, EF, EF->Offset, FSize);
444 if (RequiredBundlePadding > UINT8_MAX(255))
445 report_fatal_error("Padding cannot exceed 255 bytes");
446 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
447 EF->Offset += RequiredBundlePadding;
448 }
449}
450
451void MCAssembler::registerSymbol(const MCSymbol &Symbol, bool *Created) {
452 bool New = !Symbol.isRegistered();
453 if (Created)
454 *Created = New;
455 if (New) {
456 Symbol.setIsRegistered(true);
457 Symbols.push_back(&Symbol);
458 }
459}
460
461void MCAssembler::writeFragmentPadding(raw_ostream &OS,
462 const MCEncodedFragment &EF,
463 uint64_t FSize) const {
464 assert(getBackendPtr() && "Expected assembler backend")((getBackendPtr() && "Expected assembler backend") ? static_cast
<void> (0) : __assert_fail ("getBackendPtr() && \"Expected assembler backend\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 464, __PRETTY_FUNCTION__))
;
465 // Should NOP padding be written out before this fragment?
466 unsigned BundlePadding = EF.getBundlePadding();
467 if (BundlePadding > 0) {
468 assert(isBundlingEnabled() &&((isBundlingEnabled() && "Writing bundle padding with disabled bundling"
) ? static_cast<void> (0) : __assert_fail ("isBundlingEnabled() && \"Writing bundle padding with disabled bundling\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 469, __PRETTY_FUNCTION__))
469 "Writing bundle padding with disabled bundling")((isBundlingEnabled() && "Writing bundle padding with disabled bundling"
) ? static_cast<void> (0) : __assert_fail ("isBundlingEnabled() && \"Writing bundle padding with disabled bundling\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 469, __PRETTY_FUNCTION__))
;
470 assert(EF.hasInstructions() &&((EF.hasInstructions() && "Writing bundle padding for a fragment without instructions"
) ? static_cast<void> (0) : __assert_fail ("EF.hasInstructions() && \"Writing bundle padding for a fragment without instructions\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 471, __PRETTY_FUNCTION__))
471 "Writing bundle padding for a fragment without instructions")((EF.hasInstructions() && "Writing bundle padding for a fragment without instructions"
) ? static_cast<void> (0) : __assert_fail ("EF.hasInstructions() && \"Writing bundle padding for a fragment without instructions\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 471, __PRETTY_FUNCTION__))
;
472
473 unsigned TotalLength = BundlePadding + static_cast<unsigned>(FSize);
474 if (EF.alignToBundleEnd() && TotalLength > getBundleAlignSize()) {
475 // If the padding itself crosses a bundle boundary, it must be emitted
476 // in 2 pieces, since even nop instructions must not cross boundaries.
477 // v--------------v <- BundleAlignSize
478 // v---------v <- BundlePadding
479 // ----------------------------
480 // | Prev |####|####| F |
481 // ----------------------------
482 // ^-------------------^ <- TotalLength
483 unsigned DistanceToBoundary = TotalLength - getBundleAlignSize();
484 if (!getBackend().writeNopData(OS, DistanceToBoundary))
485 report_fatal_error("unable to write NOP sequence of " +
486 Twine(DistanceToBoundary) + " bytes");
487 BundlePadding -= DistanceToBoundary;
488 }
489 if (!getBackend().writeNopData(OS, BundlePadding))
490 report_fatal_error("unable to write NOP sequence of " +
491 Twine(BundlePadding) + " bytes");
492 }
493}
494
495/// Write the fragment \p F to the output file.
496static void writeFragment(raw_ostream &OS, const MCAssembler &Asm,
497 const MCAsmLayout &Layout, const MCFragment &F) {
498 // FIXME: Embed in fragments instead?
499 uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
500
501 support::endianness Endian = Asm.getBackend().Endian;
502
503 if (const MCEncodedFragment *EF = dyn_cast<MCEncodedFragment>(&F))
6
Taking false branch
504 Asm.writeFragmentPadding(OS, *EF, FragmentSize);
505
506 // This variable (and its dummy usage) is to participate in the assert at
507 // the end of the function.
508 uint64_t Start = OS.tell();
509 (void) Start;
510
511 ++stats::EmittedFragments;
512
513 switch (F.getKind()) {
7
Control jumps to 'case FT_Fill:' at line 575
514 case MCFragment::FT_Align: {
515 ++stats::EmittedAlignFragments;
516 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
517 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!")((AF.getValueSize() && "Invalid virtual align in concrete fragment!"
) ? static_cast<void> (0) : __assert_fail ("AF.getValueSize() && \"Invalid virtual align in concrete fragment!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 517, __PRETTY_FUNCTION__))
;
518
519 uint64_t Count = FragmentSize / AF.getValueSize();
520
521 // FIXME: This error shouldn't actually occur (the front end should emit
522 // multiple .align directives to enforce the semantics it wants), but is
523 // severe enough that we want to report it. How to handle this?
524 if (Count * AF.getValueSize() != FragmentSize)
525 report_fatal_error("undefined .align directive, value size '" +
526 Twine(AF.getValueSize()) +
527 "' is not a divisor of padding size '" +
528 Twine(FragmentSize) + "'");
529
530 // See if we are aligning with nops, and if so do that first to try to fill
531 // the Count bytes. Then if that did not fill any bytes or there are any
532 // bytes left to fill use the Value and ValueSize to fill the rest.
533 // If we are aligning with nops, ask that target to emit the right data.
534 if (AF.hasEmitNops()) {
535 if (!Asm.getBackend().writeNopData(OS, Count))
536 report_fatal_error("unable to write nop sequence of " +
537 Twine(Count) + " bytes");
538 break;
539 }
540
541 // Otherwise, write out in multiples of the value size.
542 for (uint64_t i = 0; i != Count; ++i) {
543 switch (AF.getValueSize()) {
544 default: llvm_unreachable("Invalid size!")::llvm::llvm_unreachable_internal("Invalid size!", "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 544)
;
545 case 1: OS << char(AF.getValue()); break;
546 case 2:
547 support::endian::write<uint16_t>(OS, AF.getValue(), Endian);
548 break;
549 case 4:
550 support::endian::write<uint32_t>(OS, AF.getValue(), Endian);
551 break;
552 case 8:
553 support::endian::write<uint64_t>(OS, AF.getValue(), Endian);
554 break;
555 }
556 }
557 break;
558 }
559
560 case MCFragment::FT_Data:
561 ++stats::EmittedDataFragments;
562 OS << cast<MCDataFragment>(F).getContents();
563 break;
564
565 case MCFragment::FT_Relaxable:
566 ++stats::EmittedRelaxableFragments;
567 OS << cast<MCRelaxableFragment>(F).getContents();
568 break;
569
570 case MCFragment::FT_CompactEncodedInst:
571 ++stats::EmittedCompactEncodedInstFragments;
572 OS << cast<MCCompactEncodedInstFragment>(F).getContents();
573 break;
574
575 case MCFragment::FT_Fill: {
576 ++stats::EmittedFillFragments;
577 const MCFillFragment &FF = cast<MCFillFragment>(F);
578 uint64_t V = FF.getValue();
579 unsigned VSize = FF.getValueSize();
580 const unsigned MaxChunkSize = 16;
581 char Data[MaxChunkSize];
582 // Duplicate V into Data as byte vector to reduce number of
583 // writes done. As such, do endian conversion here.
584 for (unsigned I = 0; I != VSize; ++I) {
8
Assuming 'I' is equal to 'VSize'
9
Loop condition is false. Execution continues on line 588
585 unsigned index = Endian == support::little ? I : (VSize - I - 1);
586 Data[I] = uint8_t(V >> (index * 8));
587 }
588 for (unsigned I = VSize; I < MaxChunkSize; ++I)
10
Loop condition is true. Entering loop body
589 Data[I] = Data[I - VSize];
11
Assigned value is garbage or undefined
590
591 // Set to largest multiple of VSize in Data.
592 const unsigned NumPerChunk = MaxChunkSize / VSize;
593 // Set ChunkSize to largest multiple of VSize in Data
594 const unsigned ChunkSize = VSize * NumPerChunk;
595
596 // Do copies by chunk.
597 StringRef Ref(Data, ChunkSize);
598 for (uint64_t I = 0, E = FragmentSize / ChunkSize; I != E; ++I)
599 OS << Ref;
600
601 // do remainder if needed.
602 unsigned TrailingCount = FragmentSize % ChunkSize;
603 if (TrailingCount)
604 OS.write(Data, TrailingCount);
605 break;
606 }
607
608 case MCFragment::FT_LEB: {
609 const MCLEBFragment &LF = cast<MCLEBFragment>(F);
610 OS << LF.getContents();
611 break;
612 }
613
614 case MCFragment::FT_Padding: {
615 if (!Asm.getBackend().writeNopData(OS, FragmentSize))
616 report_fatal_error("unable to write nop sequence of " +
617 Twine(FragmentSize) + " bytes");
618 break;
619 }
620
621 case MCFragment::FT_SymbolId: {
622 const MCSymbolIdFragment &SF = cast<MCSymbolIdFragment>(F);
623 support::endian::write<uint32_t>(OS, SF.getSymbol()->getIndex(), Endian);
624 break;
625 }
626
627 case MCFragment::FT_Org: {
628 ++stats::EmittedOrgFragments;
629 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
630
631 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
632 OS << char(OF.getValue());
633
634 break;
635 }
636
637 case MCFragment::FT_Dwarf: {
638 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
639 OS << OF.getContents();
640 break;
641 }
642 case MCFragment::FT_DwarfFrame: {
643 const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
644 OS << CF.getContents();
645 break;
646 }
647 case MCFragment::FT_CVInlineLines: {
648 const auto &OF = cast<MCCVInlineLineTableFragment>(F);
649 OS << OF.getContents();
650 break;
651 }
652 case MCFragment::FT_CVDefRange: {
653 const auto &DRF = cast<MCCVDefRangeFragment>(F);
654 OS << DRF.getContents();
655 break;
656 }
657 case MCFragment::FT_Dummy:
658 llvm_unreachable("Should not have been added")::llvm::llvm_unreachable_internal("Should not have been added"
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 658)
;
659 }
660
661 assert(OS.tell() - Start == FragmentSize &&((OS.tell() - Start == FragmentSize && "The stream should advance by fragment size"
) ? static_cast<void> (0) : __assert_fail ("OS.tell() - Start == FragmentSize && \"The stream should advance by fragment size\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 662, __PRETTY_FUNCTION__))
662 "The stream should advance by fragment size")((OS.tell() - Start == FragmentSize && "The stream should advance by fragment size"
) ? static_cast<void> (0) : __assert_fail ("OS.tell() - Start == FragmentSize && \"The stream should advance by fragment size\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 662, __PRETTY_FUNCTION__))
;
663}
664
665void MCAssembler::writeSectionData(raw_ostream &OS, const MCSection *Sec,
666 const MCAsmLayout &Layout) const {
667 assert(getBackendPtr() && "Expected assembler backend")((getBackendPtr() && "Expected assembler backend") ? static_cast
<void> (0) : __assert_fail ("getBackendPtr() && \"Expected assembler backend\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 667, __PRETTY_FUNCTION__))
;
1
Assuming the condition is true
2
'?' condition is true
668
669 // Ignore virtual sections.
670 if (Sec->isVirtualSection()) {
3
Assuming the condition is false
4
Taking false branch
671 assert(Layout.getSectionFileSize(Sec) == 0 && "Invalid size for section!")((Layout.getSectionFileSize(Sec) == 0 && "Invalid size for section!"
) ? static_cast<void> (0) : __assert_fail ("Layout.getSectionFileSize(Sec) == 0 && \"Invalid size for section!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 671, __PRETTY_FUNCTION__))
;
672
673 // Check that contents are only things legal inside a virtual section.
674 for (const MCFragment &F : *Sec) {
675 switch (F.getKind()) {
676 default: llvm_unreachable("Invalid fragment in virtual section!")::llvm::llvm_unreachable_internal("Invalid fragment in virtual section!"
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 676)
;
677 case MCFragment::FT_Data: {
678 // Check that we aren't trying to write a non-zero contents (or fixups)
679 // into a virtual section. This is to support clients which use standard
680 // directives to fill the contents of virtual sections.
681 const MCDataFragment &DF = cast<MCDataFragment>(F);
682 if (DF.fixup_begin() != DF.fixup_end())
683 report_fatal_error("cannot have fixups in virtual section!");
684 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
685 if (DF.getContents()[i]) {
686 if (auto *ELFSec = dyn_cast<const MCSectionELF>(Sec))
687 report_fatal_error("non-zero initializer found in section '" +
688 ELFSec->getSectionName() + "'");
689 else
690 report_fatal_error("non-zero initializer found in virtual section");
691 }
692 break;
693 }
694 case MCFragment::FT_Align:
695 // Check that we aren't trying to write a non-zero value into a virtual
696 // section.
697 assert((cast<MCAlignFragment>(F).getValueSize() == 0 ||(((cast<MCAlignFragment>(F).getValueSize() == 0 || cast
<MCAlignFragment>(F).getValue() == 0) && "Invalid align in virtual section!"
) ? static_cast<void> (0) : __assert_fail ("(cast<MCAlignFragment>(F).getValueSize() == 0 || cast<MCAlignFragment>(F).getValue() == 0) && \"Invalid align in virtual section!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 699, __PRETTY_FUNCTION__))
698 cast<MCAlignFragment>(F).getValue() == 0) &&(((cast<MCAlignFragment>(F).getValueSize() == 0 || cast
<MCAlignFragment>(F).getValue() == 0) && "Invalid align in virtual section!"
) ? static_cast<void> (0) : __assert_fail ("(cast<MCAlignFragment>(F).getValueSize() == 0 || cast<MCAlignFragment>(F).getValue() == 0) && \"Invalid align in virtual section!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 699, __PRETTY_FUNCTION__))
699 "Invalid align in virtual section!")(((cast<MCAlignFragment>(F).getValueSize() == 0 || cast
<MCAlignFragment>(F).getValue() == 0) && "Invalid align in virtual section!"
) ? static_cast<void> (0) : __assert_fail ("(cast<MCAlignFragment>(F).getValueSize() == 0 || cast<MCAlignFragment>(F).getValue() == 0) && \"Invalid align in virtual section!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 699, __PRETTY_FUNCTION__))
;
700 break;
701 case MCFragment::FT_Fill:
702 assert((cast<MCFillFragment>(F).getValue() == 0) &&(((cast<MCFillFragment>(F).getValue() == 0) && "Invalid fill in virtual section!"
) ? static_cast<void> (0) : __assert_fail ("(cast<MCFillFragment>(F).getValue() == 0) && \"Invalid fill in virtual section!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 703, __PRETTY_FUNCTION__))
703 "Invalid fill in virtual section!")(((cast<MCFillFragment>(F).getValue() == 0) && "Invalid fill in virtual section!"
) ? static_cast<void> (0) : __assert_fail ("(cast<MCFillFragment>(F).getValue() == 0) && \"Invalid fill in virtual section!\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 703, __PRETTY_FUNCTION__))
;
704 break;
705 }
706 }
707
708 return;
709 }
710
711 uint64_t Start = OS.tell();
712 (void)Start;
713
714 for (const MCFragment &F : *Sec)
715 writeFragment(OS, *this, Layout, F);
5
Calling 'writeFragment'
716
717 assert(OS.tell() - Start == Layout.getSectionAddressSize(Sec))((OS.tell() - Start == Layout.getSectionAddressSize(Sec)) ? static_cast
<void> (0) : __assert_fail ("OS.tell() - Start == Layout.getSectionAddressSize(Sec)"
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 717, __PRETTY_FUNCTION__))
;
718}
719
720std::tuple<MCValue, uint64_t, bool>
721MCAssembler::handleFixup(const MCAsmLayout &Layout, MCFragment &F,
722 const MCFixup &Fixup) {
723 // Evaluate the fixup.
724 MCValue Target;
725 uint64_t FixedValue;
726 bool WasForced;
727 bool IsResolved = evaluateFixup(Layout, Fixup, &F, Target, FixedValue,
728 WasForced);
729 if (!IsResolved) {
730 // The fixup was unresolved, we need a relocation. Inform the object
731 // writer of the relocation, and give it an opportunity to adjust the
732 // fixup value if need be.
733 if (Target.getSymA() && Target.getSymB() &&
734 getBackend().requiresDiffExpressionRelocations()) {
735 // The fixup represents the difference between two symbols, which the
736 // backend has indicated must be resolved at link time. Split up the fixup
737 // into two relocations, one for the add, and one for the sub, and emit
738 // both of these. The constant will be associated with the add half of the
739 // expression.
740 MCFixup FixupAdd = MCFixup::createAddFor(Fixup);
741 MCValue TargetAdd =
742 MCValue::get(Target.getSymA(), nullptr, Target.getConstant());
743 getWriter().recordRelocation(*this, Layout, &F, FixupAdd, TargetAdd,
744 FixedValue);
745 MCFixup FixupSub = MCFixup::createSubFor(Fixup);
746 MCValue TargetSub = MCValue::get(Target.getSymB());
747 getWriter().recordRelocation(*this, Layout, &F, FixupSub, TargetSub,
748 FixedValue);
749 } else {
750 getWriter().recordRelocation(*this, Layout, &F, Fixup, Target,
751 FixedValue);
752 }
753 }
754 return std::make_tuple(Target, FixedValue, IsResolved);
755}
756
757void MCAssembler::layout(MCAsmLayout &Layout) {
758 assert(getBackendPtr() && "Expected assembler backend")((getBackendPtr() && "Expected assembler backend") ? static_cast
<void> (0) : __assert_fail ("getBackendPtr() && \"Expected assembler backend\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 758, __PRETTY_FUNCTION__))
;
759 DEBUG_WITH_TYPE("mc-dump", {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mc-dump")) { { errs() << "assembler backend - pre-layout\n--\n"
; dump(); }; } } while (false)
760 errs() << "assembler backend - pre-layout\n--\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mc-dump")) { { errs() << "assembler backend - pre-layout\n--\n"
; dump(); }; } } while (false)
761 dump(); })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mc-dump")) { { errs() << "assembler backend - pre-layout\n--\n"
; dump(); }; } } while (false)
;
762
763 // Create dummy fragments and assign section ordinals.
764 unsigned SectionIndex = 0;
765 for (MCSection &Sec : *this) {
766 // Create dummy fragments to eliminate any empty sections, this simplifies
767 // layout.
768 if (Sec.getFragmentList().empty())
769 new MCDataFragment(&Sec);
770
771 Sec.setOrdinal(SectionIndex++);
772 }
773
774 // Assign layout order indices to sections and fragments.
775 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
776 MCSection *Sec = Layout.getSectionOrder()[i];
777 Sec->setLayoutOrder(i);
778
779 unsigned FragmentIndex = 0;
780 for (MCFragment &Frag : *Sec)
781 Frag.setLayoutOrder(FragmentIndex++);
782 }
783
784 // Layout until everything fits.
785 while (layoutOnce(Layout))
786 if (getContext().hadError())
787 return;
788
789 DEBUG_WITH_TYPE("mc-dump", {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mc-dump")) { { errs() << "assembler backend - post-relaxation\n--\n"
; dump(); }; } } while (false)
790 errs() << "assembler backend - post-relaxation\n--\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mc-dump")) { { errs() << "assembler backend - post-relaxation\n--\n"
; dump(); }; } } while (false)
791 dump(); })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mc-dump")) { { errs() << "assembler backend - post-relaxation\n--\n"
; dump(); }; } } while (false)
;
792
793 // Finalize the layout, including fragment lowering.
794 finishLayout(Layout);
795
796 DEBUG_WITH_TYPE("mc-dump", {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mc-dump")) { { errs() << "assembler backend - final-layout\n--\n"
; dump(); }; } } while (false)
797 errs() << "assembler backend - final-layout\n--\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mc-dump")) { { errs() << "assembler backend - final-layout\n--\n"
; dump(); }; } } while (false)
798 dump(); })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("mc-dump")) { { errs() << "assembler backend - final-layout\n--\n"
; dump(); }; } } while (false)
;
799
800 // Allow the object writer a chance to perform post-layout binding (for
801 // example, to set the index fields in the symbol data).
802 getWriter().executePostLayoutBinding(*this, Layout);
803
804 // Evaluate and apply the fixups, generating relocation entries as necessary.
805 for (MCSection &Sec : *this) {
806 for (MCFragment &Frag : Sec) {
807 // Data and relaxable fragments both have fixups. So only process
808 // those here.
809 // FIXME: Is there a better way to do this? MCEncodedFragmentWithFixups
810 // being templated makes this tricky.
811 if (isa<MCEncodedFragment>(&Frag) &&
812 isa<MCCompactEncodedInstFragment>(&Frag))
813 continue;
814 if (!isa<MCEncodedFragment>(&Frag) && !isa<MCCVDefRangeFragment>(&Frag) &&
815 !isa<MCAlignFragment>(&Frag))
816 continue;
817 ArrayRef<MCFixup> Fixups;
818 MutableArrayRef<char> Contents;
819 const MCSubtargetInfo *STI = nullptr;
820 if (auto *FragWithFixups = dyn_cast<MCDataFragment>(&Frag)) {
821 Fixups = FragWithFixups->getFixups();
822 Contents = FragWithFixups->getContents();
823 STI = FragWithFixups->getSubtargetInfo();
824 assert(!FragWithFixups->hasInstructions() || STI != nullptr)((!FragWithFixups->hasInstructions() || STI != nullptr) ? static_cast
<void> (0) : __assert_fail ("!FragWithFixups->hasInstructions() || STI != nullptr"
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 824, __PRETTY_FUNCTION__))
;
825 } else if (auto *FragWithFixups = dyn_cast<MCRelaxableFragment>(&Frag)) {
826 Fixups = FragWithFixups->getFixups();
827 Contents = FragWithFixups->getContents();
828 STI = FragWithFixups->getSubtargetInfo();
829 assert(!FragWithFixups->hasInstructions() || STI != nullptr)((!FragWithFixups->hasInstructions() || STI != nullptr) ? static_cast
<void> (0) : __assert_fail ("!FragWithFixups->hasInstructions() || STI != nullptr"
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 829, __PRETTY_FUNCTION__))
;
830 } else if (auto *FragWithFixups = dyn_cast<MCCVDefRangeFragment>(&Frag)) {
831 Fixups = FragWithFixups->getFixups();
832 Contents = FragWithFixups->getContents();
833 } else if (auto *FragWithFixups = dyn_cast<MCDwarfLineAddrFragment>(&Frag)) {
834 Fixups = FragWithFixups->getFixups();
835 Contents = FragWithFixups->getContents();
836 } else if (auto *AF = dyn_cast<MCAlignFragment>(&Frag)) {
837 // Insert fixup type for code alignment if the target define
838 // shouldInsertFixupForCodeAlign target hook.
839 if (Sec.UseCodeAlign() && AF->hasEmitNops()) {
840 getBackend().shouldInsertFixupForCodeAlign(*this, Layout, *AF);
841 }
842 continue;
843 } else
844 llvm_unreachable("Unknown fragment with fixups!")::llvm::llvm_unreachable_internal("Unknown fragment with fixups!"
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 844)
;
845 for (const MCFixup &Fixup : Fixups) {
846 uint64_t FixedValue;
847 bool IsResolved;
848 MCValue Target;
849 std::tie(Target, FixedValue, IsResolved) =
850 handleFixup(Layout, Frag, Fixup);
851 getBackend().applyFixup(*this, Fixup, Target, Contents, FixedValue,
852 IsResolved, STI);
853 }
854 }
855 }
856}
857
858void MCAssembler::Finish() {
859 // Create the layout object.
860 MCAsmLayout Layout(*this);
861 layout(Layout);
862
863 // Write the object file.
864 stats::ObjectBytes += getWriter().writeObject(*this, Layout);
865}
866
867bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
868 const MCRelaxableFragment *DF,
869 const MCAsmLayout &Layout) const {
870 assert(getBackendPtr() && "Expected assembler backend")((getBackendPtr() && "Expected assembler backend") ? static_cast
<void> (0) : __assert_fail ("getBackendPtr() && \"Expected assembler backend\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 870, __PRETTY_FUNCTION__))
;
871 MCValue Target;
872 uint64_t Value;
873 bool WasForced;
874 bool Resolved = evaluateFixup(Layout, Fixup, DF, Target, Value, WasForced);
875 if (Target.getSymA() &&
876 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_X86_ABS8 &&
877 Fixup.getKind() == FK_Data_1)
878 return false;
879 return getBackend().fixupNeedsRelaxationAdvanced(Fixup, Resolved, Value, DF,
880 Layout, WasForced);
881}
882
883bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
884 const MCAsmLayout &Layout) const {
885 assert(getBackendPtr() && "Expected assembler backend")((getBackendPtr() && "Expected assembler backend") ? static_cast
<void> (0) : __assert_fail ("getBackendPtr() && \"Expected assembler backend\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 885, __PRETTY_FUNCTION__))
;
886 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
887 // are intentionally pushing out inst fragments, or because we relaxed a
888 // previous instruction to one that doesn't need relaxation.
889 if (!getBackend().mayNeedRelaxation(F->getInst(), *F->getSubtargetInfo()))
890 return false;
891
892 for (const MCFixup &Fixup : F->getFixups())
893 if (fixupNeedsRelaxation(Fixup, F, Layout))
894 return true;
895
896 return false;
897}
898
899bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
900 MCRelaxableFragment &F) {
901 assert(getEmitterPtr() &&((getEmitterPtr() && "Expected CodeEmitter defined for relaxInstruction"
) ? static_cast<void> (0) : __assert_fail ("getEmitterPtr() && \"Expected CodeEmitter defined for relaxInstruction\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 902, __PRETTY_FUNCTION__))
902 "Expected CodeEmitter defined for relaxInstruction")((getEmitterPtr() && "Expected CodeEmitter defined for relaxInstruction"
) ? static_cast<void> (0) : __assert_fail ("getEmitterPtr() && \"Expected CodeEmitter defined for relaxInstruction\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 902, __PRETTY_FUNCTION__))
;
903 if (!fragmentNeedsRelaxation(&F, Layout))
904 return false;
905
906 ++stats::RelaxedInstructions;
907
908 // FIXME-PERF: We could immediately lower out instructions if we can tell
909 // they are fully resolved, to avoid retesting on later passes.
910
911 // Relax the fragment.
912
913 MCInst Relaxed;
914 getBackend().relaxInstruction(F.getInst(), *F.getSubtargetInfo(), Relaxed);
915
916 // Encode the new instruction.
917 //
918 // FIXME-PERF: If it matters, we could let the target do this. It can
919 // probably do so more efficiently in many cases.
920 SmallVector<MCFixup, 4> Fixups;
921 SmallString<256> Code;
922 raw_svector_ostream VecOS(Code);
923 getEmitter().encodeInstruction(Relaxed, VecOS, Fixups, *F.getSubtargetInfo());
924
925 // Update the fragment.
926 F.setInst(Relaxed);
927 F.getContents() = Code;
928 F.getFixups() = Fixups;
929
930 return true;
931}
932
933bool MCAssembler::relaxPaddingFragment(MCAsmLayout &Layout,
934 MCPaddingFragment &PF) {
935 assert(getBackendPtr() && "Expected assembler backend")((getBackendPtr() && "Expected assembler backend") ? static_cast
<void> (0) : __assert_fail ("getBackendPtr() && \"Expected assembler backend\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 935, __PRETTY_FUNCTION__))
;
936 uint64_t OldSize = PF.getSize();
937 if (!getBackend().relaxFragment(&PF, Layout))
938 return false;
939 uint64_t NewSize = PF.getSize();
940
941 ++stats::PaddingFragmentsRelaxations;
942 stats::PaddingFragmentsBytes += NewSize;
943 stats::PaddingFragmentsBytes -= OldSize;
944 return true;
945}
946
947bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
948 uint64_t OldSize = LF.getContents().size();
949 int64_t Value;
950 bool Abs = LF.getValue().evaluateKnownAbsolute(Value, Layout);
951 if (!Abs)
952 report_fatal_error("sleb128 and uleb128 expressions must be absolute");
953 SmallString<8> &Data = LF.getContents();
954 Data.clear();
955 raw_svector_ostream OSE(Data);
956 // The compiler can generate EH table assembly that is impossible to assemble
957 // without either adding padding to an LEB fragment or adding extra padding
958 // to a later alignment fragment. To accommodate such tables, relaxation can
959 // only increase an LEB fragment size here, not decrease it. See PR35809.
960 if (LF.isSigned())
961 encodeSLEB128(Value, OSE, OldSize);
962 else
963 encodeULEB128(Value, OSE, OldSize);
964 return OldSize != LF.getContents().size();
965}
966
967bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
968 MCDwarfLineAddrFragment &DF) {
969 MCContext &Context = Layout.getAssembler().getContext();
970 uint64_t OldSize = DF.getContents().size();
971 int64_t AddrDelta;
972 bool Abs;
973 if (getBackend().requiresDiffExpressionRelocations())
974 Abs = DF.getAddrDelta().evaluateAsAbsolute(AddrDelta, Layout);
975 else {
976 Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
977 assert(Abs && "We created a line delta with an invalid expression")((Abs && "We created a line delta with an invalid expression"
) ? static_cast<void> (0) : __assert_fail ("Abs && \"We created a line delta with an invalid expression\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 977, __PRETTY_FUNCTION__))
;
978 }
979 int64_t LineDelta;
980 LineDelta = DF.getLineDelta();
981 SmallVectorImpl<char> &Data = DF.getContents();
982 Data.clear();
983 raw_svector_ostream OSE(Data);
984 DF.getFixups().clear();
985
986 if (Abs) {
987 MCDwarfLineAddr::Encode(Context, getDWARFLinetableParams(), LineDelta,
988 AddrDelta, OSE);
989 } else {
990 uint32_t Offset;
991 uint32_t Size;
992 bool SetDelta = MCDwarfLineAddr::FixedEncode(Context,
993 getDWARFLinetableParams(),
994 LineDelta, AddrDelta,
995 OSE, &Offset, &Size);
996 // Add Fixups for address delta or new address.
997 const MCExpr *FixupExpr;
998 if (SetDelta) {
999 FixupExpr = &DF.getAddrDelta();
1000 } else {
1001 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(&DF.getAddrDelta());
1002 FixupExpr = ABE->getLHS();
1003 }
1004 DF.getFixups().push_back(
1005 MCFixup::create(Offset, FixupExpr,
1006 MCFixup::getKindForSize(Size, false /*isPCRel*/)));
1007 }
1008
1009 return OldSize != Data.size();
1010}
1011
1012bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
1013 MCDwarfCallFrameFragment &DF) {
1014 MCContext &Context = Layout.getAssembler().getContext();
1015 uint64_t OldSize = DF.getContents().size();
1016 int64_t AddrDelta;
1017 bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
1018 assert(Abs && "We created call frame with an invalid expression")((Abs && "We created call frame with an invalid expression"
) ? static_cast<void> (0) : __assert_fail ("Abs && \"We created call frame with an invalid expression\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 1018, __PRETTY_FUNCTION__))
;
1019 (void) Abs;
1020 SmallString<8> &Data = DF.getContents();
1021 Data.clear();
1022 raw_svector_ostream OSE(Data);
1023 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OSE);
1024 return OldSize != Data.size();
1025}
1026
1027bool MCAssembler::relaxCVInlineLineTable(MCAsmLayout &Layout,
1028 MCCVInlineLineTableFragment &F) {
1029 unsigned OldSize = F.getContents().size();
1030 getContext().getCVContext().encodeInlineLineTable(Layout, F);
1031 return OldSize != F.getContents().size();
1032}
1033
1034bool MCAssembler::relaxCVDefRange(MCAsmLayout &Layout,
1035 MCCVDefRangeFragment &F) {
1036 unsigned OldSize = F.getContents().size();
1037 getContext().getCVContext().encodeDefRange(Layout, F);
1038 return OldSize != F.getContents().size();
1039}
1040
1041bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec) {
1042 // Holds the first fragment which needed relaxing during this layout. It will
1043 // remain NULL if none were relaxed.
1044 // When a fragment is relaxed, all the fragments following it should get
1045 // invalidated because their offset is going to change.
1046 MCFragment *FirstRelaxedFragment = nullptr;
1047
1048 // Attempt to relax all the fragments in the section.
1049 for (MCSection::iterator I = Sec.begin(), IE = Sec.end(); I != IE; ++I) {
1050 // Check if this is a fragment that needs relaxation.
1051 bool RelaxedFrag = false;
1052 switch(I->getKind()) {
1053 default:
1054 break;
1055 case MCFragment::FT_Relaxable:
1056 assert(!getRelaxAll() &&((!getRelaxAll() && "Did not expect a MCRelaxableFragment in RelaxAll mode"
) ? static_cast<void> (0) : __assert_fail ("!getRelaxAll() && \"Did not expect a MCRelaxableFragment in RelaxAll mode\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 1057, __PRETTY_FUNCTION__))
1057 "Did not expect a MCRelaxableFragment in RelaxAll mode")((!getRelaxAll() && "Did not expect a MCRelaxableFragment in RelaxAll mode"
) ? static_cast<void> (0) : __assert_fail ("!getRelaxAll() && \"Did not expect a MCRelaxableFragment in RelaxAll mode\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 1057, __PRETTY_FUNCTION__))
;
1058 RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I));
1059 break;
1060 case MCFragment::FT_Dwarf:
1061 RelaxedFrag = relaxDwarfLineAddr(Layout,
1062 *cast<MCDwarfLineAddrFragment>(I));
1063 break;
1064 case MCFragment::FT_DwarfFrame:
1065 RelaxedFrag =
1066 relaxDwarfCallFrameFragment(Layout,
1067 *cast<MCDwarfCallFrameFragment>(I));
1068 break;
1069 case MCFragment::FT_LEB:
1070 RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I));
1071 break;
1072 case MCFragment::FT_Padding:
1073 RelaxedFrag = relaxPaddingFragment(Layout, *cast<MCPaddingFragment>(I));
1074 break;
1075 case MCFragment::FT_CVInlineLines:
1076 RelaxedFrag =
1077 relaxCVInlineLineTable(Layout, *cast<MCCVInlineLineTableFragment>(I));
1078 break;
1079 case MCFragment::FT_CVDefRange:
1080 RelaxedFrag = relaxCVDefRange(Layout, *cast<MCCVDefRangeFragment>(I));
1081 break;
1082 }
1083 if (RelaxedFrag && !FirstRelaxedFragment)
1084 FirstRelaxedFragment = &*I;
1085 }
1086 if (FirstRelaxedFragment) {
1087 Layout.invalidateFragmentsFrom(FirstRelaxedFragment);
1088 return true;
1089 }
1090 return false;
1091}
1092
1093bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
1094 ++stats::RelaxationSteps;
1095
1096 bool WasRelaxed = false;
1097 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1098 MCSection &Sec = *it;
1099 while (layoutSectionOnce(Layout, Sec))
1100 WasRelaxed = true;
1101 }
1102
1103 return WasRelaxed;
1104}
1105
1106void MCAssembler::finishLayout(MCAsmLayout &Layout) {
1107 assert(getBackendPtr() && "Expected assembler backend")((getBackendPtr() && "Expected assembler backend") ? static_cast
<void> (0) : __assert_fail ("getBackendPtr() && \"Expected assembler backend\""
, "/build/llvm-toolchain-snapshot-9~svn361465/lib/MC/MCAssembler.cpp"
, 1107, __PRETTY_FUNCTION__))
;
1108 // The layout is done. Mark every fragment as valid.
1109 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
1110 MCSection &Section = *Layout.getSectionOrder()[i];
1111 Layout.getFragmentOffset(&*Section.rbegin());
1112 computeFragmentSize(Layout, *Section.rbegin());
1113 }
1114 getBackend().finishLayout(*this, Layout);
1115}
1116
1117#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1118LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void MCAssembler::dump() const{
1119 raw_ostream &OS = errs();
1120
1121 OS << "<MCAssembler\n";
1122 OS << " Sections:[\n ";
1123 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
1124 if (it != begin()) OS << ",\n ";
1125 it->dump();
1126 }
1127 OS << "],\n";
1128 OS << " Symbols:[";
1129
1130 for (const_symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
1131 if (it != symbol_begin()) OS << ",\n ";
1132 OS << "(";
1133 it->dump();
1134 OS << ", Index:" << it->getIndex() << ", ";
1135 OS << ")";
1136 }
1137 OS << "]>\n";
1138}
1139#endif