• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- lld/unittest/MachOTests/MachONormalizedFileBinaryReaderTests.cpp ---===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "../../lib/ReaderWriter/MachO/MachONormalizedFile.h"
10 #include "lld/ReaderWriter/MachOLinkingContext.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/BinaryFormat/MachO.h"
13 #include "llvm/Support/Error.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/Support/YAMLTraits.h"
17 #include "gtest/gtest.h"
18 #include <cstdint>
19 #include <memory>
20 
21 using llvm::SmallString;
22 using llvm::StringRef;
23 using llvm::MemoryBuffer;
24 using llvm::Twine;
25 
26 using namespace lld::mach_o::normalized;
27 using namespace llvm::MachO;
28 
29 static std::unique_ptr<NormalizedFile>
fromBinary(const uint8_t bytes[],unsigned length,StringRef archStr)30 fromBinary(const uint8_t bytes[], unsigned length, StringRef archStr) {
31   StringRef sr((const char*)bytes, length);
32   std::unique_ptr<MemoryBuffer> mb(MemoryBuffer::getMemBuffer(sr, "", false));
33   llvm::Expected<std::unique_ptr<NormalizedFile>> r =
34       lld::mach_o::normalized::readBinary(
35           mb, lld::MachOLinkingContext::archFromName(archStr));
36   EXPECT_FALSE(!r);
37   return std::move(*r);
38 }
39 
40 // The Mach-O object reader uses functions such as read32 or read64
41 // which don't allow unaligned access. Our in-memory object file
42 // needs to be aligned to a larger boundary than uint8_t's.
43 #if _MSC_VER
44 #define FILEBYTES __declspec(align(64)) const uint8_t fileBytes[]
45 #else
46 #define FILEBYTES const uint8_t fileBytes[] __attribute__((aligned(64)))
47 #endif
48 
TEST(BinaryReaderTest,empty_obj_x86_64)49 TEST(BinaryReaderTest, empty_obj_x86_64) {
50   FILEBYTES = {
51       0xcf, 0xfa, 0xed, 0xfe, 0x07, 0x00, 0x00, 0x01,
52       0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
53       0x01, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
54       0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55       0x19, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
56       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60       0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62       0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
63       0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64       0x5f, 0x5f, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00,
65       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66       0x5f, 0x5f, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
67       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70       0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72       0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
73       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
74   std::unique_ptr<NormalizedFile> f =
75       fromBinary(fileBytes, sizeof(fileBytes), "x86_64");
76   EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_x86_64);
77   EXPECT_EQ((int)(f->fileType), MH_OBJECT);
78   EXPECT_EQ((int)(f->flags), (int)MH_SUBSECTIONS_VIA_SYMBOLS);
79   EXPECT_TRUE(f->localSymbols.empty());
80   EXPECT_TRUE(f->globalSymbols.empty());
81   EXPECT_TRUE(f->undefinedSymbols.empty());
82 }
83 
TEST(BinaryReaderTest,empty_obj_x86)84 TEST(BinaryReaderTest, empty_obj_x86) {
85   FILEBYTES = {
86       0xce, 0xfa, 0xed, 0xfe, 0x07, 0x00, 0x00, 0x00,
87       0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
88       0x01, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00,
89       0x00, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
90       0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93       0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
94       0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
95       0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
96       0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x74, 0x65,
97       0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98       0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x54, 0x45,
99       0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101       0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
102       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
104       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
105   std::unique_ptr<NormalizedFile> f =
106       fromBinary(fileBytes, sizeof(fileBytes), "i386");
107   EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_x86);
108   EXPECT_EQ((int)(f->fileType), MH_OBJECT);
109   EXPECT_EQ((int)(f->flags), (int)MH_SUBSECTIONS_VIA_SYMBOLS);
110   EXPECT_TRUE(f->localSymbols.empty());
111   EXPECT_TRUE(f->globalSymbols.empty());
112   EXPECT_TRUE(f->undefinedSymbols.empty());
113 }
114 
TEST(BinaryReaderTest,empty_obj_ppc)115 TEST(BinaryReaderTest, empty_obj_ppc) {
116   FILEBYTES = {
117       0xfe, 0xed, 0xfa, 0xce, 0x00, 0x00, 0x00, 0x12,
118       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
119       0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7c,
120       0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x01,
121       0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00,
122       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98,
125       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
126       0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01,
127       0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x74, 0x65,
128       0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129       0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x54, 0x45,
130       0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98,
133       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134       0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
135       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
136   std::unique_ptr<NormalizedFile> f =
137       fromBinary(fileBytes, sizeof(fileBytes), "ppc");
138   EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_ppc);
139   EXPECT_EQ((int)(f->fileType), MH_OBJECT);
140   EXPECT_EQ((int)(f->flags), (int)MH_SUBSECTIONS_VIA_SYMBOLS);
141   EXPECT_TRUE(f->localSymbols.empty());
142   EXPECT_TRUE(f->globalSymbols.empty());
143   EXPECT_TRUE(f->undefinedSymbols.empty());
144 }
145 
TEST(BinaryReaderTest,empty_obj_armv7)146 TEST(BinaryReaderTest, empty_obj_armv7) {
147   FILEBYTES = {
148       0xce, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x00,
149       0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
150       0x01, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00,
151       0x00, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
152       0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
155       0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
156       0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
157       0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
158       0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x74, 0x65,
159       0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160       0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x54, 0x45,
161       0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
163       0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
164       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
165       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
166       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
167   std::unique_ptr<NormalizedFile> f =
168       fromBinary(fileBytes, sizeof(fileBytes), "armv7");
169   EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_armv7);
170   EXPECT_EQ((int)(f->fileType), MH_OBJECT);
171   EXPECT_EQ((int)(f->flags), (int)MH_SUBSECTIONS_VIA_SYMBOLS);
172   EXPECT_TRUE(f->localSymbols.empty());
173   EXPECT_TRUE(f->globalSymbols.empty());
174   EXPECT_TRUE(f->undefinedSymbols.empty());
175 }
176 
TEST(BinaryReaderTest,empty_obj_x86_64_arm7)177 TEST(BinaryReaderTest, empty_obj_x86_64_arm7) {
178   FILEBYTES = {
179 #include "empty_obj_x86_armv7.txt"
180   };
181   std::unique_ptr<NormalizedFile> f =
182       fromBinary(fileBytes, sizeof(fileBytes), "x86_64");
183   EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_x86_64);
184   EXPECT_EQ((int)(f->fileType), MH_OBJECT);
185   EXPECT_EQ((int)(f->flags), (int)MH_SUBSECTIONS_VIA_SYMBOLS);
186   EXPECT_TRUE(f->localSymbols.empty());
187   EXPECT_TRUE(f->globalSymbols.empty());
188   EXPECT_TRUE(f->undefinedSymbols.empty());
189 
190   std::unique_ptr<NormalizedFile> f2 =
191       fromBinary(fileBytes, sizeof(fileBytes), "armv7");
192   EXPECT_EQ(f2->arch, lld::MachOLinkingContext::arch_armv7);
193   EXPECT_EQ((int)(f2->fileType), MH_OBJECT);
194   EXPECT_EQ((int)(f2->flags), (int)MH_SUBSECTIONS_VIA_SYMBOLS);
195   EXPECT_TRUE(f2->localSymbols.empty());
196   EXPECT_TRUE(f2->globalSymbols.empty());
197   EXPECT_TRUE(f2->undefinedSymbols.empty());
198 }
199 
TEST(BinaryReaderTest,hello_obj_x86_64)200 TEST(BinaryReaderTest, hello_obj_x86_64) {
201   FILEBYTES = {
202     0xCF, 0xFA, 0xED, 0xFE, 0x07, 0x00, 0x00, 0x01,
203     0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
204     0x03, 0x00, 0x00, 0x00, 0x50, 0x01, 0x00, 0x00,
205     0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206     0x19, 0x00, 0x00, 0x00, 0xE8, 0x00, 0x00, 0x00,
207     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
209     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
210     0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211     0x70, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
212     0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
213     0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
214     0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
215     0x5F, 0x5F, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00,
216     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217     0x5F, 0x5F, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
218     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
219     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
220     0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
221     0x70, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
222     0xA4, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
223     0x00, 0x04, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
224     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
225     0x5F, 0x5F, 0x63, 0x73, 0x74, 0x72, 0x69, 0x6E,
226     0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227     0x5F, 0x5F, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
228     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
229     0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230     0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
231     0x9D, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
232     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
233     0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235     0x02, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
236     0xB4, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
237     0xE4, 0x01, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
238     0x0B, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
239     0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
240     0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
241     0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
242     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
245     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
247     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248     0x55, 0x48, 0x89, 0xE5, 0x48, 0x83, 0xEC, 0x10,
249     0x48, 0x8D, 0x3D, 0x00, 0x00, 0x00, 0x00, 0xC7,
250     0x45, 0xFC, 0x00, 0x00, 0x00, 0x00, 0xB0, 0x00,
251     0xE8, 0x00, 0x00, 0x00, 0x00, 0xB9, 0x00, 0x00,
252     0x00, 0x00, 0x89, 0x45, 0xF8, 0x89, 0xC8, 0x48,
253     0x83, 0xC4, 0x10, 0x5D, 0xC3, 0x68, 0x65, 0x6C,
254     0x6C, 0x6F, 0x0A, 0x00, 0x19, 0x00, 0x00, 0x00,
255     0x02, 0x00, 0x00, 0x2D, 0x0B, 0x00, 0x00, 0x00,
256     0x00, 0x00, 0x00, 0x1D, 0x0F, 0x00, 0x00, 0x00,
257     0x0E, 0x02, 0x00, 0x00, 0x2D, 0x00, 0x00, 0x00,
258     0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
259     0x0F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
260     0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
261     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
262     0x00, 0x00, 0x00, 0x00, 0x00, 0x5F, 0x6D, 0x61,
263     0x69, 0x6E, 0x00, 0x5F, 0x70, 0x72, 0x69, 0x6E,
264     0x74, 0x66, 0x00, 0x4C, 0x5F, 0x2E, 0x73, 0x74,
265     0x72, 0x00, 0x00, 0x00 };
266   std::unique_ptr<NormalizedFile> f =
267       fromBinary(fileBytes, sizeof(fileBytes), "x86_64");
268 
269   EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_x86_64);
270   EXPECT_EQ((int)(f->fileType), MH_OBJECT);
271   EXPECT_EQ((int)(f->flags), (int)MH_SUBSECTIONS_VIA_SYMBOLS);
272   EXPECT_EQ(f->sections.size(), 2UL);
273   const Section& text = f->sections[0];
274   EXPECT_TRUE(text.segmentName.equals("__TEXT"));
275   EXPECT_TRUE(text.sectionName.equals("__text"));
276   EXPECT_EQ(text.type, S_REGULAR);
277   EXPECT_EQ(text.attributes,SectionAttr(S_ATTR_PURE_INSTRUCTIONS
278                                       | S_ATTR_SOME_INSTRUCTIONS));
279   EXPECT_EQ((uint16_t)text.alignment, 16U);
280   EXPECT_EQ(text.address, Hex64(0x0));
281   EXPECT_EQ(text.content.size(), 45UL);
282   EXPECT_EQ((int)(text.content[0]), 0x55);
283   EXPECT_EQ((int)(text.content[1]), 0x48);
284   EXPECT_TRUE(text.indirectSymbols.empty());
285   EXPECT_EQ(text.relocations.size(), 2UL);
286   const Relocation& call = text.relocations[0];
287   EXPECT_EQ(call.offset, Hex32(0x19));
288   EXPECT_EQ(call.type, X86_64_RELOC_BRANCH);
289   EXPECT_EQ(call.length, 2);
290   EXPECT_EQ(call.isExtern, true);
291   EXPECT_EQ(call.symbol, 2U);
292   const Relocation& str = text.relocations[1];
293   EXPECT_EQ(str.offset, Hex32(0xB));
294   EXPECT_EQ(str.type, X86_64_RELOC_SIGNED);
295   EXPECT_EQ(str.length, 2);
296   EXPECT_EQ(str.isExtern, true);
297   EXPECT_EQ(str.symbol, 0U);
298 
299   const Section& cstring = f->sections[1];
300   EXPECT_TRUE(cstring.segmentName.equals("__TEXT"));
301   EXPECT_TRUE(cstring.sectionName.equals("__cstring"));
302   EXPECT_EQ(cstring.type, S_CSTRING_LITERALS);
303   EXPECT_EQ(cstring.attributes, SectionAttr(0));
304   EXPECT_EQ((uint16_t)cstring.alignment, 1U);
305   EXPECT_EQ(cstring.address, Hex64(0x02D));
306   EXPECT_EQ(cstring.content.size(), 7UL);
307   EXPECT_EQ((int)(cstring.content[0]), 0x68);
308   EXPECT_EQ((int)(cstring.content[1]), 0x65);
309   EXPECT_EQ((int)(cstring.content[2]), 0x6c);
310   EXPECT_TRUE(cstring.indirectSymbols.empty());
311   EXPECT_TRUE(cstring.relocations.empty());
312 
313   EXPECT_EQ(f->localSymbols.size(), 1UL);
314   const Symbol& strLabel = f->localSymbols[0];
315   EXPECT_EQ(strLabel.type, N_SECT);
316   EXPECT_EQ(strLabel.sect, 2);
317   EXPECT_EQ(strLabel.value, Hex64(0x2D));
318   EXPECT_EQ(f->globalSymbols.size(), 1UL);
319   const Symbol& mainLabel = f->globalSymbols[0];
320   EXPECT_TRUE(mainLabel.name.equals("_main"));
321   EXPECT_EQ(mainLabel.type, N_SECT);
322   EXPECT_EQ(mainLabel.sect, 1);
323   EXPECT_EQ(mainLabel.scope, SymbolScope(N_EXT));
324   EXPECT_EQ(mainLabel.value, Hex64(0x0));
325   EXPECT_EQ(f->undefinedSymbols.size(), 1UL);
326   const Symbol& printfLabel = f->undefinedSymbols[0];
327   EXPECT_TRUE(printfLabel.name.equals("_printf"));
328   EXPECT_EQ(printfLabel.type, N_UNDF);
329   EXPECT_EQ(printfLabel.scope, SymbolScope(N_EXT));
330 }
331 
TEST(BinaryReaderTest,hello_obj_x86)332 TEST(BinaryReaderTest, hello_obj_x86) {
333   FILEBYTES = {
334     0xCE, 0xFA, 0xED, 0xFE, 0x07, 0x00, 0x00, 0x00,
335     0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
336     0x03, 0x00, 0x00, 0x00, 0x28, 0x01, 0x00, 0x00,
337     0x00, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
338     0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
339     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
340     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
341     0x37, 0x00, 0x00, 0x00, 0x44, 0x01, 0x00, 0x00,
342     0x37, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
343     0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
344     0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x74, 0x65,
345     0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
346     0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x54, 0x45,
347     0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
348     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
349     0x30, 0x00, 0x00, 0x00, 0x44, 0x01, 0x00, 0x00,
350     0x04, 0x00, 0x00, 0x00, 0x7C, 0x01, 0x00, 0x00,
351     0x03, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x80,
352     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
353     0x5F, 0x5F, 0x63, 0x73, 0x74, 0x72, 0x69, 0x6E,
354     0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
355     0x5F, 0x5F, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
356     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
357     0x30, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
358     0x74, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
359     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
360     0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
361     0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
362     0x18, 0x00, 0x00, 0x00, 0x94, 0x01, 0x00, 0x00,
363     0x02, 0x00, 0x00, 0x00, 0xAC, 0x01, 0x00, 0x00,
364     0x10, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00,
365     0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
366     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
367     0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
368     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
369     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
370     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
371     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
372     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
373     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
374     0x00, 0x00, 0x00, 0x00, 0x55, 0x89, 0xE5, 0x83,
375     0xEC, 0x18, 0xE8, 0x00, 0x00, 0x00, 0x00, 0x58,
376     0x8D, 0x80, 0x25, 0x00, 0x00, 0x00, 0xC7, 0x45,
377     0xFC, 0x00, 0x00, 0x00, 0x00, 0x89, 0x04, 0x24,
378     0xE8, 0xDF, 0xFF, 0xFF, 0xFF, 0xB9, 0x00, 0x00,
379     0x00, 0x00, 0x89, 0x45, 0xF8, 0x89, 0xC8, 0x83,
380     0xC4, 0x18, 0x5D, 0xC3, 0x68, 0x65, 0x6C, 0x6C,
381     0x6F, 0x0A, 0x00, 0x00, 0x1D, 0x00, 0x00, 0x00,
382     0x01, 0x00, 0x00, 0x0D, 0x0E, 0x00, 0x00, 0xA4,
383     0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA1,
384     0x0B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
385     0x0F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
386     0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
387     0x00, 0x00, 0x00, 0x00, 0x00, 0x5F, 0x6D, 0x61,
388     0x69, 0x6E, 0x00, 0x5F, 0x70, 0x72, 0x69, 0x6E,
389     0x74, 0x66, 0x00, 0x00
390   };
391   std::unique_ptr<NormalizedFile> f =
392       fromBinary(fileBytes, sizeof(fileBytes), "i386");
393 
394   EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_x86);
395   EXPECT_EQ((int)(f->fileType), MH_OBJECT);
396   EXPECT_EQ((int)(f->flags), (int)MH_SUBSECTIONS_VIA_SYMBOLS);
397   EXPECT_EQ(f->sections.size(), 2UL);
398   const Section& text = f->sections[0];
399   EXPECT_TRUE(text.segmentName.equals("__TEXT"));
400   EXPECT_TRUE(text.sectionName.equals("__text"));
401   EXPECT_EQ(text.type, S_REGULAR);
402   EXPECT_EQ(text.attributes,SectionAttr(S_ATTR_PURE_INSTRUCTIONS
403                                       | S_ATTR_SOME_INSTRUCTIONS));
404   EXPECT_EQ((uint16_t)text.alignment, 16U);
405   EXPECT_EQ(text.address, Hex64(0x0));
406   EXPECT_EQ(text.content.size(), 48UL);
407   EXPECT_EQ((int)(text.content[0]), 0x55);
408   EXPECT_EQ((int)(text.content[1]), 0x89);
409   EXPECT_TRUE(text.indirectSymbols.empty());
410   EXPECT_EQ(text.relocations.size(), 3UL);
411   const Relocation& call = text.relocations[0];
412   EXPECT_EQ(call.offset, Hex32(0x1D));
413   EXPECT_EQ(call.scattered, false);
414   EXPECT_EQ(call.type, GENERIC_RELOC_VANILLA);
415   EXPECT_EQ(call.pcRel, true);
416   EXPECT_EQ(call.length, 2);
417   EXPECT_EQ(call.isExtern, true);
418   EXPECT_EQ(call.symbol, 1U);
419   const Relocation& sectDiff = text.relocations[1];
420   EXPECT_EQ(sectDiff.offset, Hex32(0xE));
421   EXPECT_EQ(sectDiff.scattered, true);
422   EXPECT_EQ(sectDiff.type, GENERIC_RELOC_LOCAL_SECTDIFF);
423   EXPECT_EQ(sectDiff.pcRel, false);
424   EXPECT_EQ(sectDiff.length, 2);
425   EXPECT_EQ(sectDiff.value, 0x30U);
426   const Relocation& pair = text.relocations[2];
427   EXPECT_EQ(pair.offset, Hex32(0x0));
428   EXPECT_EQ(pair.scattered, true);
429   EXPECT_EQ(pair.type, GENERIC_RELOC_PAIR);
430   EXPECT_EQ(pair.pcRel, false);
431   EXPECT_EQ(pair.length, 2);
432   EXPECT_EQ(pair.value, 0x0BU);
433 
434   const Section& cstring = f->sections[1];
435   EXPECT_TRUE(cstring.segmentName.equals("__TEXT"));
436   EXPECT_TRUE(cstring.sectionName.equals("__cstring"));
437   EXPECT_EQ(cstring.type, S_CSTRING_LITERALS);
438   EXPECT_EQ(cstring.attributes, SectionAttr(0));
439   EXPECT_EQ((uint16_t)cstring.alignment, 1U);
440   EXPECT_EQ(cstring.address, Hex64(0x030));
441   EXPECT_EQ(cstring.content.size(), 7UL);
442   EXPECT_EQ((int)(cstring.content[0]), 0x68);
443   EXPECT_EQ((int)(cstring.content[1]), 0x65);
444   EXPECT_EQ((int)(cstring.content[2]), 0x6c);
445   EXPECT_TRUE(cstring.indirectSymbols.empty());
446   EXPECT_TRUE(cstring.relocations.empty());
447 
448   EXPECT_EQ(f->localSymbols.size(), 0UL);
449   EXPECT_EQ(f->globalSymbols.size(), 1UL);
450   const Symbol& mainLabel = f->globalSymbols[0];
451   EXPECT_TRUE(mainLabel.name.equals("_main"));
452   EXPECT_EQ(mainLabel.type, N_SECT);
453   EXPECT_EQ(mainLabel.sect, 1);
454   EXPECT_EQ(mainLabel.scope, SymbolScope(N_EXT));
455   EXPECT_EQ(mainLabel.value, Hex64(0x0));
456   EXPECT_EQ(f->undefinedSymbols.size(), 1UL);
457   const Symbol& printfLabel = f->undefinedSymbols[0];
458   EXPECT_TRUE(printfLabel.name.equals("_printf"));
459   EXPECT_EQ(printfLabel.type, N_UNDF);
460   EXPECT_EQ(printfLabel.scope, SymbolScope(N_EXT));
461 }
462 
TEST(BinaryReaderTest,hello_obj_armv7)463 TEST(BinaryReaderTest, hello_obj_armv7) {
464   FILEBYTES = {
465     0xCE, 0xFA, 0xED, 0xFE, 0x0C, 0x00, 0x00, 0x00,
466     0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
467     0x03, 0x00, 0x00, 0x00, 0x28, 0x01, 0x00, 0x00,
468     0x00, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
469     0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
470     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
471     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
472     0x31, 0x00, 0x00, 0x00, 0x44, 0x01, 0x00, 0x00,
473     0x31, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
474     0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
475     0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x74, 0x65,
476     0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
477     0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x54, 0x45,
478     0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
479     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
480     0x2A, 0x00, 0x00, 0x00, 0x44, 0x01, 0x00, 0x00,
481     0x02, 0x00, 0x00, 0x00, 0x78, 0x01, 0x00, 0x00,
482     0x05, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x80,
483     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
484     0x5F, 0x5F, 0x63, 0x73, 0x74, 0x72, 0x69, 0x6E,
485     0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
486     0x5F, 0x5F, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
487     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
488     0x2A, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
489     0x6E, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
490     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
491     0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
492     0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
493     0x18, 0x00, 0x00, 0x00, 0xA0, 0x01, 0x00, 0x00,
494     0x02, 0x00, 0x00, 0x00, 0xB8, 0x01, 0x00, 0x00,
495     0x10, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00,
496     0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
497     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
498     0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
499     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
500     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
501     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
502     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
503     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
504     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
505     0x00, 0x00, 0x00, 0x00, 0x80, 0xB5, 0x6F, 0x46,
506     0x82, 0xB0, 0x40, 0xF2, 0x18, 0x00, 0xC0, 0xF2,
507     0x00, 0x00, 0x78, 0x44, 0x00, 0x21, 0xC0, 0xF2,
508     0x00, 0x01, 0x01, 0x91, 0xFF, 0xF7, 0xF2, 0xFF,
509     0x00, 0x21, 0xC0, 0xF2, 0x00, 0x01, 0x00, 0x90,
510     0x08, 0x46, 0x02, 0xB0, 0x80, 0xBD, 0x68, 0x65,
511     0x6C, 0x6C, 0x6F, 0x0A, 0x00, 0x00, 0x00, 0x00,
512     0x18, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x6D,
513     0x0A, 0x00, 0x00, 0xB9, 0x2A, 0x00, 0x00, 0x00,
514     0x18, 0x00, 0x00, 0xB1, 0x0E, 0x00, 0x00, 0x00,
515     0x06, 0x00, 0x00, 0xA9, 0x2A, 0x00, 0x00, 0x00,
516     0x00, 0x00, 0x00, 0xA1, 0x0E, 0x00, 0x00, 0x00,
517     0x01, 0x00, 0x00, 0x00, 0x0F, 0x01, 0x08, 0x00,
518     0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
519     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
520     0x00, 0x5F, 0x6D, 0x61, 0x69, 0x6E, 0x00, 0x5F,
521     0x70, 0x72, 0x69, 0x6E, 0x74, 0x66, 0x00, 0x00
522   };
523   std::unique_ptr<NormalizedFile> f =
524       fromBinary(fileBytes, sizeof(fileBytes), "armv7");
525 
526   EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_armv7);
527   EXPECT_EQ((int)(f->fileType), MH_OBJECT);
528   EXPECT_EQ((int)(f->flags), (int)MH_SUBSECTIONS_VIA_SYMBOLS);
529   EXPECT_EQ(f->sections.size(), 2UL);
530   const Section& text = f->sections[0];
531   EXPECT_TRUE(text.segmentName.equals("__TEXT"));
532   EXPECT_TRUE(text.sectionName.equals("__text"));
533   EXPECT_EQ(text.type, S_REGULAR);
534   EXPECT_EQ(text.attributes,SectionAttr(S_ATTR_PURE_INSTRUCTIONS
535                                       | S_ATTR_SOME_INSTRUCTIONS));
536   EXPECT_EQ((uint16_t)text.alignment, 4U);
537   EXPECT_EQ(text.address, Hex64(0x0));
538   EXPECT_EQ(text.content.size(), 42UL);
539   EXPECT_EQ((int)(text.content[0]), 0x80);
540   EXPECT_EQ((int)(text.content[1]), 0xB5);
541   EXPECT_TRUE(text.indirectSymbols.empty());
542   EXPECT_EQ(text.relocations.size(), 5UL);
543   const Relocation& call = text.relocations[0];
544   EXPECT_EQ(call.offset, Hex32(0x18));
545   EXPECT_EQ(call.scattered, false);
546   EXPECT_EQ(call.type, ARM_THUMB_RELOC_BR22);
547   EXPECT_EQ(call.length, 2);
548   EXPECT_EQ(call.isExtern, true);
549   EXPECT_EQ(call.symbol, 1U);
550   const Relocation& movt = text.relocations[1];
551   EXPECT_EQ(movt.offset, Hex32(0xA));
552   EXPECT_EQ(movt.scattered, true);
553   EXPECT_EQ(movt.type, ARM_RELOC_HALF_SECTDIFF);
554   EXPECT_EQ(movt.length, 3);
555   EXPECT_EQ(movt.value, Hex32(0x2A));
556   const Relocation& movtPair = text.relocations[2];
557   EXPECT_EQ(movtPair.offset, Hex32(0x18));
558   EXPECT_EQ(movtPair.scattered, true);
559   EXPECT_EQ(movtPair.type, ARM_RELOC_PAIR);
560   EXPECT_EQ(movtPair.length, 3);
561   EXPECT_EQ(movtPair.value, Hex32(0xE));
562   const Relocation& movw = text.relocations[3];
563   EXPECT_EQ(movw.offset, Hex32(0x6));
564   EXPECT_EQ(movw.scattered, true);
565   EXPECT_EQ(movw.type, ARM_RELOC_HALF_SECTDIFF);
566   EXPECT_EQ(movw.length, 2);
567   EXPECT_EQ(movw.value, Hex32(0x2A));
568   const Relocation& movwPair = text.relocations[4];
569   EXPECT_EQ(movwPair.offset, Hex32(0x0));
570   EXPECT_EQ(movwPair.scattered, true);
571   EXPECT_EQ(movwPair.type, ARM_RELOC_PAIR);
572   EXPECT_EQ(movwPair.length, 2);
573   EXPECT_EQ(movwPair.value, Hex32(0xE));
574 
575   const Section& cstring = f->sections[1];
576   EXPECT_TRUE(cstring.segmentName.equals("__TEXT"));
577   EXPECT_TRUE(cstring.sectionName.equals("__cstring"));
578   EXPECT_EQ(cstring.type, S_CSTRING_LITERALS);
579   EXPECT_EQ(cstring.attributes, SectionAttr(0));
580   EXPECT_EQ((uint16_t)cstring.alignment, 1U);
581   EXPECT_EQ(cstring.address, Hex64(0x02A));
582   EXPECT_EQ(cstring.content.size(), 7UL);
583   EXPECT_EQ((int)(cstring.content[0]), 0x68);
584   EXPECT_EQ((int)(cstring.content[1]), 0x65);
585   EXPECT_EQ((int)(cstring.content[2]), 0x6c);
586   EXPECT_TRUE(cstring.indirectSymbols.empty());
587   EXPECT_TRUE(cstring.relocations.empty());
588 
589   EXPECT_EQ(f->localSymbols.size(), 0UL);
590   EXPECT_EQ(f->globalSymbols.size(), 1UL);
591   const Symbol& mainLabel = f->globalSymbols[0];
592   EXPECT_TRUE(mainLabel.name.equals("_main"));
593   EXPECT_EQ(mainLabel.type, N_SECT);
594   EXPECT_EQ(mainLabel.sect, 1);
595   EXPECT_EQ(mainLabel.scope, SymbolScope(N_EXT));
596   EXPECT_EQ(mainLabel.value, Hex64(0x0));
597   EXPECT_EQ(f->undefinedSymbols.size(), 1UL);
598   const Symbol& printfLabel = f->undefinedSymbols[0];
599   EXPECT_TRUE(printfLabel.name.equals("_printf"));
600   EXPECT_EQ(printfLabel.type, N_UNDF);
601   EXPECT_EQ(printfLabel.scope, SymbolScope(N_EXT));
602 }
603 
TEST(BinaryReaderTest,hello_obj_ppc)604 TEST(BinaryReaderTest, hello_obj_ppc) {
605   FILEBYTES = {
606     0xFE, 0xED, 0xFA, 0xCE, 0x00, 0x00, 0x00, 0x12,
607     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
608     0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x01, 0x28,
609     0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x01,
610     0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00,
611     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
612     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
613     0x00, 0x00, 0x00, 0x4B, 0x00, 0x00, 0x01, 0x44,
614     0x00, 0x00, 0x00, 0x4B, 0x00, 0x00, 0x00, 0x07,
615     0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x02,
616     0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x74, 0x65,
617     0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
618     0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x54, 0x45,
619     0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
620     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
621     0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x01, 0x44,
622     0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x90,
623     0x00, 0x00, 0x00, 0x05, 0x80, 0x00, 0x04, 0x00,
624     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
625     0x5F, 0x5F, 0x63, 0x73, 0x74, 0x72, 0x69, 0x6E,
626     0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
627     0x5F, 0x5F, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
628     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
629     0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x07,
630     0x00, 0x00, 0x01, 0x88, 0x00, 0x00, 0x00, 0x02,
631     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
632     0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
633     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
634     0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x01, 0xB8,
635     0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0xD0,
636     0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0B,
637     0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
638     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
639     0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
640     0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
641     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
642     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
643     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
644     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
645     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
646     0x00, 0x00, 0x00, 0x00, 0x7C, 0x08, 0x02, 0xA6,
647     0xBF, 0xC1, 0xFF, 0xF8, 0x90, 0x01, 0x00, 0x08,
648     0x94, 0x21, 0xFF, 0xB0, 0x7C, 0x3E, 0x0B, 0x78,
649     0x42, 0x9F, 0x00, 0x05, 0x7F, 0xE8, 0x02, 0xA6,
650     0x3C, 0x5F, 0x00, 0x00, 0x38, 0x62, 0x00, 0x2C,
651     0x4B, 0xFF, 0xFF, 0xDD, 0x38, 0x00, 0x00, 0x00,
652     0x7C, 0x03, 0x03, 0x78, 0x80, 0x21, 0x00, 0x00,
653     0x80, 0x01, 0x00, 0x08, 0x7C, 0x08, 0x03, 0xA6,
654     0xBB, 0xC1, 0xFF, 0xF8, 0x4E, 0x80, 0x00, 0x20,
655     0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0A, 0x00, 0x00,
656     0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x01, 0xD3,
657     0xAB, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x44,
658     0xA1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18,
659     0xAC, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x44,
660     0xA1, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x18,
661     0x00, 0x00, 0x00, 0x01, 0x0F, 0x01, 0x00, 0x00,
662     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
663     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
664     0x00, 0x5F, 0x6D, 0x61, 0x69, 0x6E, 0x00, 0x5F,
665     0x70, 0x72, 0x69, 0x6E, 0x74, 0x66, 0x00, 0x00
666   };
667   std::unique_ptr<NormalizedFile> f =
668       fromBinary(fileBytes, sizeof(fileBytes), "ppc");
669 
670   EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_ppc);
671   EXPECT_EQ((int)(f->fileType), MH_OBJECT);
672   EXPECT_EQ((int)(f->flags), (int)MH_SUBSECTIONS_VIA_SYMBOLS);
673   EXPECT_EQ(f->sections.size(), 2UL);
674   const Section& text = f->sections[0];
675   EXPECT_TRUE(text.segmentName.equals("__TEXT"));
676   EXPECT_TRUE(text.sectionName.equals("__text"));
677   EXPECT_EQ(text.type, S_REGULAR);
678   EXPECT_EQ(text.attributes,SectionAttr(S_ATTR_PURE_INSTRUCTIONS
679                                       | S_ATTR_SOME_INSTRUCTIONS));
680   EXPECT_EQ((uint16_t)text.alignment, 4U);
681   EXPECT_EQ(text.address, Hex64(0x0));
682   EXPECT_EQ(text.content.size(), 68UL);
683   EXPECT_EQ((int)(text.content[0]), 0x7C);
684   EXPECT_EQ((int)(text.content[1]), 0x08);
685   EXPECT_TRUE(text.indirectSymbols.empty());
686   EXPECT_EQ(text.relocations.size(), 5UL);
687   const Relocation& bl = text.relocations[0];
688   EXPECT_EQ(bl.offset, Hex32(0x24));
689   EXPECT_EQ(bl.type, PPC_RELOC_BR24);
690   EXPECT_EQ(bl.length, 2);
691   EXPECT_EQ(bl.isExtern, true);
692   EXPECT_EQ(bl.symbol, 1U);
693   const Relocation& lo = text.relocations[1];
694   EXPECT_EQ(lo.offset, Hex32(0x20));
695   EXPECT_EQ(lo.scattered, true);
696   EXPECT_EQ(lo.type, PPC_RELOC_LO16_SECTDIFF);
697   EXPECT_EQ(lo.length, 2);
698   EXPECT_EQ(lo.value, Hex32(0x44));
699   const Relocation& loPair = text.relocations[2];
700   EXPECT_EQ(loPair.offset, Hex32(0x0));
701   EXPECT_EQ(loPair.scattered, true);
702   EXPECT_EQ(loPair.type, PPC_RELOC_PAIR);
703   EXPECT_EQ(loPair.length, 2);
704   EXPECT_EQ(loPair.value, Hex32(0x18));
705   const Relocation& ha = text.relocations[3];
706   EXPECT_EQ(ha.offset, Hex32(0x1C));
707   EXPECT_EQ(ha.scattered, true);
708   EXPECT_EQ(ha.type, PPC_RELOC_HA16_SECTDIFF);
709   EXPECT_EQ(ha.length, 2);
710   EXPECT_EQ(ha.value, Hex32(0x44));
711   const Relocation& haPair = text.relocations[4];
712   EXPECT_EQ(haPair.offset, Hex32(0x2c));
713   EXPECT_EQ(haPair.scattered, true);
714   EXPECT_EQ(haPair.type, PPC_RELOC_PAIR);
715   EXPECT_EQ(haPair.length, 2);
716   EXPECT_EQ(haPair.value, Hex32(0x18));
717 
718   const Section& cstring = f->sections[1];
719   EXPECT_TRUE(cstring.segmentName.equals("__TEXT"));
720   EXPECT_TRUE(cstring.sectionName.equals("__cstring"));
721   EXPECT_EQ(cstring.type, S_CSTRING_LITERALS);
722   EXPECT_EQ(cstring.attributes, SectionAttr(0));
723   EXPECT_EQ((uint16_t)cstring.alignment, 4U);
724   EXPECT_EQ(cstring.address, Hex64(0x044));
725   EXPECT_EQ(cstring.content.size(), 7UL);
726   EXPECT_EQ((int)(cstring.content[0]), 0x68);
727   EXPECT_EQ((int)(cstring.content[1]), 0x65);
728   EXPECT_EQ((int)(cstring.content[2]), 0x6c);
729   EXPECT_TRUE(cstring.indirectSymbols.empty());
730   EXPECT_TRUE(cstring.relocations.empty());
731 
732   EXPECT_EQ(f->localSymbols.size(), 0UL);
733   EXPECT_EQ(f->globalSymbols.size(), 1UL);
734   const Symbol& mainLabel = f->globalSymbols[0];
735   EXPECT_TRUE(mainLabel.name.equals("_main"));
736   EXPECT_EQ(mainLabel.type, N_SECT);
737   EXPECT_EQ(mainLabel.sect, 1);
738   EXPECT_EQ(mainLabel.scope, SymbolScope(N_EXT));
739   EXPECT_EQ(mainLabel.value, Hex64(0x0));
740   EXPECT_EQ(f->undefinedSymbols.size(), 1UL);
741   const Symbol& printfLabel = f->undefinedSymbols[0];
742   EXPECT_TRUE(printfLabel.name.equals("_printf"));
743   EXPECT_EQ(printfLabel.type, N_UNDF);
744   EXPECT_EQ(printfLabel.scope, SymbolScope(N_EXT));
745 
746   SmallString<128> tmpFl;
747   std::error_code ec =
748       llvm::sys::fs::createTemporaryFile(Twine("xx"), "o", tmpFl);
749   EXPECT_FALSE(ec);
750   llvm::Error ec2 = writeBinary(*f, tmpFl);
751   EXPECT_FALSE(ec2);
752   llvm::sys::fs::remove(tmpFl);
753 }
754