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"
16 // Shift value for STT_* flags. 7 possible values. 3 bits.
19 // Shift value for STB_* flags. 4 possible values, 2 bits.
22 // Shift value for STV_* flags. 4 possible values, 2 bits.
25 // Shift value for STO_* flags. 3 bits. All the values are between 0x20 and
26 // 0xe0, so we shift right by 5 before storing.
30 ELF_IsSignature_Shift
= 10,
33 ELF_WeakrefUsedInReloc_Shift
= 11,
36 ELF_BindingSet_Shift
= 12,
39 ELF_IsMemoryTagged_Shift
= 13,
43 void MCSymbolELF::setBinding(unsigned Binding
) const {
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 {
98 llvm_unreachable("Unsupported Binding");
102 case ELF::STT_OBJECT
:
108 case ELF::STT_SECTION
:
111 case ELF::STT_COMMON
:
117 case ELF::STT_GNU_IFUNC
:
121 uint32_t OtherFlags
= getFlags() & ~(0x7 << ELF_STT_Shift
);
122 setFlags(OtherFlags
| (Val
<< ELF_STT_Shift
));
125 unsigned MCSymbolELF::getType() const {
126 uint32_t Val
= (Flags
>> ELF_STT_Shift
) & 7;
129 llvm_unreachable("Invalid value");
131 return ELF::STT_NOTYPE
;
133 return ELF::STT_OBJECT
;
135 return ELF::STT_FUNC
;
137 return ELF::STT_SECTION
;
139 return ELF::STT_COMMON
;
143 return ELF::STT_GNU_IFUNC
;
147 void MCSymbolELF::setVisibility(unsigned Visibility
) {
148 assert(Visibility
== ELF::STV_DEFAULT
|| Visibility
== ELF::STV_INTERNAL
||
149 Visibility
== ELF::STV_HIDDEN
|| Visibility
== ELF::STV_PROTECTED
);
151 uint32_t OtherFlags
= getFlags() & ~(0x3 << ELF_STV_Shift
);
152 setFlags(OtherFlags
| (Visibility
<< ELF_STV_Shift
));
155 unsigned MCSymbolELF::getVisibility() const {
156 unsigned Visibility
= (Flags
>> ELF_STV_Shift
) & 3;
160 void MCSymbolELF::setOther(unsigned Other
) {
161 assert((Other
& 0x1f) == 0);
163 assert(Other
<= 0x7);
164 uint32_t OtherFlags
= getFlags() & ~(0x7 << ELF_STO_Shift
);
165 setFlags(OtherFlags
| (Other
<< ELF_STO_Shift
));
168 unsigned MCSymbolELF::getOther() const {
169 unsigned Other
= (Flags
>> ELF_STO_Shift
) & 7;
173 void MCSymbolELF::setIsWeakrefUsedInReloc() const {
174 uint32_t OtherFlags
= getFlags() & ~(0x1 << ELF_WeakrefUsedInReloc_Shift
);
175 setFlags(OtherFlags
| (1 << ELF_WeakrefUsedInReloc_Shift
));
178 bool MCSymbolELF::isWeakrefUsedInReloc() const {
179 return getFlags() & (0x1 << ELF_WeakrefUsedInReloc_Shift
);
182 void MCSymbolELF::setIsSignature() const {
183 uint32_t OtherFlags
= getFlags() & ~(0x1 << ELF_IsSignature_Shift
);
184 setFlags(OtherFlags
| (1 << ELF_IsSignature_Shift
));
187 bool MCSymbolELF::isSignature() const {
188 return getFlags() & (0x1 << ELF_IsSignature_Shift
);
191 void MCSymbolELF::setIsBindingSet() const {
192 uint32_t OtherFlags
= getFlags() & ~(0x1 << ELF_BindingSet_Shift
);
193 setFlags(OtherFlags
| (1 << ELF_BindingSet_Shift
));
196 bool MCSymbolELF::isBindingSet() const {
197 return getFlags() & (0x1 << ELF_BindingSet_Shift
);
200 bool MCSymbolELF::isMemtag() const {
201 return getFlags() & (0x1 << ELF_IsMemoryTagged_Shift
);
204 void MCSymbolELF::setMemtag(bool Tagged
) {
205 uint32_t OtherFlags
= getFlags() & ~(1 << ELF_IsMemoryTagged_Shift
);
207 setFlags(OtherFlags
| (1 << ELF_IsMemoryTagged_Shift
));
209 setFlags(OtherFlags
);