Coverage Report

Created: 2019-02-21 13:17

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/MC/MCSymbol.h
Line
Count
Source
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
  };
52
53
  /// A symbol can contain an Offset, or Value, or be Common, but never more
54
  /// than one of these.
55
  enum Contents : uint8_t {
56
    SymContentsUnset,
57
    SymContentsOffset,
58
    SymContentsVariable,
59
    SymContentsCommon,
60
  };
61
62
  // Special sentinal value for the absolute pseudo fragment.
63
  static MCFragment *AbsolutePseudoFragment;
64
65
  /// If a symbol has a Fragment, the section is implied, so we only need
66
  /// one pointer.
67
  /// The special AbsolutePseudoFragment value is for absolute symbols.
68
  /// If this is a variable symbol, this caches the variable value's fragment.
69
  /// FIXME: We might be able to simplify this by having the asm streamer create
70
  /// dummy fragments.
71
  /// If this is a section, then it gives the symbol is defined in. This is null
72
  /// for undefined symbols.
73
  ///
74
  /// If this is a fragment, then it gives the fragment this symbol's value is
75
  /// relative to, if any.
76
  ///
77
  /// For the 'HasName' integer, this is true if this symbol is named.
78
  /// A named symbol will have a pointer to the name allocated in the bytes
79
  /// immediately prior to the MCSymbol.
80
  mutable PointerIntPair<MCFragment *, 1> FragmentAndHasName;
81
82
  /// IsTemporary - True if this is an assembler temporary label, which
83
  /// typically does not survive in the .o file's symbol table.  Usually
84
  /// "Lfoo" or ".foo".
85
  unsigned IsTemporary : 1;
86
87
  /// True if this symbol can be redefined.
88
  unsigned IsRedefinable : 1;
89
90
  /// IsUsed - True if this symbol has been used.
91
  mutable unsigned IsUsed : 1;
92
93
  mutable unsigned IsRegistered : 1;
94
95
  /// This symbol is visible outside this translation unit.
96
  mutable unsigned IsExternal : 1;
97
98
  /// This symbol is private extern.
99
  mutable unsigned IsPrivateExtern : 1;
100
101
  /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
102
  /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
103
  unsigned Kind : 3;
104
105
  /// True if we have created a relocation that uses this symbol.
106
  mutable unsigned IsUsedInReloc : 1;
107
108
  /// This is actually a Contents enumerator, but is unsigned to avoid sign
109
  /// extension and achieve better bitpacking with MSVC.
110
  unsigned SymbolContents : 2;
111
112
  /// The alignment of the symbol, if it is 'common', or -1.
113
  ///
114
  /// The alignment is stored as log2(align) + 1.  This allows all values from
115
  /// 0 to 2^31 to be stored which is every power of 2 representable by an
116
  /// unsigned.
117
  enum : unsigned { NumCommonAlignmentBits = 5 };
118
  unsigned CommonAlignLog2 : NumCommonAlignmentBits;
119
120
  /// The Flags field is used by object file implementations to store
121
  /// additional per symbol information which is not easily classified.
122
  enum : unsigned { NumFlagsBits = 16 };
123
  mutable uint32_t Flags : NumFlagsBits;
124
125
  /// Index field, for use by the object file implementation.
126
  mutable uint32_t Index = 0;
127
128
  union {
129
    /// The offset to apply to the fragment address to form this symbol's value.
130
    uint64_t Offset;
131
132
    /// The size of the symbol, if it is 'common'.
133
    uint64_t CommonSize;
134
135
    /// If non-null, the value for a variable symbol.
136
    const MCExpr *Value;
137
  };
138
139
  // MCContext creates and uniques these.
140
  friend class MCExpr;
141
  friend class MCContext;
142
143
  /// The name for a symbol.
144
  /// MCSymbol contains a uint64_t so is probably aligned to 8.  On a 32-bit
145
  /// system, the name is a pointer so isn't going to satisfy the 8 byte
146
  /// alignment of uint64_t.  Account for that here.
