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 {
46 llvm_unreachable("Unsupported Binding");
56 case ELF::STB_GNU_UNIQUE
:
60 uint32_t OtherFlags
= getFlags() & ~(0x3 << ELF_STB_Shift
);
61 setFlags(OtherFlags
| (Val
<< ELF_STB_Shift
));
64 unsigned MCSymbolELF::getBinding() const {
66 uint32_t Val
= (Flags
>> ELF_STB_Shift
) & 3;
69 llvm_unreachable("Invalid value");
71 return ELF::STB_LOCAL
;
73 return ELF::STB_GLOBAL
;
77 return ELF::STB_GNU_UNIQUE
;
82 return ELF::STB_LOCAL
;
84 return ELF::STB_GLOBAL
;
85 if (isWeakrefUsedInReloc())
88 return ELF::STB_LOCAL
;
89 return ELF::STB_GLOBAL
;
92 void MCSymbolELF::setType(unsigned Type
) const {
96 llvm_unreachable("Unsupported Binding");
100 case ELF::STT_OBJECT
:
106 case ELF::STT_SECTION
:
109 case ELF::STT_COMMON
:
115 case ELF::STT_GNU_IFUNC
:
119 uint32_t OtherFlags
= getFlags() & ~(0x7 << ELF_STT_Shift
);
120 setFlags(OtherFlags
| (Val
<< ELF_STT_Shift
));
123 unsigned MCSymbolELF::getType() const {
124 uint32_t Val
= (Flags
>> ELF_STT_Shift
) & 7;
127 llvm_unreachable("Invalid value");
129 return ELF::STT_NOTYPE
;
131 return ELF::STT_OBJECT
;
133 return ELF::STT_FUNC
;
135 return ELF::STT_SECTION
;
137 return ELF::STT_COMMON
;
141 return ELF::STT_GNU_IFUNC
;
145 void MCSymbolELF::setVisibility(unsigned Visibility
) {
146 assert(Visibility
== ELF::STV_DEFAULT
|| Visibility
== ELF::STV_INTERNAL
||
147 Visibility
== ELF::STV_HIDDEN
|| Visibility
== ELF::STV_PROTECTED
);
149 uint32_t OtherFlags
= getFlags() & ~(0x3 << ELF_STV_Shift
);
150 setFlags(OtherFlags
| (Visibility
<< ELF_STV_Shift
));
153 unsigned MCSymbolELF::getVisibility() const {
154 unsigned Visibility
= (Flags
>> ELF_STV_Shift
) & 3;
158 void MCSymbolELF::setOther(unsigned Other
) {
159 assert((Other
& 0x1f) == 0);
161 assert(Other
<= 0x7);
162 uint32_t OtherFlags
= getFlags() & ~(0x7 << ELF_STO_Shift
);
163 setFlags(OtherFlags
| (Other
<< ELF_STO_Shift
));
166 unsigned MCSymbolELF::getOther() const {
167 unsigned Other
= (Flags
>> ELF_STO_Shift
) & 7;
171 void MCSymbolELF::setIsWeakrefUsedInReloc() const {
172 uint32_t OtherFlags
= getFlags() & ~(0x1 << ELF_WeakrefUsedInReloc_Shift
);
173 setFlags(OtherFlags
| (1 << ELF_WeakrefUsedInReloc_Shift
));
176 bool MCSymbolELF::isWeakrefUsedInReloc() const {
177 return getFlags() & (0x1 << ELF_WeakrefUsedInReloc_Shift
);
180 void MCSymbolELF::setIsSignature() const {
181 uint32_t OtherFlags
= getFlags() & ~(0x1 << ELF_IsSignature_Shift
);
182 setFlags(OtherFlags
| (1 << ELF_IsSignature_Shift
));
185 bool MCSymbolELF::isSignature() const {
186 return getFlags() & (0x1 << ELF_IsSignature_Shift
);
189 void MCSymbolELF::setIsBindingSet() const {
190 uint32_t OtherFlags
= getFlags() & ~(0x1 << ELF_BindingSet_Shift
);
191 setFlags(OtherFlags
| (1 << ELF_BindingSet_Shift
));
194 bool MCSymbolELF::isBindingSet() const {
195 return getFlags() & (0x1 << ELF_BindingSet_Shift
);