blob: 12c724f6b1ee3be3ffb42a532bd6a2eb190a02d4 [file] [log] [blame]
Rafael Espindola569f3822015-06-02 20:38:46 +00001//===- 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
Rafael Espindola569f3822015-06-02 20:38:46 +000010#include "llvm/MC/MCSymbolELF.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000011#include "llvm/BinaryFormat/ELF.h"
Rafael Espindola569f3822015-06-02 20:38:46 +000012#include "llvm/MC/MCFixupKindInfo.h"
Rafael Espindola569f3822015-06-02 20:38:46 +000013
14namespace llvm {
15
Rafael Espindolac2128562015-06-04 00:47:43 +000016namespace {
17enum {
Rafael Espindolacddcaba2015-06-04 05:59:23 +000018 // Shift value for STT_* flags. 7 possible values. 3 bits.
19 ELF_STT_Shift = 0,
20
21 // Shift value for STB_* flags. 4 possible values, 2 bits.
22 ELF_STB_Shift = 3,
23
24 // Shift value for STV_* flags. 4 possible values, 2 bits.
25 ELF_STV_Shift = 5,
26
27 // Shift value for STO_* flags. 3 bits. All the values are between 0x20 and
28 // 0xe0, so we shift right by 5 before storing.
29 ELF_STO_Shift = 7,
30
31 // One bit.
32 ELF_IsSignature_Shift = 10,
33
34 // One bit.
35 ELF_WeakrefUsedInReloc_Shift = 11,
36
37 // One bit.
Rafael Espindolaa1e31b42015-06-17 20:08:20 +000038 ELF_BindingSet_Shift = 12
Rafael Espindolac2128562015-06-04 00:47:43 +000039};
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +000040}
Rafael Espindolac2128562015-06-04 00:47:43 +000041
Rafael Espindola569f3822015-06-02 20:38:46 +000042void MCSymbolELF::setBinding(unsigned Binding) const {
Rafael Espindolacddcaba2015-06-04 05:59:23 +000043 setIsBindingSet();
Rafael Espindola7931a612017-02-02 21:26:06 +000044 if (getType() == ELF::STT_SECTION && Binding != ELF::STB_LOCAL)
45 setType(ELF::STT_NOTYPE);
Rafael Espindolacddcaba2015-06-04 05:59:23 +000046 unsigned Val;
47 switch (Binding) {
48 default:
49 llvm_unreachable("Unsupported Binding");
50 case ELF::STB_LOCAL:
51 Val = 0;
52 break;
53 case ELF::STB_GLOBAL:
54 Val = 1;
55 break;
56 case ELF::STB_WEAK:
57 Val = 2;
58 break;
59 case ELF::STB_GNU_UNIQUE:
60 Val = 3;
61 break;
62 }
63 uint32_t OtherFlags = getFlags() & ~(0x3 << ELF_STB_Shift);
64 setFlags(OtherFlags | (Val << ELF_STB_Shift));
Rafael Espindola569f3822015-06-02 20:38:46 +000065}
66
67unsigned MCSymbolELF::getBinding() const {
Rafael Espindolaf3857c32015-06-03 21:41:59 +000068 if (isBindingSet()) {
Rafael Espindolacddcaba2015-06-04 05:59:23 +000069 uint32_t Val = (getFlags() & (0x3 << ELF_STB_Shift)) >> ELF_STB_Shift;
70 switch (Val) {
71 default:
72 llvm_unreachable("Invalid value");
73 case 0:
74 return ELF::STB_LOCAL;
75 case 1:
76 return ELF::STB_GLOBAL;
77 case 2:
78 return ELF::STB_WEAK;
79 case 3:
80 return ELF::STB_GNU_UNIQUE;
81 }
Rafael Espindolaf3857c32015-06-03 21:41:59 +000082 }
83
84 if (isDefined())
85 return ELF::STB_LOCAL;
86 if (isUsedInReloc())
87 return ELF::STB_GLOBAL;
Rafael Espindola5eb74812015-06-03 21:52:06 +000088 if (isWeakrefUsedInReloc())
89 return ELF::STB_WEAK;
Rafael Espindolaf3857c32015-06-03 21:41:59 +000090 if (isSignature())
91 return ELF::STB_LOCAL;
92 return ELF::STB_GLOBAL;
Rafael Espindola569f3822015-06-02 20:38:46 +000093}
94
95void MCSymbolELF::setType(unsigned Type) const {
Rafael Espindolacddcaba2015-06-04 05:59:23 +000096 unsigned Val;
Rafael Espindola7931a612017-02-02 21:26:06 +000097 if (Type == ELF::STT_SECTION && getBinding() != ELF::STB_LOCAL)
98 return;
Rafael Espindolacddcaba2015-06-04 05:59:23 +000099 switch (Type) {
100 default:
101 llvm_unreachable("Unsupported Binding");
102 case ELF::STT_NOTYPE:
103 Val = 0;
104 break;
105 case ELF::STT_OBJECT:
106 Val = 1;
107 break;
108 case ELF::STT_FUNC:
109 Val = 2;
110 break;
111 case ELF::STT_SECTION:
112 Val = 3;
113 break;
114 case ELF::STT_COMMON:
115 Val = 4;
116 break;
117 case ELF::STT_TLS:
118 Val = 5;
119 break;
120 case ELF::STT_GNU_IFUNC:
121 Val = 6;
122 break;
123 }
124 uint32_t OtherFlags = getFlags() & ~(0x7 << ELF_STT_Shift);
125 setFlags(OtherFlags | (Val << ELF_STT_Shift));
Rafael Espindola569f3822015-06-02 20:38:46 +0000126}
127
128unsigned MCSymbolELF::getType() const {
Rafael Espindolacddcaba2015-06-04 05:59:23 +0000129 uint32_t Val = (getFlags() & (0x7 << ELF_STT_Shift)) >> ELF_STT_Shift;
130 switch (Val) {
131 default:
132 llvm_unreachable("Invalid value");
133 case 0:
134 return ELF::STT_NOTYPE;
135 case 1:
136 return ELF::STT_OBJECT;
137 case 2:
138 return ELF::STT_FUNC;
139 case 3:
140 return ELF::STT_SECTION;
141 case 4:
142 return ELF::STT_COMMON;
143 case 5:
144 return ELF::STT_TLS;
145 case 6:
146 return ELF::STT_GNU_IFUNC;
147 }
Rafael Espindola569f3822015-06-02 20:38:46 +0000148}
149
Rafael Espindola569f3822015-06-02 20:38:46 +0000150void MCSymbolELF::setVisibility(unsigned Visibility) {
151 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
152 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
153
154 uint32_t OtherFlags = getFlags() & ~(0x3 << ELF_STV_Shift);
155 setFlags(OtherFlags | (Visibility << ELF_STV_Shift));
156}
157
158unsigned MCSymbolELF::getVisibility() const {
159 unsigned Visibility = (getFlags() & (0x3 << ELF_STV_Shift)) >> ELF_STV_Shift;
160 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
161 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
162 return Visibility;
163}
164
Rafael Espindola569f3822015-06-02 20:38:46 +0000165void MCSymbolELF::setOther(unsigned Other) {
Rafael Espindolacddcaba2015-06-04 05:59:23 +0000166 assert((Other & 0x1f) == 0);
167 Other >>= 5;
168 assert(Other <= 0x7);
169 uint32_t OtherFlags = getFlags() & ~(0x7 << ELF_STO_Shift);
Rafael Espindola569f3822015-06-02 20:38:46 +0000170 setFlags(OtherFlags | (Other << ELF_STO_Shift));
171}
172
173unsigned MCSymbolELF::getOther() const {
Rafael Espindolacddcaba2015-06-04 05:59:23 +0000174 unsigned Other = (getFlags() & (0x7 << ELF_STO_Shift)) >> ELF_STO_Shift;
175 return Other << 5;
Rafael Espindola569f3822015-06-02 20:38:46 +0000176}
Rafael Espindola2b9f4dc2015-06-03 21:30:10 +0000177
Rafael Espindolacddcaba2015-06-04 05:59:23 +0000178void MCSymbolELF::setIsWeakrefUsedInReloc() const {
179 uint32_t OtherFlags = getFlags() & ~(0x1 << ELF_WeakrefUsedInReloc_Shift);
180 setFlags(OtherFlags | (1 << ELF_WeakrefUsedInReloc_Shift));
181}
Rafael Espindola5eb74812015-06-03 21:52:06 +0000182
Rafael Espindolacddcaba2015-06-04 05:59:23 +0000183bool MCSymbolELF::isWeakrefUsedInReloc() const {
184 return getFlags() & (0x1 << ELF_WeakrefUsedInReloc_Shift);
185}
Rafael Espindola5eb74812015-06-03 21:52:06 +0000186
Rafael Espindolacddcaba2015-06-04 05:59:23 +0000187void MCSymbolELF::setIsSignature() const {
188 uint32_t OtherFlags = getFlags() & ~(0x1 << ELF_IsSignature_Shift);
189 setFlags(OtherFlags | (1 << ELF_IsSignature_Shift));
190}
Rafael Espindolaf3857c32015-06-03 21:41:59 +0000191
Rafael Espindolacddcaba2015-06-04 05:59:23 +0000192bool MCSymbolELF::isSignature() const {
193 return getFlags() & (0x1 << ELF_IsSignature_Shift);
194}
195
196void MCSymbolELF::setIsBindingSet() const {
197 uint32_t OtherFlags = getFlags() & ~(0x1 << ELF_BindingSet_Shift);
198 setFlags(OtherFlags | (1 << ELF_BindingSet_Shift));
199}
200
201bool MCSymbolELF::isBindingSet() const {
202 return getFlags() & (0x1 << ELF_BindingSet_Shift);
203}
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +0000204}