Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/MC/MCSymbol.h
Line
Count
Source (jump to first uncovered line)
1
//===- MCSymbol.h - Machine Code Symbols ------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file contains the declaration of the MCSymbol class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_MC_MCSYMBOL_H
14
#define LLVM_MC_MCSYMBOL_H
15
16
#include "llvm/ADT/PointerIntPair.h"
17
#include "llvm/ADT/StringMap.h"
18
#include "llvm/ADT/StringRef.h"
19
#include "llvm/MC/MCFragment.h"
20
#include "llvm/Support/ErrorHandling.h"
21
#include "llvm/Support/MathExtras.h"
22
#include <cassert>
23
#include <cstddef>
24
#include <cstdint>
25
26
namespace llvm {
27
28
class MCAsmInfo;
29
class MCContext;
30
class MCExpr;
31
class MCSection;
32
class raw_ostream;
33
34
/// MCSymbol - Instances of this class represent a symbol name in the MC file,
35
/// and MCSymbols are created and uniqued by the MCContext class.  MCSymbols
36
/// should only be constructed with valid names for the object file.
37
///
38
/// If the symbol is defined/emitted into the current translation unit, the
39
/// Section member is set to indicate what section it lives in.  Otherwise, if
40
/// it is a reference to an external entity, it has a null section.
41
class MCSymbol {
42
protected:
43
  /// The kind of the symbol.  If it is any value other than unset then this
44
  /// class is actually one of the appropriate subclasses of MCSymbol.
45
  enum SymbolKind {
46
    SymbolKindUnset,
47
    SymbolKindCOFF,
48
    SymbolKindELF,
49
    SymbolKindMachO,
50
    SymbolKindWasm,
51
    SymbolKindXCOFF,
52
  };
53
54
  /// A symbol can contain an Offset, or Value, or be Common, but never more
55
  /// than one of these.
56
  enum Contents : uint8_t {
57
    SymContentsUnset,
58
    SymContentsOffset,
59
    SymContentsVariable,
60
    SymContentsCommon,
61
    SymContentsTargetCommon, // Index stores the section index
62
  };
63
64
  // Special sentinal value for the absolute pseudo fragment.
65
  static MCFragment *AbsolutePseudoFragment;
66
67
  /// If a symbol has a Fragment, the section is implied, so we only need
68
  /// one pointer.
69
  /// The special AbsolutePseudoFragment value is for absolute symbols.
70
  /// If this is a variable symbol, this caches the variable value's fragment.
71
  /// FIXME: We might be able to simplify this by having the asm streamer create
72
  /// dummy fragments.
73
  /// If this is a section, then it gives the symbol is defined in. This is null
74
  /// for undefined symbols.
75
  ///
76
  /// If this is a fragment, then it gives the fragment this symbol's value is
77
  /// relative to, if any.
78
  ///
79
  /// For the 'HasName' integer, this is true if this symbol is named.
80
  /// A named symbol will have a pointer to the name allocated in the bytes
81
  /// immediately prior to the MCSymbol.
82
  mutable PointerIntPair<MCFragment *, 1> FragmentAndHasName;
83
84
  /// IsTemporary - True if this is an assembler temporary label, which
85
  /// typically does not survive in the .o file's symbol table.  Usually
86
  /// "Lfoo" or ".foo".
87
  unsigned IsTemporary : 1;
88
89
  /// True if this symbol can be redefined.
90
  unsigned IsRedefinable : 1;
91
92
  /// IsUsed - True if this symbol has been used.
93
  mutable unsigned IsUsed : 1;
94
95
  mutable unsigned IsRegistered : 1;
96
97
  /// This symbol is visible outside this translation unit.
98
  mutable unsigned IsExternal : 1;
99
100
  /// This symbol is private extern.
101
  mutable unsigned IsPrivateExtern : 1;
102
103
  /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
104
  /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
105
  unsigned Kind : 3;
106
107
  /// True if we have created a relocation that uses this symbol.
108
  mutable unsigned IsUsedInReloc : 1;
109
110
  /// This is actually a Contents enumerator, but is unsigned to avoid sign
111
  /// extension and achieve better bitpacking with MSVC.
112
  unsigned SymbolContents : 3;
113
114
  /// The alignment of the symbol, if it is 'common', or -1.
115
  ///
116
  /// The alignment is stored as log2(align) + 1.  This allows all values from
117
  /// 0 to 2^31 to be stored which is every power of 2 representable by an
118
  /// unsigned.
119
  enum : unsigned { NumCommonAlignmentBits = 5 };
120
  unsigned CommonAlignLog2 : NumCommonAlignmentBits;
121
122
  /// The Flags field is used by object file implementations to store
123
  /// additional per symbol information which is not easily classified.
124
  enum : unsigned { NumFlagsBits = 16 };
125
  mutable uint32_t Flags : NumFlagsBits;
126
127
  /// Index field, for use by the object file implementation.
128
  mutable uint32_t Index = 0;
129
130
  union {
131
    /// The offset to apply to the fragment address to form this symbol's value.
132
    uint64_t Offset;
133
134
    /// The size of the symbol, if it is 'common'.
135
    uint64_t CommonSize;
136
137
    /// If non-null, the value for a variable symbol.
138
    const MCExpr *Value;
139
  };
140
141
  // MCContext creates and uniques these.
142
  friend class MCExpr;
143
  friend class MCContext;
144
145
  /// The name for a symbol.
146
  /// MCSymbol contains a uint64_t so is probably aligned to 8.  On a 32-bit
147
  /// system, the name is a pointer so isn't going to satisfy the 8 byte
148
  /// alignment of uint64_t.  Account for that here.
149
  using NameEntryStorageTy = union {
150
    const StringMapEntry<bool> *NameEntry;
151
    uint64_t AlignmentPadding;
152
  };
153
154
  MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
155
      : IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
156
        IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
157
        Kind(Kind), IsUsedInReloc(false), SymbolContents(SymContentsUnset),
158
9.47M
        CommonAlignLog2(0), Flags(0) {
159
9.47M
    Offset = 0;
160
9.47M
    FragmentAndHasName.setInt(!!Name);
161
9.47M
    if (Name)
162
5.07M
      getNameEntryPtr() = Name;
163
9.47M
  }
164
165
  // Provide custom new/delete as we will only allocate space for a name
166
  // if we need one.
167
  void *operator new(size_t s, const StringMapEntry<bool> *Name,
168
                     MCContext &Ctx);
169
170
private:
171
  void operator delete(void *);
172
  /// Placement delete - required by std, but never called.
173
0
  void operator delete(void*, unsigned) {
174
0
    llvm_unreachable("Constructor throws?");
175
0
  }
176
  /// Placement delete - required by std, but never called.
177
0
  void operator delete(void*, unsigned, bool) {
178
0
    llvm_unreachable("Constructor throws?");
179
0
  }
180
181
4.64M
  MCSection *getSectionPtr() const {
182
4.64M
    if (MCFragment *F = getFragment()) {
183
4.64M
      assert(F != AbsolutePseudoFragment);
184
4.64M
      return F->getParent();
185
4.64M
    }
186
1
    return nullptr;
187
1
  }
188
189
  /// Get a reference to the name field.  Requires that we have a name
190
17.4M
  const StringMapEntry<bool> *&getNameEntryPtr() {
191
17.4M
    assert(FragmentAndHasName.getInt() && "Name is required");
192
17.4M
    NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
193
17.4M
    return (*(Name - 1)).NameEntry;
194
17.4M
  }
195
12.3M
  const StringMapEntry<bool> *&getNameEntryPtr() const {
196
12.3M
    return const_cast<MCSymbol*>(this)->getNameEntryPtr();
197
12.3M
  }
198
199
public:
200
  MCSymbol(const MCSymbol &) = delete;
201
  MCSymbol &operator=(const MCSymbol &) = delete;
202
203
  /// getName - Get the symbol name.
204
12.3M
  StringRef getName() const {
205
12.3M
    if (!FragmentAndHasName.getInt())
206
1.66k
      return StringRef();
207
12.3M
208
12.3M
    return getNameEntryPtr()->first();
209
12.3M
  }
210
211
14.4M
  bool isRegistered() const { return IsRegistered; }
212
6.86M
  void setIsRegistered(bool Value) const { IsRegistered = Value; }
213
214
27.1k
  void setUsedInReloc() const { IsUsedInReloc = true; }
215
20.5M
  bool isUsedInReloc() const { return IsUsedInReloc; }
216
217
  /// \name Accessors
218
  /// @{
219
220
  /// isTemporary - Check if this is an assembler temporary symbol.
221
40.2M
  bool isTemporary() const { return IsTemporary; }
222
223
  /// isUsed - Check if this is used.
224
738
  bool isUsed() const { return IsUsed; }
225
226
  /// Check if this symbol is redefinable.
227
0
  bool isRedefinable() const { return IsRedefinable; }
228
  /// Mark this symbol as redefinable.
229
1.37k
  void setRedefinable(bool Value) { IsRedefinable = Value; }
230
  /// Prepare this symbol to be redefined.
231
7.10M
  void redefineIfPossible() {
232
7.10M
    if (IsRedefinable) {
233
3
      if (SymbolContents == SymContentsVariable) {
234
3
        Value = nullptr;
235
3
        SymbolContents = SymContentsUnset;
236
3
      }
237
3
      setUndefined();
238
3
      IsRedefinable = false;
239
3
    }
240
7.10M
  }
241
242
  /// @}
243
  /// \name Associated Sections
244
  /// @{
245
246
  /// isDefined - Check if this symbol is defined (i.e., it has an address).
247
  ///
248
  /// Defined symbols are either absolute or in some section.
249
44.5M
  bool isDefined() const { return !isUndefined(); }
250
251
  /// isInSection - Check if this symbol is defined in some section (i.e., it
252
  /// is defined but not absolute).
253
36.7M
  bool isInSection() const {
254
36.7M
    return isDefined() && 
!isAbsolute()30.9M
;
255
36.7M
  }
256
257
  /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
258
74.4M
  bool isUndefined(bool SetUsed = true) const {
259
74.4M
    return getFragment(SetUsed) == nullptr;
260
74.4M
  }
261
262
  /// isAbsolute - Check if this is an absolute symbol.
263
32.4M
  bool isAbsolute() const {
264
32.4M
    return getFragment() == AbsolutePseudoFragment;
265
32.4M
  }
266
267
  /// Get the section associated with a defined, non-absolute symbol.
268
4.64M
  MCSection &getSection() const {
269
4.64M
    assert(isInSection() && "Invalid accessor!");
270
4.64M
    return *getSectionPtr();
271
4.64M
  }
272
273
  /// Mark the symbol as defined in the fragment \p F.
274
14.0M
  void setFragment(MCFragment *F) const {
275
14.0M
    assert(!isVariable() && "Cannot set fragment of variable");
276
14.0M
    FragmentAndHasName.setPointer(F);
277
14.0M
  }
278
279
  /// Mark the symbol as undefined.
280
543k
  void setUndefined() { FragmentAndHasName.setPointer(nullptr); }
281
282
146k
  bool isELF() const { return Kind == SymbolKindELF; }
283
284
0
  bool isCOFF() const { return Kind == SymbolKindCOFF; }
285
286
0
  bool isMachO() const { return Kind == SymbolKindMachO; }
287
288
0
  bool isWasm() const { return Kind == SymbolKindWasm; }
289
290
8
  bool isXCOFF() const { return Kind == SymbolKindXCOFF; }
291
292
  /// @}
293
  /// \name Variable Symbols
294
  /// @{
295
296
  /// isVariable - Check if this is a variable symbol.
297
83.9M
  bool isVariable() const {
298
83.9M
    return SymbolContents == SymContentsVariable;
299
83.9M
  }
300
301
  /// getVariableValue - Get the value for variable symbols.
302
2.73M
  const MCExpr *getVariableValue(bool SetUsed = true) const {
303
2.73M
    assert(isVariable() && "Invalid accessor!");
304
2.73M
    IsUsed |= SetUsed;
305
2.73M
    return Value;
306
2.73M
  }
307
308
  void setVariableValue(const MCExpr *Value);
309
310
  /// @}
311
312
  /// Get the (implementation defined) index.
313
2.81M
  uint32_t getIndex() const {
314
2.81M
    return Index;
315
2.81M
  }
316
317
  /// Set the (implementation defined) index.
318
959k
  void setIndex(uint32_t Value) const {
319
959k
    Index = Value;
320
959k
  }
321
322
5.84M
  bool isUnset() const { return SymbolContents == SymContentsUnset; }
323
324
17.3M
  uint64_t getOffset() const {
325
17.3M
    assert((SymbolContents == SymContentsUnset ||
326
17.3M
            SymbolContents == SymContentsOffset) &&
327
17.3M
           "Cannot get offset for a common/variable symbol");
328
17.3M
    return Offset;
329
17.3M
  }
330
5.68M
  void setOffset(uint64_t Value) {
331
5.68M
    assert((SymbolContents == SymContentsUnset ||
332
5.68M
            SymbolContents == SymContentsOffset) &&
333
5.68M
           "Cannot set offset for a common/variable symbol");
334
5.68M
    Offset = Value;
335
5.68M
    SymbolContents = SymContentsOffset;
336
5.68M
  }
337
338
  /// Return the size of a 'common' symbol.
339
11.1k
  uint64_t getCommonSize() const {
340
11.1k
    assert(isCommon() && "Not a 'common' symbol!");
341
11.1k
    return CommonSize;
342
11.1k
  }
343
344
  /// Mark this symbol as being 'common'.
345
  ///
346
  /// \param Size - The size of the symbol.
347
  /// \param Align - The alignment of the symbol.
348
  /// \param Target - Is the symbol a target-specific common-like symbol.
349
11.3k
  void setCommon(uint64_t Size, unsigned Align, bool Target = false) {
350
11.3k
    assert(getOffset() == 0);
351
11.3k
    CommonSize = Size;
352
11.3k
    SymbolContents = Target ? 
SymContentsTargetCommon7
:
SymContentsCommon11.3k
;
353
11.3k
354
11.3k
    assert((!Align || isPowerOf2_32(Align)) &&
355
11.3k
           "Alignment must be a power of 2");
356
11.3k
    unsigned Log2Align = Log2_32(Align) + 1;
357
11.3k
    assert(Log2Align < (1U << NumCommonAlignmentBits) &&
358
11.3k
           "Out of range alignment");
359
11.3k
    CommonAlignLog2 = Log2Align;
360
11.3k
  }
361
362
  ///  Return the alignment of a 'common' symbol.
363
11.3k
  unsigned getCommonAlignment() const {
364
11.3k
    assert(isCommon() && "Not a 'common' symbol!");
365
11.3k
    return CommonAlignLog2 ? 
(1U << (CommonAlignLog2 - 1))11.3k
:
022
;
366
11.3k
  }
367
368
  /// Declare this symbol as being 'common'.
369
  ///
370
  /// \param Size - The size of the symbol.
371
  /// \param Align - The alignment of the symbol.
372
  /// \param Target - Is the symbol a target-specific common-like symbol.
373
  /// \return True if symbol was already declared as a different type
374
214
  bool declareCommon(uint64_t Size, unsigned Align, bool Target = false) {
375
214
    assert(isCommon() || getOffset() == 0);
376
214
    if(isCommon()) {
377
3
      if (CommonSize != Size || 
getCommonAlignment() != Align2
||
378
3
          
isTargetCommon() != Target2
)
379
1
        return true;
380
211
    } else
381
211
      setCommon(Size, Align, Target);
382
214
    
return false213
;
383
214
  }
384
385
  /// Is this a 'common' symbol.
386
1.31M
  bool isCommon() const {
387
1.31M
    return SymbolContents == SymContentsCommon ||
388
1.31M
           
SymbolContents == SymContentsTargetCommon1.29M
;
389
1.31M
  }
390
391
  /// Is this a target-specific common-like symbol.
392
420
  bool isTargetCommon() const {
393
420
    return SymbolContents == SymContentsTargetCommon;
394
420
  }
395
396
166M
  MCFragment *getFragment(bool SetUsed = true) const {
397
166M
    MCFragment *Fragment = FragmentAndHasName.getPointer();
398
166M
    if (Fragment || 
!isVariable()16.1M
)
399
165M
      return Fragment;
400
535k
    Fragment = getVariableValue(SetUsed)->findAssociatedFragment();
401
535k
    FragmentAndHasName.setPointer(Fragment);
402
535k
    return Fragment;
403
535k
  }
404
405
2.59M
  bool isExternal() const { return IsExternal; }
406
461k
  void setExternal(bool Value) const { IsExternal = Value; }
407
408
857k
  bool isPrivateExtern() const { return IsPrivateExtern; }
409
36.9k
  void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
410
411
  /// print - Print the value to the stream \p OS.
412
  void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
413
414
  /// dump - Print the value to stderr.
415
  void dump() const;
416
417
protected:
418
  /// Get the (implementation defined) symbol flags.
419
11.2M
  uint32_t getFlags() const { return Flags; }
420
421
  /// Set the (implementation defined) symbol flags.
422
6.67M
  void setFlags(uint32_t Value) const {
423
6.67M
    assert(Value < (1U << NumFlagsBits) && "Out of range flags");
424
6.67M
    Flags = Value;
425
6.67M
  }
426
427
  /// Modify the flags via a mask
428
6.15M
  void modifyFlags(uint32_t Value, uint32_t Mask) const {
429
6.15M
    assert(Value < (1U << NumFlagsBits) && "Out of range flags");
430
6.15M
    Flags = (Flags & ~Mask) | Value;
431
6.15M
  }
432
};
433
434
61
inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
435
61
  Sym.print(OS, nullptr);
436
61
  return OS;
437
61
}
438
439
} // end namespace llvm
440
441
#endif // LLVM_MC_MCSYMBOL_H