llvm.org GIT mirror llvm / d52a244
MachO: remove weird ARM/Thumb interface from MachOObjectFile Only one consumer (llvm-objdump) actually cared about the fact that there were two triples. Others were actively working around the fact that the Triple returned by getArch might have been invalid. As for llvm-objdump, it needs to be acutely aware of both Triples anyway, so being generic in the exposed API is no benefit. Also rename the version of getArch returning a Triple. Users were having to pass an unwanted nullptr to disambiguate the two, which was nasty. The only functional change here is that armv7m and armv7em object files no longer crash llvm-objdump. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@267249 91177308-0d34-0410-b5e6-96231b3b80d8 Tim Northover 3 years ago
13 changed file(s) with 47 addition(s) and 100 deletion(s). Raw diff Collapse all Expand all
260260
261261 StringRef getFileFormatName() const override;
262262 unsigned getArch() const override;
263 Triple getArch(const char **McpuDefault, Triple *ThumbTriple) const;
263 Triple getArchTriple(const char **McpuDefault = nullptr) const;
264264
265265 relocation_iterator section_rel_begin(unsigned Index) const;
266266 relocation_iterator section_rel_end(unsigned Index) const;
406406 StringRef &Suffix);
407407
408408 static Triple::ArchType getArch(uint32_t CPUType);
409 static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
410 const char **McpuDefault = nullptr);
411 static Triple getThumbArch(uint32_t CPUType, uint32_t CPUSubType,
412 const char **McpuDefault = nullptr);
413 static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
414 const char **McpuDefault, Triple *ThumbTriple);
409 static Triple getArchTriple(uint32_t CPUType, uint32_t CPUSubType,
410 const char **McpuDefault = nullptr);
415411 static bool isValidArch(StringRef ArchFlag);
416412 static Triple getHostArch();
417413
5757 uint32_t getSize() const { return Header.size; }
5858 uint32_t getAlign() const { return Header.align; }
5959 std::string getArchTypeName() const {
60 Triple T = MachOObjectFile::getArch(Header.cputype, Header.cpusubtype);
60 Triple T =
61 MachOObjectFile::getArchTriple(Header.cputype, Header.cpusubtype);
6162 return T.getArchName();
6263 }
6364
11831183 }
11841184 }
11851185
1186 Triple MachOObjectFile::getArch(uint32_t CPUType, uint32_t CPUSubType,
1187 const char **McpuDefault) {
1186 Triple MachOObjectFile::getArchTriple(uint32_t CPUType, uint32_t CPUSubType,
1187 const char **McpuDefault) {
11881188 if (McpuDefault)
11891189 *McpuDefault = nullptr;
11901190
12241224 case MachO::CPU_SUBTYPE_ARM_V7EM:
12251225 if (McpuDefault)
12261226 *McpuDefault = "cortex-m4";
1227 return Triple("armv7em-apple-darwin");
1227 return Triple("thumbv7em-apple-darwin");
12281228 case MachO::CPU_SUBTYPE_ARM_V7K:
12291229 return Triple("armv7k-apple-darwin");
12301230 case MachO::CPU_SUBTYPE_ARM_V7M:
12311231 if (McpuDefault)
12321232 *McpuDefault = "cortex-m3";
1233 return Triple("armv7m-apple-darwin");
1233 return Triple("thumbv7m-apple-darwin");
12341234 case MachO::CPU_SUBTYPE_ARM_V7S:
12351235 return Triple("armv7s-apple-darwin");
12361236 default:
12601260 default:
12611261 return Triple();
12621262 }
1263 }
1264
1265 Triple MachOObjectFile::getThumbArch(uint32_t CPUType, uint32_t CPUSubType,
1266 const char **McpuDefault) {
1267 if (McpuDefault)
1268 *McpuDefault = nullptr;
1269
1270 switch (CPUType) {
1271 case MachO::CPU_TYPE_ARM:
1272 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1273 case MachO::CPU_SUBTYPE_ARM_V4T:
1274 return Triple("thumbv4t-apple-darwin");
1275 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1276 return Triple("thumbv5e-apple-darwin");
1277 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1278 return Triple("xscale-apple-darwin");
1279 case MachO::CPU_SUBTYPE_ARM_V6:
1280 return Triple("thumbv6-apple-darwin");
1281 case MachO::CPU_SUBTYPE_ARM_V6M:
1282 if (McpuDefault)
1283 *McpuDefault = "cortex-m0";
1284 return Triple("thumbv6m-apple-darwin");
1285 case MachO::CPU_SUBTYPE_ARM_V7:
1286 return Triple("thumbv7-apple-darwin");
1287 case MachO::CPU_SUBTYPE_ARM_V7EM:
1288 if (McpuDefault)
1289 *McpuDefault = "cortex-m4";
1290 return Triple("thumbv7em-apple-darwin");
1291 case MachO::CPU_SUBTYPE_ARM_V7K:
1292 return Triple("thumbv7k-apple-darwin");
1293 case MachO::CPU_SUBTYPE_ARM_V7M:
1294 if (McpuDefault)
1295 *McpuDefault = "cortex-m3";
1296 return Triple("thumbv7m-apple-darwin");
1297 case MachO::CPU_SUBTYPE_ARM_V7S:
1298 return Triple("thumbv7s-apple-darwin");
1299 default:
1300 return Triple();
1301 }
1302 default:
1303 return Triple();
1304 }
1305 }
1306
1307 Triple MachOObjectFile::getArch(uint32_t CPUType, uint32_t CPUSubType,
1308 const char **McpuDefault, Triple *ThumbTriple) {
1309 Triple T = MachOObjectFile::getArch(CPUType, CPUSubType, McpuDefault);
1310 *ThumbTriple = MachOObjectFile::getThumbArch(CPUType, CPUSubType,
1311 McpuDefault);
1312 return T;
13131263 }
13141264
13151265 Triple MachOObjectFile::getHostArch() {
13411291 return getArch(getCPUType(this));
13421292 }
13431293
1344 Triple MachOObjectFile::getArch(const char **McpuDefault,
1345 Triple *ThumbTriple) const {
1346 *ThumbTriple = getThumbArch(Header.cputype, Header.cpusubtype, McpuDefault);
1347 return getArch(Header.cputype, Header.cpusubtype, McpuDefault);
1294 Triple MachOObjectFile::getArchTriple(const char **McpuDefault) const {
1295 return getArchTriple(Header.cputype, Header.cpusubtype, McpuDefault);
13481296 }
13491297
13501298 relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
55 # you can extend this file with as much object files and symbols as needed.
66
77 ---
8 triple: 'thumbv7-apple-darwin'
8 triple: 'armv7-apple-darwin'
99 objects:
1010 - filename: 1.o
1111 symbols:
1313
1414 ARMV7: ---
1515 ARMV7-NOT: ...
16 ARMV7: triple: 'thumbv7-apple-darwin'
16 ARMV7: triple: 'armv7-apple-darwin'
1717 ARMV7-NOT: ...
1818 ARMV7: sym: _armv7_var
1919 ARMV7-NOT: ---
2020
2121 ARMV7S: ---
2222 ARMV7S-NOT: ...
23 ARMV7S: triple: 'thumbv7s-apple-darwin'
23 ARMV7S: triple: 'armv7s-apple-darwin'
2424 ARMV7S-NOT: ...
2525 ARMV7S: sym: _armv7s_var
2626 ARMV7S-NOT: ---
0 @ RUN: llvm-mc < %s -triple thumbv7m-apple-macho -filetype=obj | llvm-objdump -macho -d - | FileCheck %s
1 @ RUN: llvm-mc < %s -triple thumbv7em-apple-macho -filetype=obj | llvm-objdump -macho -d - | FileCheck %s
2
3 .thumb
4 .thumb_func _t
5 _t:
6 @ A nice Cortex-M only instruction to make sure the default CPU is sound.
7 msr msp, r0
8
9 @ CHECK: msr msp, r0
0 RUN: echo 0 | llvm-symbolizer -obj=%p/Inputs/fat.o -default-arch=x86_64 | FileCheck --check-prefix=X86_64 %s
11 RUN: echo 0 | llvm-symbolizer -obj=%p/Inputs/fat.o -default-arch=x86_64h | FileCheck --check-prefix=X86_64H %s
22 RUN: echo 0 | llvm-symbolizer -obj=%p/Inputs/fat.o -default-arch=armv7 | FileCheck --check-prefix=ARMV7 %s
3 RUN: echo 0 | llvm-symbolizer -obj=%p/Inputs/fat.o -default-arch=armv7em | FileCheck --check-prefix=ARMV7EM %s
4 RUN: echo 0 | llvm-symbolizer -obj=%p/Inputs/fat.o -default-arch=armv7m | FileCheck --check-prefix=ARMV7M %s
3 RUN: echo 0 | llvm-symbolizer -obj=%p/Inputs/fat.o -default-arch=thumbv7em | FileCheck --check-prefix=ARMV7EM %s
4 RUN: echo 0 | llvm-symbolizer -obj=%p/Inputs/fat.o -default-arch=thumbv7m | FileCheck --check-prefix=ARMV7M %s
55
66 X86_64: x86_64_function
77 X86_64H: x86_64h_function
1717
1818 namespace llvm {
1919 namespace dsymutil {
20
21 Triple BinaryHolder::getTriple(const object::MachOObjectFile &Obj) {
22 // If a ThumbTriple is returned, use it instead of the standard
23 // one. This is because the thumb triple always allows to create a
24 // target, whereas the non-thumb one might not.
25 Triple ThumbTriple;
26 Triple T = Obj.getArch(nullptr, &ThumbTriple);
27 return ThumbTriple.getArch() ? ThumbTriple : T;
28 }
2920
3021 static std::vector
3122 getMachOFatMemoryBuffers(StringRef Filename, MemoryBuffer &Mem,
174165 BinaryHolder::getObjfileForArch(const Triple &T) {
175166 for (const auto &Obj : CurrentObjectFiles) {
176167 if (const auto *MachO = dyn_cast(Obj.get())) {
177 if (getTriple(*MachO).str() == T.str())
168 if (MachO->getArchTriple().str() == T.str())
178169 return *MachO;
179170 } else if (Obj->getArch() == T.getArch())
180171 return *Obj;
127127 return Err;
128128 return cast(*ErrOrObj);
129129 }
130
131 static Triple getTriple(const object::MachOObjectFile &Obj);
132130 };
133131 }
134132 }
136136 }
137137
138138 static std::string getArchName(const object::MachOObjectFile &Obj) {
139 Triple ThumbTriple;
140 Triple T = Obj.getArch(nullptr, &ThumbTriple);
139 Triple T = Obj.getArchTriple();
141140 return T.getArchName();
142141 }
143142
145144 MachODebugMapParser::parseOneBinary(const MachOObjectFile &MainBinary,
146145 StringRef BinaryPath) {
147146 loadMainBinarySymbols(MainBinary);
148 Result =
149 make_unique(BinaryHolder::getTriple(MainBinary), BinaryPath);
147 Result = make_unique(MainBinary.getArchTriple(), BinaryPath);
150148 MainBinaryStrings = MainBinary.getStringTableData();
151149 for (const SymbolRef &Symbol : MainBinary.symbols()) {
152150 const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
307305 return false;
308306 }
309307
310 Triple T;
311308 for (const auto *Binary : *MainBinOrError)
312 if (shouldLinkArch(Archs, Binary->getArch(nullptr, &T).getArchName()))
309 if (shouldLinkArch(Archs, Binary->getArchTriple().getArchName()))
313310 dumpOneBinaryStab(*Binary, BinaryPath);
314311
315312 return true;
325322 return Error;
326323
327324 std::vector> Results;
328 Triple T;
329325 for (const auto *Binary : *MainBinOrError)
330 if (shouldLinkArch(Archs, Binary->getArch(nullptr, &T).getArchName()))
326 if (shouldLinkArch(Archs, Binary->getArchTriple().getArchName()))
331327 Results.push_back(parseOneBinary(*Binary, BinaryPath));
332328
333329 return std::move(Results);
10041004 Triple T;
10051005 if (MachO->is64Bit()) {
10061006 H_64 = MachO->MachOObjectFile::getHeader64();
1007 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1007 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
10081008 } else {
10091009 H = MachO->MachOObjectFile::getHeader();
1010 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1010 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
10111011 }
10121012 if (std::none_of(
10131013 ArchFlags.begin(), ArchFlags.end(),
141141 const char **McpuDefault,
142142 const Target **ThumbTarget) {
143143 // Figure out the target triple.
144 llvm::Triple TT(TripleName);
144145 if (TripleName.empty()) {
145 llvm::Triple TT("unknown-unknown-unknown");
146 llvm::Triple ThumbTriple = Triple();
147 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
146 TT = MachOObj->getArchTriple(McpuDefault);
148147 TripleName = TT.str();
148 }
149
150 if (TT.getArch() == Triple::arm) {
151 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
152 // that support ARM are also capable of Thumb mode.
153 llvm::Triple ThumbTriple = TT;
154 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
155 ThumbTriple.setArchName(ThumbName);
149156 ThumbTripleName = ThumbTriple.str();
150157 }
151158
11561163 Triple T;
11571164 if (MachO->is64Bit()) {
11581165 H_64 = MachO->MachOObjectFile::getHeader64();
1159 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1166 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
11601167 } else {
11611168 H = MachO->MachOObjectFile::getHeader();
1162 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1169 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
11631170 }
11641171 unsigned i;
11651172 for (i = 0; i < ArchFlags.size(); ++i) {
437437 Triple T;
438438 if (MachO->is64Bit()) {
439439 H_64 = MachO->MachOObjectFile::getHeader64();
440 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
440 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
441441 } else {
442442 H = MachO->MachOObjectFile::getHeader();
443 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
443 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
444444 }
445445 unsigned i;
446446 for (i = 0; i < ArchFlags.size(); ++i) {