Bug Summary

File:lib/MC/WinCOFFStreamer.cpp
Warning:line 269, column 1
Potential leak of memory pointed to by 'Fragment'

Annotated Source Code

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