147
  using NameEntryStorageTy = union {
148
    const StringMapEntry<bool> *NameEntry;
149
    uint64_t AlignmentPadding;
150
  };
151
152
  MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
153
      : IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
154
        IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
155
        Kind(Kind), IsUsedInReloc(false), SymbolContents(SymContentsUnset),
156
9.19M
        CommonAlignLog2(0), Flags(0) {
157
9.19M
    Offset = 0;
158
9.19M
    FragmentAndHasName.setInt(!!Name);
159
9.19M
    if (Name)
160
4.60M
      getNameEntryPtr() = Name;
161
9.19M
  }
162
163
  // Provide custom new/delete as we will only allocate space for a name
164
  // if we need one.
165
  void *operator new(size_t s, const StringMapEntry<bool> *Name,
166
                     MCContext &Ctx);
167
168
private:
169
  void operator delete(void *);
170
  /// Placement delete - required by std, but never called.
171
  void operator delete(void*, unsigned) {
172
    llvm_unreachable("Constructor throws?");
173
  }
174
  /// Placement delete - required by std, but never called.
175
  void operator delete(void*, unsigned, bool) {
176
    llvm_unreachable("Constructor throws?");
177
  }
178
179
4.65M
  MCSection *getSectionPtr() const {
180
4.65M
    if (MCFragment *
F4.65M
= getFragment()) {
181
4.65M
      assert(F != AbsolutePseudoFragment);
182
4.65M
      return F->getParent();
183
4.65M
    }
184
18.4E
    return nullptr;
185
18.4E
  }
186
187
  /// Get a reference to the name field.  Requires that we have a name
188
16.5M
  const StringMapEntry<bool> *&getNameEntryPtr() {
189
16.5M
    assert(FragmentAndHasName.getInt() && "Name is required");
190
16.5M
    NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
191
16.5M
    return (*(Name - 1)).NameEntry;
192
16.5M
  }
193
11.9M
  const StringMapEntry<bool> *&getNameEntryPtr() const {
194
11.9M
    return const_cast<MCSymbol*>(this)->getNameEntryPtr();
195
11.9M
  }
196
197
public:
198
  MCSymbol(const MCSymbol &) = delete;
199
  MCSymbol &operator=(const MCSymbol &) = delete;
200
201
  /// getName - Get the symbol name.
202
11.9M
  StringRef getName() const {
203
11.9M
    if (!FragmentAndHasName.getInt())
204
1.50k
      return StringRef();
205
11.9M
206
11.9M
    return getNameEntryPtr()->first();
207
11.9M
  }
208
209
14.5M
  bool isRegistered() const { return IsRegistered; }
210
6.91M
  void setIsRegistered(bool Value) const { IsRegistered = Value; }
211
212
24.8k
  void setUsedInReloc() const { IsUsedInReloc = true; }
213
21.1M
  bool isUsedInReloc() const { return IsUsedInReloc; }
214
215
  /// \name Accessors
216
  /// @{
217
218
  /// isTemporary - Check if this is an assembler temporary symbol.
219
41.1M
  bool isTemporary() const { return IsTemporary; }
220
221
  /// isUsed - Check if this is used.
222
418
  bool isUsed() const { return IsUsed; }
223
224
  /// Check if this symbol is redefinable.
225
  bool isRedefinable() const { return IsRedefinable; }
226
  /// Mark this symbol as redefinable.
227
919
  void setRedefinable(bool Value) { IsRedefinable = Value; }
228
  /// Prepare this symbol to be redefined.
229
7.11M
  void redefineIfPossible() {
230
7.11M
    if (IsRedefinable) {
231
3
      if (SymbolContents == SymContentsVariable) {
232
3
        Value = nullptr;
233
3
        SymbolContents = SymContentsUnset;
234
3
      }
235
3
      setUndefined();
236
3
      IsRedefinable = false;
237
3
    }
238
7.11M
  }
239
240
  /// @}
241
  /// \name Associated Sections
