1 //===- lib/MC/MCSymbolELF.cpp ---------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCSymbolELF.h"
10 #include "llvm/BinaryFormat/ELF.h"
11 #include "llvm/MC/MCFixupKindInfo.h"
17 // Shift value for STT_* flags. 7 possible values. 3 bits.
20 // Shift value for STB_* flags. 4 possible values, 2 bits.
23 // Shift value for STV_* flags. 4 possible values, 2 bits.
26 // Shift value for STO_* flags. 3 bits. All the values are between 0x20 and
27 // 0xe0, so we shift right by 5 before storing.
31 ELF_IsSignature_Shift
= 10,
34 ELF_WeakrefUsedInReloc_Shift
= 11,
37 ELF_BindingSet_Shift
= 12
41 void MCSymbolELF::setBinding(unsigned Binding
) const {
43 if (getType() == ELF::STT_SECTION
&& Binding
!= ELF::STB_LOCAL
)
44 setType(ELF::STT_NOTYPE
);
48 llvm_unreachable("Unsupported Binding");
58 case ELF::STB_GNU_UNIQUE
:
62 uint32_t OtherFlags
= getFlags() & ~(0x3 << ELF_STB_Shift
);
63 setFlags(OtherFlags
| (Val
<< ELF_STB_Shift
));
66 unsigned MCSymbolELF::getBinding() const {
68 uint32_t Val
= (Flags
>> ELF_STB_Shift
) & 3;
71 llvm_unreachable("Invalid value");
73 return ELF::STB_LOCAL
;
75 return ELF::STB_GLOBAL
;
79 return ELF::STB_GNU_UNIQUE
;
84 return ELF::STB_LOCAL
;
86 return ELF::STB_GLOBAL
;
87 if (isWeakrefUsedInReloc())
90 return ELF::STB_LOCAL
;
91 return ELF::STB_GLOBAL
;
94 void MCSymbolELF::setType(unsigned Type
) const {
96 if (Type
== ELF::STT_SECTION
&& getBinding() != ELF::STB_LOCAL
)
100 llvm_unreachable("Unsupported Binding");
101 case ELF::STT_NOTYPE
:
104 case ELF::STT_OBJECT
:
110 case ELF::STT_SECTION
:
113 case ELF::STT_COMMON
:
119 case ELF::STT_GNU_IFUNC
:
123 uint32_t OtherFlags
= getFlags() & ~(0x7 << ELF_STT_Shift
);
124 setFlags(OtherFlags
| (Val
<< ELF_STT_Shift
));
127 unsigned MCSymbolELF::getType() const {
128 uint32_t Val
= (Flags
>> ELF_STT_Shift
) & 7;
131 llvm_unreachable("Invalid value");
133 return ELF::STT_NOTYPE
;
135 return ELF::STT_OBJECT
;
137 return ELF::STT_FUNC
;
139 return ELF::STT_SECTION
;
141 return ELF::STT_COMMON
;
145 return ELF::STT_GNU_IFUNC
;
149 void MCSymbolELF::setVisibility(unsigned Visibility
) {
150 assert(Visibility
== ELF::STV_DEFAULT
|| Visibility
== ELF::STV_INTERNAL
||
151 Visibility
== ELF::STV_HIDDEN
|| Visibility
== ELF::STV_PROTECTED
);
153 uint32_t OtherFlags
= getFlags() & ~(0x3 << ELF_STV_Shift
);
154 setFlags(OtherFlags
| (Visibility
<< ELF_STV_Shift
));
157 unsigned MCSymbolELF::getVisibility() const {
158 unsigned Visibility
= (Flags
>> ELF_STV_Shift
) & 3;
162 void MCSymbolELF::setOther(unsigned Other
) {
163 assert((Other
& 0x1f) == 0);
165 assert(Other
<= 0x7);
166 uint32_t OtherFlags
= getFlags() & ~(0x7 << ELF_STO_Shift
);
167 setFlags(OtherFlags
| (Other
<< ELF_STO_Shift
));
170 unsigned MCSymbolELF::getOther() const {
171 unsigned Other
= (Flags
>> ELF_STO_Shift
) & 7;
175 void MCSymbolELF::setIsWeakrefUsedInReloc() const {
176 uint32_t OtherFlags
= getFlags() & ~(0x1 << ELF_WeakrefUsedInReloc_Shift
);
177 setFlags(OtherFlags
| (1 << ELF_WeakrefUsedInReloc_Shift
));
180 bool MCSymbolELF::isWeakrefUsedInReloc() const {
181 return getFlags() & (0x1 << ELF_WeakrefUsedInReloc_Shift
);
184 void MCSymbolELF::setIsSignature() const {
185 uint32_t OtherFlags
= getFlags() & ~(0x1 << ELF_IsSignature_Shift
);
186 setFlags(OtherFlags
| (1 << ELF_IsSignature_Shift
));
189 bool MCSymbolELF::isSignature() const {
190 return getFlags() & (0x1 << ELF_IsSignature_Shift
);
193 void MCSymbolELF::setIsBindingSet() const {
194 uint32_t OtherFlags
= getFlags() & ~(0x1 << ELF_BindingSet_Shift
);
195 setFlags(OtherFlags
| (1 << ELF_BindingSet_Shift
));
198 bool MCSymbolELF::isBindingSet() const {
199 return getFlags() & (0x1 << ELF_BindingSet_Shift
);