Bug Summary

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