242
  /// @{
243
244
  /// isDefined - Check if this symbol is defined (i.e., it has an address).
245
  ///
246
  /// Defined symbols are either absolute or in some section.
247
45.8M
  bool isDefined() const { return !isUndefined(); }
248
249
  /// isInSection - Check if this symbol is defined in some section (i.e., it
250
  /// is defined but not absolute).
251
37.7M
  bool isInSection() const {
252
37.7M
    return isDefined() && 
!isAbsolute()31.8M
;
253
37.7M
  }
254
255
  /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
256
76.9M
  bool isUndefined(bool SetUsed = true) const {
257
76.9M
    return getFragment(SetUsed) == nullptr;
258
76.9M
  }
259
260
  /// isAbsolute - Check if this is an absolute symbol.
261
33.2M
  bool isAbsolute() const {
262
33.2M
    return getFragment() == AbsolutePseudoFragment;
263
33.2M
  }
264
265
  /// Get the section associated with a defined, non-absolute symbol.
266
4.65M
  MCSection &getSection() const {
267
4.65M
    assert(isInSection() && "Invalid accessor!");
268
4.65M
    return *getSectionPtr();
269
4.65M
  }
270
271
  /// Mark the symbol as defined in the fragment \p F.
272
13.8M
  void setFragment(MCFragment *F) const {
273
13.8M
    assert(!isVariable() && "Cannot set fragment of variable");
274
13.8M
    FragmentAndHasName.setPointer(F);
275
13.8M
  }
276
277
  /// Mark the symbol as undefined.
278
563k
  void setUndefined() { FragmentAndHasName.setPointer(nullptr); }
279
280
175k
  bool isELF() const { return Kind == SymbolKindELF; }
281
282
  bool isCOFF() const { return Kind == SymbolKindCOFF; }
283
284
  bool isMachO() const { return Kind == SymbolKindMachO; }
285
286
  bool isWasm() const { return Kind == SymbolKindWasm; }
287
288
  /// @}
289
  /// \name Variable Symbols
290
  /// @{
291
292
  /// isVariable - Check if this is a variable symbol.
293
87.0M
  bool isVariable() const {
294
87.0M
    return SymbolContents == SymContentsVariable;
295
87.0M
  }
296
297
  /// getVariableValue - Get the value for variable symbols.
298
2.84M
  const MCExpr *getVariableValue(bool SetUsed = true) const {
299
2.84M
    assert(isVariable() && "Invalid accessor!");
300
2.84M
    IsUsed |= SetUsed;
301
2.84M
    return Value;
302
2.84M
  }
303
304
  void setVariableValue(const MCExpr *Value);
305
306
  /// @}
307
308
  /// Get the (implementation defined) index.
309
2.89M
  uint32_t getIndex() const {
310
2.89M
    return Index;
311
2.89M
  }
312
313
  /// Set the (implementation defined) index.
314
896k
  void setIndex(uint32_t Value) const {
315
896k
    Index = Value;
316
896k
  }
317
318
6.29M
  bool isUnset() const { return SymbolContents == SymContentsUnset; }
319
320
18.1M
  uint64_t getOffset() const {
321
18.1M
    assert((SymbolContents == SymContentsUnset ||
322
18.1M
            SymbolContents == SymContentsOffset) &&
323
18.1M
           "Cannot get offset for a common/variable symbol");
324
18.1M
    return Offset;
325
18.1M
  }
326
5.78M
  void setOffset(uint64_t Value) {
327
5.78M
    assert((SymbolContents == SymContentsUnset ||
328
5.78M
            SymbolContents == SymContentsOffset) &&
329
5.78M
           "Cannot set offset for a common/variable symbol");
330
5.78M
    Offset = Value;
331
5.78M
    SymbolContents = SymContentsOffset;
332
5.78M
  }
333
334
  /// Return the size of a 'common' symbol.
335
11.1k
  uint64_t getCommonSize() const {
336
11.1k
    assert(isCommon() && "Not a 'common' symbol!");
337
11.1k
    return CommonSize;
338
11.1k
  }
