xref: /llvm-project/llvm/lib/MC/MCSymbolELF.cpp (revision 8c52a9b0f611e4daa09c00e0da15b44b9528dbe5)
1 //===- lib/MC/MCSymbolELF.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 #include "llvm/MC/MCAssembler.h"
11 #include "llvm/MC/MCSymbolELF.h"
12 #include "llvm/MC/MCELFSymbolFlags.h"
13 #include "llvm/MC/MCFixupKindInfo.h"
14 #include "llvm/Support/ELF.h"
15 
16 namespace llvm {
17 
18 void MCSymbolELF::setBinding(unsigned Binding) const {
19   BindingSet = true;
20   assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
21          Binding == ELF::STB_WEAK || Binding == ELF::STB_GNU_UNIQUE);
22   uint32_t OtherFlags = getFlags() & ~(0xf << ELF_STB_Shift);
23   setFlags(OtherFlags | (Binding << ELF_STB_Shift));
24 }
25 
26 unsigned MCSymbolELF::getBinding() const {
27   if (isBindingSet()) {
28     uint32_t Binding = (getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
29     assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
30            Binding == ELF::STB_WEAK || Binding == ELF::STB_GNU_UNIQUE);
31     return Binding;
32   }
33 
34   if (isDefined())
35     return ELF::STB_LOCAL;
36   if (isUsedInReloc())
37     return ELF::STB_GLOBAL;
38   if (isSignature())
39     return ELF::STB_LOCAL;
40   return ELF::STB_GLOBAL;
41 }
42 
43 void MCSymbolELF::setType(unsigned Type) const {
44   assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
45          Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
46          Type == ELF::STT_COMMON || Type == ELF::STT_TLS ||
47          Type == ELF::STT_GNU_IFUNC);
48 
49   uint32_t OtherFlags = getFlags() & ~(0xf << ELF_STT_Shift);
50   setFlags(OtherFlags | (Type << ELF_STT_Shift));
51 }
52 
53 unsigned MCSymbolELF::getType() const {
54   uint32_t Type = (getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
55   assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
56          Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
57          Type == ELF::STT_COMMON || Type == ELF::STT_TLS ||
58          Type == ELF::STT_GNU_IFUNC);
59   return Type;
60 }
61 
62 // Visibility is stored in the first two bits of st_other
63 // st_other values are stored in the second byte of get/setFlags
64 void MCSymbolELF::setVisibility(unsigned Visibility) {
65   assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
66          Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
67 
68   uint32_t OtherFlags = getFlags() & ~(0x3 << ELF_STV_Shift);
69   setFlags(OtherFlags | (Visibility << ELF_STV_Shift));
70 }
71 
72 unsigned MCSymbolELF::getVisibility() const {
73   unsigned Visibility = (getFlags() & (0x3 << ELF_STV_Shift)) >> ELF_STV_Shift;
74   assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
75          Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
76   return Visibility;
77 }
78 
79 // Other is stored in the last six bits of st_other
80 // st_other values are stored in the second byte of get/setFlags
81 void MCSymbolELF::setOther(unsigned Other) {
82   uint32_t OtherFlags = getFlags() & ~(0x3f << ELF_STO_Shift);
83   setFlags(OtherFlags | (Other << ELF_STO_Shift));
84 }
85 
86 unsigned MCSymbolELF::getOther() const {
87   unsigned Other = (getFlags() & (0x3f << ELF_STO_Shift)) >> ELF_STO_Shift;
88   return Other;
89 }
90 
91 void MCSymbolELF::setUsedInReloc() const {
92   UsedInReloc = true;
93 }
94 
95 bool MCSymbolELF::isUsedInReloc() const {
96   return UsedInReloc;
97 }
98 
99 void MCSymbolELF::setIsSignature() const { IsSignature = true; }
100 
101 bool MCSymbolELF::isSignature() const { return IsSignature; }
102 }
103