Bug Summary

File:lib/Object/ELFObjectFile.cpp
Warning:line 45, column 12
The result of the '<<' expression is undefined

Annotated Source Code

1//===- ELFObjectFile.cpp - ELF object file implementation -----------------===//
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// Part of the ELFObjectFile class implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/Triple.h"
15#include "llvm/MC/SubtargetFeature.h"
16#include "llvm/Object/ELF.h"
17#include "llvm/Object/ELFObjectFile.h"
18#include "llvm/Object/ELFTypes.h"
19#include "llvm/Object/Error.h"
20#include "llvm/Support/ARMBuildAttributes.h"
21#include "llvm/Support/ARMAttributeParser.h"
22#include "llvm/Support/ELF.h"
23#include "llvm/Support/Endian.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/MathExtras.h"
26#include <algorithm>
27#include <cstddef>
28#include <cstdint>
29#include <memory>
30#include <string>
31#include <system_error>
32#include <utility>
33
34using namespace llvm;
35using namespace object;
36
37ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
38 : ObjectFile(Type, Source) {}
39
40ErrorOr<std::unique_ptr<ObjectFile>>
41ObjectFile::createELFObjectFile(MemoryBufferRef Obj) {
42 std::pair<unsigned char, unsigned char> Ident =
43 getElfArchType(Obj.getBuffer());
44 std::size_t MaxAlignment =
45 1ULL << countTrailingZeros(uintptr_t(Obj.getBufferStart()));
The result of the '<<' expression is undefined
46
47 if (MaxAlignment < 2)
48 return object_error::parse_failed;
49
50 std::error_code EC;
51 std::unique_ptr<ObjectFile> R;
52 if (Ident.first == ELF::ELFCLASS32) {
53 if (Ident.second == ELF::ELFDATA2LSB)
54 R.reset(new ELFObjectFile<ELFType<support::little, false>>(Obj, EC));
55 else if (Ident.second == ELF::ELFDATA2MSB)
56 R.reset(new ELFObjectFile<ELFType<support::big, false>>(Obj, EC));
57 else
58 return object_error::parse_failed;
59 } else if (Ident.first == ELF::ELFCLASS64) {
60 if (Ident.second == ELF::ELFDATA2LSB)
61 R.reset(new ELFObjectFile<ELFType<support::little, true>>(Obj, EC));
62 else if (Ident.second == ELF::ELFDATA2MSB)
63 R.reset(new ELFObjectFile<ELFType<support::big, true>>(Obj, EC));
64 else
65 return object_error::parse_failed;
66 } else {
67 return object_error::parse_failed;
68 }
69
70 if (EC)
71 return EC;
72 return std::move(R);
73}
74
75SubtargetFeatures ELFObjectFileBase::getMIPSFeatures() const {
76 SubtargetFeatures Features;
77 unsigned PlatformFlags;
78 getPlatformFlags(PlatformFlags);
79
80 switch (PlatformFlags & ELF::EF_MIPS_ARCH) {
81 case ELF::EF_MIPS_ARCH_1:
82 break;
83 case ELF::EF_MIPS_ARCH_2:
84 Features.AddFeature("mips2");
85 break;
86 case ELF::EF_MIPS_ARCH_3:
87 Features.AddFeature("mips3");
88 break;
89 case ELF::EF_MIPS_ARCH_4:
90 Features.AddFeature("mips4");
91 break;
92 case ELF::EF_MIPS_ARCH_5:
93 Features.AddFeature("mips5");
94 break;
95 case ELF::EF_MIPS_ARCH_32:
96 Features.AddFeature("mips32");
97 break;
98 case ELF::EF_MIPS_ARCH_64:
99 Features.AddFeature("mips64");
100 break;
101 case ELF::EF_MIPS_ARCH_32R2:
102 Features.AddFeature("mips32r2");
103 break;
104 case ELF::EF_MIPS_ARCH_64R2:
105 Features.AddFeature("mips64r2");
106 break;
107 case ELF::EF_MIPS_ARCH_32R6:
108 Features.AddFeature("mips32r6");
109 break;
110 case ELF::EF_MIPS_ARCH_64R6:
111 Features.AddFeature("mips64r6");
112 break;
113 default:
114 llvm_unreachable("Unknown EF_MIPS_ARCH value")::llvm::llvm_unreachable_internal("Unknown EF_MIPS_ARCH value"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/lib/Object/ELFObjectFile.cpp"
, 114)
;
115 }
116
117 switch (PlatformFlags & ELF::EF_MIPS_MACH) {
118 case ELF::EF_MIPS_MACH_NONE:
119 // No feature associated with this value.
120 break;
121 case ELF::EF_MIPS_MACH_OCTEON:
122 Features.AddFeature("cnmips");
123 break;
124 default:
125 llvm_unreachable("Unknown EF_MIPS_ARCH value")::llvm::llvm_unreachable_internal("Unknown EF_MIPS_ARCH value"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/lib/Object/ELFObjectFile.cpp"
, 125)
;
126 }
127
128 if (PlatformFlags & ELF::EF_MIPS_ARCH_ASE_M16)
129 Features.AddFeature("mips16");
130 if (PlatformFlags & ELF::EF_MIPS_MICROMIPS)
131 Features.AddFeature("micromips");
132
133 return Features;
134}
135
136SubtargetFeatures ELFObjectFileBase::getARMFeatures() const {
137 SubtargetFeatures Features;
138 ARMAttributeParser Attributes;
139 std::error_code EC = getBuildAttributes(Attributes);
140 if (EC)
141 return SubtargetFeatures();
142
143 // both ARMv7-M and R have to support thumb hardware div
144 bool isV7 = false;
145 if (Attributes.hasAttribute(ARMBuildAttrs::CPU_arch))
146 isV7 = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch)
147 == ARMBuildAttrs::v7;
148
149 if (Attributes.hasAttribute(ARMBuildAttrs::CPU_arch_profile)) {
150 switch(Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile)) {
151 case ARMBuildAttrs::ApplicationProfile:
152 Features.AddFeature("aclass");
153 break;
154 case ARMBuildAttrs::RealTimeProfile:
155 Features.AddFeature("rclass");
156 if (isV7)
157 Features.AddFeature("hwdiv");
158 break;
159 case ARMBuildAttrs::MicroControllerProfile:
160 Features.AddFeature("mclass");
161 if (isV7)
162 Features.AddFeature("hwdiv");
163 break;
164 }
165 }
166
167 if (Attributes.hasAttribute(ARMBuildAttrs::THUMB_ISA_use)) {
168 switch(Attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use)) {
169 default:
170 break;
171 case ARMBuildAttrs::Not_Allowed:
172 Features.AddFeature("thumb", false);
173 Features.AddFeature("thumb2", false);
174 break;
175 case ARMBuildAttrs::AllowThumb32:
176 Features.AddFeature("thumb2");
177 break;
178 }
179 }
180
181 if (Attributes.hasAttribute(ARMBuildAttrs::FP_arch)) {
182 switch(Attributes.getAttributeValue(ARMBuildAttrs::FP_arch)) {
183 default:
184 break;
185 case ARMBuildAttrs::Not_Allowed:
186 Features.AddFeature("vfp2", false);
187 Features.AddFeature("vfp3", false);
188 Features.AddFeature("vfp4", false);
189 break;
190 case ARMBuildAttrs::AllowFPv2:
191 Features.AddFeature("vfp2");
192 break;
193 case ARMBuildAttrs::AllowFPv3A:
194 case ARMBuildAttrs::AllowFPv3B:
195 Features.AddFeature("vfp3");
196 break;
197 case ARMBuildAttrs::AllowFPv4A:
198 case ARMBuildAttrs::AllowFPv4B:
199 Features.AddFeature("vfp4");
200 break;
201 }
202 }
203
204 if (Attributes.hasAttribute(ARMBuildAttrs::Advanced_SIMD_arch)) {
205 switch(Attributes.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch)) {
206 default:
207 break;
208 case ARMBuildAttrs::Not_Allowed:
209 Features.AddFeature("neon", false);
210 Features.AddFeature("fp16", false);
211 break;
212 case ARMBuildAttrs::AllowNeon:
213 Features.AddFeature("neon");
214 break;
215 case ARMBuildAttrs::AllowNeon2:
216 Features.AddFeature("neon");
217 Features.AddFeature("fp16");
218 break;
219 }
220 }
221
222 if (Attributes.hasAttribute(ARMBuildAttrs::DIV_use)) {
223 switch(Attributes.getAttributeValue(ARMBuildAttrs::DIV_use)) {
224 default:
225 break;
226 case ARMBuildAttrs::DisallowDIV:
227 Features.AddFeature("hwdiv", false);
228 Features.AddFeature("hwdiv-arm", false);
229 break;
230 case ARMBuildAttrs::AllowDIVExt:
231 Features.AddFeature("hwdiv");
232 Features.AddFeature("hwdiv-arm");
233 break;
234 }
235 }
236
237 return Features;
238}
239
240SubtargetFeatures ELFObjectFileBase::getFeatures() const {
241 switch (getEMachine()) {
242 case ELF::EM_MIPS:
243 return getMIPSFeatures();
244 case ELF::EM_ARM:
245 return getARMFeatures();
246 default:
247 return SubtargetFeatures();
248 }
249}
250
251// FIXME Encode from a tablegen description or target parser.
252void ELFObjectFileBase::setARMSubArch(Triple &TheTriple) const {
253 if (TheTriple.getSubArch() != Triple::NoSubArch)
254 return;
255
256 ARMAttributeParser Attributes;
257 std::error_code EC = getBuildAttributes(Attributes);
258 if (EC)
259 return;
260
261 std::string Triple;
262 // Default to ARM, but use the triple if it's been set.
263 if (TheTriple.getArch() == Triple::thumb ||
264 TheTriple.getArch() == Triple::thumbeb)
265 Triple = "thumb";
266 else
267 Triple = "arm";
268
269 if (Attributes.hasAttribute(ARMBuildAttrs::CPU_arch)) {
270 switch(Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch)) {
271 case ARMBuildAttrs::v4:
272 Triple += "v4";
273 break;
274 case ARMBuildAttrs::v4T:
275 Triple += "v4t";
276 break;
277 case ARMBuildAttrs::v5T:
278 Triple += "v5t";
279 break;
280 case ARMBuildAttrs::v5TE:
281 Triple += "v5te";
282 break;
283 case ARMBuildAttrs::v5TEJ:
284 Triple += "v5tej";
285 break;
286 case ARMBuildAttrs::v6:
287 Triple += "v6";
288 break;
289 case ARMBuildAttrs::v6KZ:
290 Triple += "v6kz";
291 break;
292 case ARMBuildAttrs::v6T2:
293 Triple += "v6t2";
294 break;
295 case ARMBuildAttrs::v6K:
296 Triple += "v6k";
297 break;
298 case ARMBuildAttrs::v7:
299 Triple += "v7";
300 break;
301 case ARMBuildAttrs::v6_M:
302 Triple += "v6m";
303 break;
304 case ARMBuildAttrs::v6S_M:
305 Triple += "v6sm";
306 break;
307 case ARMBuildAttrs::v7E_M:
308 Triple += "v7em";
309 break;
310 }
311 }
312 if (!isLittleEndian())
313 Triple += "eb";
314
315 TheTriple.setArchName(Triple);
316}