LLVM  5.0.0
ELFObjectFile.cpp
Go to the documentation of this file.
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 
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/Object/ELF.h"
19 #include "llvm/Object/ELFTypes.h"
20 #include "llvm/Object/Error.h"
23 #include "llvm/Support/Endian.h"
26 #include <algorithm>
27 #include <cstddef>
28 #include <cstdint>
29 #include <memory>
30 #include <string>
31 #include <system_error>
32 #include <utility>
33 
34 using namespace llvm;
35 using namespace object;
36 
38  : ObjectFile(Type, Source) {}
39 
42  std::pair<unsigned char, unsigned char> Ident =
44  std::size_t MaxAlignment =
45  1ULL << countTrailingZeros(uintptr_t(Obj.getBufferStart()));
46 
47  if (MaxAlignment < 2)
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
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
66  } else {
68  }
69 
70  if (EC)
71  return EC;
72  return std::move(R);
73 }
74 
77  unsigned PlatformFlags;
78  getPlatformFlags(PlatformFlags);
79 
80  switch (PlatformFlags & ELF::EF_MIPS_ARCH) {
82  break;
84  Features.AddFeature("mips2");
85  break;
87  Features.AddFeature("mips3");
88  break;
90  Features.AddFeature("mips4");
91  break;
93  Features.AddFeature("mips5");
94  break;
96  Features.AddFeature("mips32");
97  break;
99  Features.AddFeature("mips64");
100  break;
102  Features.AddFeature("mips32r2");
103  break;
105  Features.AddFeature("mips64r2");
106  break;
108  Features.AddFeature("mips32r6");
109  break;
111  Features.AddFeature("mips64r6");
112  break;
113  default:
114  llvm_unreachable("Unknown EF_MIPS_ARCH value");
115  }
116 
117  switch (PlatformFlags & ELF::EF_MIPS_MACH) {
119  // No feature associated with this value.
120  break;
122  Features.AddFeature("cnmips");
123  break;
124  default:
125  llvm_unreachable("Unknown EF_MIPS_ARCH value");
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 
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)
148 
152  Features.AddFeature("aclass");
153  break;
155  Features.AddFeature("rclass");
156  if (isV7)
157  Features.AddFeature("hwdiv");
158  break;
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;
172  Features.AddFeature("thumb", false);
173  Features.AddFeature("thumb2", false);
174  break;
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;
186  Features.AddFeature("vfp2", false);
187  Features.AddFeature("vfp3", false);
188  Features.AddFeature("vfp4", false);
189  break;
191  Features.AddFeature("vfp2");
192  break;
195  Features.AddFeature("vfp3");
196  break;
199  Features.AddFeature("vfp4");
200  break;
201  }
202  }
203 
206  default:
207  break;
209  Features.AddFeature("neon", false);
210  Features.AddFeature("fp16", false);
211  break;
213  Features.AddFeature("neon");
214  break;
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;
227  Features.AddFeature("hwdiv", false);
228  Features.AddFeature("hwdiv-arm", false);
229  break;
231  Features.AddFeature("hwdiv");
232  Features.AddFeature("hwdiv-arm");
233  break;
234  }
235  }
236 
237  return Features;
238 }
239 
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.
252 void 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;
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;
305  Triple += "v6sm";
306  break;
308  Triple += "v7em";
309  break;
310  }
311  }
312  if (!isLittleEndian())
313  Triple += "eb";
314 
315  TheTriple.setArchName(Triple);
316 }
bool hasAttribute(unsigned Tag) const
Represents either an error or a value T.
Definition: ErrorOr.h:69
virtual uint16_t getEMachine() const =0
SubtargetFeatures getFeatures() const override
This class is the base class for all object file types.
Definition: ObjectFile.h:189
const char * getBufferStart() const
Definition: MemoryBuffer.h:173
SubtargetFeatures getARMFeatures() const
void setARMSubArch(Triple &TheTriple) const override
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
SubtargetFeatures getMIPSFeatures() const
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
Definition: ELF.h:39
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:281
SubArchType getSubArch() const
getSubArch - get the parsed subarchitecture type for this triple.
Definition: Triple.h:284
std::size_t countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
Definition: MathExtras.h:112
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
void setArchName(StringRef Str)
setArchName - Set the architecture (first) component of the triple by name.
Definition: Triple.cpp:1128
virtual std::error_code getPlatformFlags(unsigned &Result) const
Returns platform-specific object flags, if any.
Definition: ObjectFile.h:286
ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
StringRef getBuffer() const
Definition: MemoryBuffer.h:169
Manages the enabling and disabling of subtarget specific features.
virtual std::error_code getBuildAttributes(ARMAttributeParser &Attributes) const
Definition: ObjectFile.h:292
static ErrorOr< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object)
llvm_unreachable("Unknown RegBank!")
bool isLittleEndian() const
Definition: Binary.h:130
unsigned getAttributeValue(unsigned Tag) const
const FeatureBitset Features