339
340
  /// Mark this symbol as being 'common'.
341
  ///
342
  /// \param Size - The size of the symbol.
343
  /// \param Align - The alignment of the symbol.
344
11.3k
  void setCommon(uint64_t Size, unsigned Align) {
345
11.3k
    assert(getOffset() == 0);
346
11.3k
    CommonSize = Size;
347
11.3k
    SymbolContents = SymContentsCommon;
348
11.3k
349
11.3k
    assert((!Align || isPowerOf2_32(Align)) &&
350
11.3k
           "Alignment must be a power of 2");
351
11.3k
    unsigned Log2Align = Log2_32(Align) + 1;
352
11.3k
    assert(Log2Align < (1U << NumCommonAlignmentBits) &&
353
11.3k
           "Out of range alignment");
354
11.3k
    CommonAlignLog2 = Log2Align;
355
11.3k
  }
356
357
  ///  Return the alignment of a 'common' symbol.
358
11.2k
  unsigned getCommonAlignment() const {
359
11.2k
    assert(isCommon() && "Not a 'common' symbol!");
360
11.2k
    return CommonAlignLog2 ? 
(1U << (CommonAlignLog2 - 1))11.2k
:
022
;
361
11.2k
  }
362
363
  /// Declare this symbol as being 'common'.
364
  ///
365
  /// \param Size - The size of the symbol.
366
  /// \param Align - The alignment of the symbol.
367
  /// \return True if symbol was already declared as a different type
368
204
  bool declareCommon(uint64_t Size, unsigned Align) {
369
204
    assert(isCommon() || getOffset() == 0);
370
204
    if(isCommon()) {
371
3
      if(CommonSize != Size || 
getCommonAlignment() != Align2
)
372
1
       return true;
373
201
    } else
374
201
      setCommon(Size, Align);
375
204
    
return false203
;
376
204
  }
377
378
  /// Is this a 'common' symbol.
379
1.10M
  bool isCommon() const {
380
1.10M
    return SymbolContents == SymContentsCommon;
381
1.10M
  }
382
383
172M
  MCFragment *getFragment(bool SetUsed = true) const {
384
172M
    MCFragment *Fragment = FragmentAndHasName.getPointer();
385
172M
    if (Fragment || 
!isVariable()16.5M
)
386
171M
      return Fragment;
387
557k
    Fragment = getVariableValue(SetUsed)->findAssociatedFragment();
388
557k
    FragmentAndHasName.setPointer(Fragment);
389
557k
    return Fragment;
390
557k
  }
391
392
2.62M
  bool isExternal() const { return IsExternal; }
393
388k
  void setExternal(bool Value) const { IsExternal = Value; }
394
395
867k
  bool isPrivateExtern() const { return IsPrivateExtern; }
396
36.3k
  void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
397
398
  /// print - Print the value to the stream \p OS.
399
  void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
400
401
  /// dump - Print the value to stderr.
402
  void dump() const;
403
404
protected:
405
  /// Get the (implementation defined) symbol flags.
406
14.4M
  uint32_t getFlags() const { return Flags; }
407
408
  /// Set the (implementation defined) symbol flags.
409
5.53M
  void setFlags(uint32_t Value) const {
410
5.53M
    assert(Value < (1U << NumFlagsBits) && "Out of range flags");
411
5.53M
    Flags = Value;
412
5.53M
  }
413
414
  /// Modify the flags via a mask
415
6.33M
  void modifyFlags(uint32_t Value, uint32_t Mask) const {
416
6.33M
    assert(Value < (1U << NumFlagsBits) && "Out of range flags");
417
6.33M
    Flags = (Flags & ~Mask) | Value;
418
6.33M
  }
419
};
420
421
29
inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
422
29
  Sym.print(OS, nullptr);
423
29
  return OS;
424
29
}
425
426
} // end namespace llvm
427
428
#endif // LLVM_MC_MCSYMBOL_H