blob: 05c0b9f1dc955c5e2006418f56077dcb6c69494b [file] [log] [blame]
Nick Kledzike34182f2013-11-06 21:36:55 +00001//===- lld/unittest/MachOTests/MachONormalizedFileBinaryReaderTests.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
10#include "gtest/gtest.h"
11
12#include <llvm/Support/MachO.h>
13#include "../../lib/ReaderWriter/MachO/MachONormalizedFile.h"
14
15#include <assert.h>
16#include <vector>
17
18using llvm::StringRef;
19using llvm::MemoryBuffer;
20using llvm::ErrorOr;
21
22using namespace lld::mach_o::normalized;
23using namespace llvm::MachO;
24
Rui Ueyama7c13c442014-01-13 23:21:33 +000025static std::unique_ptr<NormalizedFile>
Joey Gouly010b3762014-01-14 22:32:38 +000026fromBinary(const uint8_t bytes[], unsigned length, StringRef archStr) {
Nick Kledzike34182f2013-11-06 21:36:55 +000027 StringRef sr((const char*)bytes, length);
28 std::unique_ptr<MemoryBuffer> mb(MemoryBuffer::getMemBuffer(sr, "", false));
Joey Gouly010b3762014-01-14 22:32:38 +000029 ErrorOr<std::unique_ptr<NormalizedFile>> r =
30 lld::mach_o::normalized::readBinary(
31 mb, lld::MachOLinkingContext::archFromName(archStr));
Nick Kledzike34182f2013-11-06 21:36:55 +000032 EXPECT_FALSE(!r);
33 return std::move(*r);
34}
35
36
37TEST(BinaryReaderTest, empty_obj_x86_64) {
38 const uint8_t fileBytes[] = {
Rui Ueyama7c13c442014-01-13 23:21:33 +000039 0xcf, 0xfa, 0xed, 0xfe, 0x07, 0x00, 0x00, 0x01,
Nick Kledzike34182f2013-11-06 21:36:55 +000040 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
Rui Ueyama7c13c442014-01-13 23:21:33 +000041 0x01, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
Nick Kledzike34182f2013-11-06 21:36:55 +000042 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rui Ueyama7c13c442014-01-13 23:21:33 +000043 0x19, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
Nick Kledzike34182f2013-11-06 21:36:55 +000044 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Nick Kledzike34182f2013-11-06 21:36:55 +000045 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Nick Kledzike34182f2013-11-06 21:36:55 +000046 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rui Ueyama7c13c442014-01-13 23:21:33 +000047 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Nick Kledzike34182f2013-11-06 21:36:55 +000048 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rui Ueyama7c13c442014-01-13 23:21:33 +000049 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
51 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x5f, 0x5f, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x5f, 0x5f, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Nick Kledzike34182f2013-11-06 21:36:55 +000060 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
Rui Ueyama7c13c442014-01-13 23:21:33 +000061 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Joey Gouly010b3762014-01-14 22:32:38 +000062 std::unique_ptr<NormalizedFile> f =
63 fromBinary(fileBytes, sizeof(fileBytes), "x86_64");
Nick Kledzike34182f2013-11-06 21:36:55 +000064 EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_x86_64);
65 EXPECT_EQ((int)(f->fileType), MH_OBJECT);
66 EXPECT_EQ((int)(f->flags), MH_SUBSECTIONS_VIA_SYMBOLS);
67 EXPECT_TRUE(f->localSymbols.empty());
68 EXPECT_TRUE(f->globalSymbols.empty());
69 EXPECT_TRUE(f->undefinedSymbols.empty());
70}
71
72
73TEST(BinaryReaderTest, empty_obj_x86) {
74 const uint8_t fileBytes[] = {
75 0xce, 0xfa, 0xed, 0xfe, 0x07, 0x00, 0x00, 0x00,
76 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
77 0x01, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00,
78 0x00, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
79 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
84 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
85 0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x74, 0x65,
86 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x54, 0x45,
88 0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Joey Gouly010b3762014-01-14 22:32:38 +000094 std::unique_ptr<NormalizedFile> f =
95 fromBinary(fileBytes, sizeof(fileBytes), "i386");
Nick Kledzike34182f2013-11-06 21:36:55 +000096 EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_x86);
97 EXPECT_EQ((int)(f->fileType), MH_OBJECT);
98 EXPECT_EQ((int)(f->flags), MH_SUBSECTIONS_VIA_SYMBOLS);
99 EXPECT_TRUE(f->localSymbols.empty());
100 EXPECT_TRUE(f->globalSymbols.empty());
101 EXPECT_TRUE(f->undefinedSymbols.empty());
102}
103
104
105TEST(BinaryReaderTest, empty_obj_ppc) {
106 const uint8_t fileBytes[] = {
107 0xfe, 0xed, 0xfa, 0xce, 0x00, 0x00, 0x00, 0x12,
108 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
109 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7c,
110 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x01,
111 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00,
112 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98,
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
116 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01,
117 0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x74, 0x65,
118 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
119 0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x54, 0x45,
120 0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x98,
123 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Joey Gouly010b3762014-01-14 22:32:38 +0000126 std::unique_ptr<NormalizedFile> f =
127 fromBinary(fileBytes, sizeof(fileBytes), "ppc");
Nick Kledzike34182f2013-11-06 21:36:55 +0000128 EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_ppc);
129 EXPECT_EQ((int)(f->fileType), MH_OBJECT);
130 EXPECT_EQ((int)(f->flags), MH_SUBSECTIONS_VIA_SYMBOLS);
131 EXPECT_TRUE(f->localSymbols.empty());
132 EXPECT_TRUE(f->globalSymbols.empty());
133 EXPECT_TRUE(f->undefinedSymbols.empty());
134}
135
136
137TEST(BinaryReaderTest, empty_obj_armv7) {
138 const uint8_t fileBytes[] = {
139 0xce, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x00,
140 0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
141 0x01, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00,
142 0x00, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
143 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
146 0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
147 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
148 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
149 0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x74, 0x65,
150 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
151 0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x54, 0x45,
152 0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154 0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Joey Gouly010b3762014-01-14 22:32:38 +0000158 std::unique_ptr<NormalizedFile> f =
159 fromBinary(fileBytes, sizeof(fileBytes), "armv7");
Nick Kledzike34182f2013-11-06 21:36:55 +0000160 EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_armv7);
161 EXPECT_EQ((int)(f->fileType), MH_OBJECT);
162 EXPECT_EQ((int)(f->flags), MH_SUBSECTIONS_VIA_SYMBOLS);
163 EXPECT_TRUE(f->localSymbols.empty());
164 EXPECT_TRUE(f->globalSymbols.empty());
165 EXPECT_TRUE(f->undefinedSymbols.empty());
166}
167
Joey Gouly010b3762014-01-14 22:32:38 +0000168TEST(BinaryReaderTest, empty_obj_x86_64_arm7) {
169 const uint8_t fileBytes[] = {
170#include "empty_obj_x86_armv7.txt"
171 };
172 std::unique_ptr<NormalizedFile> f =
173 fromBinary(fileBytes, sizeof(fileBytes), "x86_64");
174 EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_x86_64);
175 EXPECT_EQ((int)(f->fileType), MH_OBJECT);
176 EXPECT_EQ((int)(f->flags), MH_SUBSECTIONS_VIA_SYMBOLS);
177 EXPECT_TRUE(f->localSymbols.empty());
178 EXPECT_TRUE(f->globalSymbols.empty());
179 EXPECT_TRUE(f->undefinedSymbols.empty());
Nick Kledzike34182f2013-11-06 21:36:55 +0000180
Joey Gouly010b3762014-01-14 22:32:38 +0000181 std::unique_ptr<NormalizedFile> f2 =
182 fromBinary(fileBytes, sizeof(fileBytes), "armv7");
183 EXPECT_EQ(f2->arch, lld::MachOLinkingContext::arch_armv7);
184 EXPECT_EQ((int)(f2->fileType), MH_OBJECT);
185 EXPECT_EQ((int)(f2->flags), MH_SUBSECTIONS_VIA_SYMBOLS);
186 EXPECT_TRUE(f2->localSymbols.empty());
187 EXPECT_TRUE(f2->globalSymbols.empty());
188 EXPECT_TRUE(f2->undefinedSymbols.empty());
189}
Nick Kledzike34182f2013-11-06 21:36:55 +0000190
191TEST(BinaryReaderTest, hello_obj_x86_64) {
192 const uint8_t fileBytes[] = {
193 0xCF, 0xFA, 0xED, 0xFE, 0x07, 0x00, 0x00, 0x01,
194 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
195 0x03, 0x00, 0x00, 0x00, 0x50, 0x01, 0x00, 0x00,
196 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
197 0x19, 0x00, 0x00, 0x00, 0xE8, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
199 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
201 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202 0x70, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
204 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
205 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206 0x5F, 0x5F, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00,
207 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208 0x5F, 0x5F, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
209 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
210 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211 0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
212 0x70, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
213 0xA4, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
214 0x00, 0x04, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
215 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
216 0x5F, 0x5F, 0x63, 0x73, 0x74, 0x72, 0x69, 0x6E,
217 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
218 0x5F, 0x5F, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
219 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
220 0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
221 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
222 0x9D, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
223 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
224 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
225 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
226 0x02, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
227 0xB4, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
228 0xE4, 0x01, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
229 0x0B, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
230 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
231 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
232 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
233 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239 0x55, 0x48, 0x89, 0xE5, 0x48, 0x83, 0xEC, 0x10,
240 0x48, 0x8D, 0x3D, 0x00, 0x00, 0x00, 0x00, 0xC7,
241 0x45, 0xFC, 0x00, 0x00, 0x00, 0x00, 0xB0, 0x00,
242 0xE8, 0x00, 0x00, 0x00, 0x00, 0xB9, 0x00, 0x00,
243 0x00, 0x00, 0x89, 0x45, 0xF8, 0x89, 0xC8, 0x48,
244 0x83, 0xC4, 0x10, 0x5D, 0xC3, 0x68, 0x65, 0x6C,
245 0x6C, 0x6F, 0x0A, 0x00, 0x19, 0x00, 0x00, 0x00,
246 0x02, 0x00, 0x00, 0x2D, 0x0B, 0x00, 0x00, 0x00,
247 0x00, 0x00, 0x00, 0x1D, 0x0F, 0x00, 0x00, 0x00,
248 0x0E, 0x02, 0x00, 0x00, 0x2D, 0x00, 0x00, 0x00,
249 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
250 0x0F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
251 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
252 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253 0x00, 0x00, 0x00, 0x00, 0x00, 0x5F, 0x6D, 0x61,
254 0x69, 0x6E, 0x00, 0x5F, 0x70, 0x72, 0x69, 0x6E,
255 0x74, 0x66, 0x00, 0x4C, 0x5F, 0x2E, 0x73, 0x74,
256 0x72, 0x00, 0x00, 0x00 };
Joey Gouly010b3762014-01-14 22:32:38 +0000257 std::unique_ptr<NormalizedFile> f =
258 fromBinary(fileBytes, sizeof(fileBytes), "x86_64");
Rui Ueyama7c13c442014-01-13 23:21:33 +0000259
Nick Kledzike34182f2013-11-06 21:36:55 +0000260 EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_x86_64);
261 EXPECT_EQ((int)(f->fileType), MH_OBJECT);
262 EXPECT_EQ((int)(f->flags), MH_SUBSECTIONS_VIA_SYMBOLS);
263 EXPECT_EQ(f->sections.size(), 2UL);
264 const Section& text = f->sections[0];
265 EXPECT_TRUE(text.segmentName.equals("__TEXT"));
266 EXPECT_TRUE(text.sectionName.equals("__text"));
267 EXPECT_EQ(text.type, S_REGULAR);
Rui Ueyama7c13c442014-01-13 23:21:33 +0000268 EXPECT_EQ(text.attributes,SectionAttr(S_ATTR_PURE_INSTRUCTIONS
Nick Kledzike34182f2013-11-06 21:36:55 +0000269 | S_ATTR_SOME_INSTRUCTIONS));
270 EXPECT_EQ(text.alignment, 4U);
Nick Kledzikf3e89cb2013-11-06 22:20:56 +0000271 EXPECT_EQ(text.address, Hex64(0x0));
Nick Kledzike34182f2013-11-06 21:36:55 +0000272 EXPECT_EQ(text.content.size(), 45UL);
273 EXPECT_EQ((int)(text.content[0]), 0x55);
274 EXPECT_EQ((int)(text.content[1]), 0x48);
275 EXPECT_TRUE(text.indirectSymbols.empty());
276 EXPECT_EQ(text.relocations.size(), 2UL);
277 const Relocation& call = text.relocations[0];
278 EXPECT_EQ(call.offset, Hex32(0x19));
279 EXPECT_EQ(call.type, X86_64_RELOC_BRANCH);
280 EXPECT_EQ(call.length, 2);
281 EXPECT_EQ(call.isExtern, true);
282 EXPECT_EQ(call.symbol, 2U);
283 const Relocation& str = text.relocations[1];
284 EXPECT_EQ(str.offset, Hex32(0xB));
285 EXPECT_EQ(str.type, X86_64_RELOC_SIGNED);
286 EXPECT_EQ(str.length, 2);
287 EXPECT_EQ(str.isExtern, true);
288 EXPECT_EQ(str.symbol, 0U);
Rui Ueyama7c13c442014-01-13 23:21:33 +0000289
Nick Kledzike34182f2013-11-06 21:36:55 +0000290 const Section& cstring = f->sections[1];
291 EXPECT_TRUE(cstring.segmentName.equals("__TEXT"));
292 EXPECT_TRUE(cstring.sectionName.equals("__cstring"));
293 EXPECT_EQ(cstring.type, S_CSTRING_LITERALS);
294 EXPECT_EQ(cstring.attributes, SectionAttr(0));
295 EXPECT_EQ(cstring.alignment, 0U);
Nick Kledzikf3e89cb2013-11-06 22:20:56 +0000296 EXPECT_EQ(cstring.address, Hex64(0x02D));
Nick Kledzike34182f2013-11-06 21:36:55 +0000297 EXPECT_EQ(cstring.content.size(), 7UL);
298 EXPECT_EQ((int)(cstring.content[0]), 0x68);
299 EXPECT_EQ((int)(cstring.content[1]), 0x65);
300 EXPECT_EQ((int)(cstring.content[2]), 0x6c);
301 EXPECT_TRUE(cstring.indirectSymbols.empty());
302 EXPECT_TRUE(cstring.relocations.empty());
303
304 EXPECT_EQ(f->localSymbols.size(), 1UL);
305 const Symbol& strLabel = f->localSymbols[0];
306 EXPECT_EQ(strLabel.type, N_SECT);
307 EXPECT_EQ(strLabel.sect, 2);
308 EXPECT_EQ(strLabel.value, Hex64(0x2D));
309 EXPECT_EQ(f->globalSymbols.size(), 1UL);
310 const Symbol& mainLabel = f->globalSymbols[0];
311 EXPECT_TRUE(mainLabel.name.equals("_main"));
312 EXPECT_EQ(mainLabel.type, N_SECT);
313 EXPECT_EQ(mainLabel.sect, 1);
314 EXPECT_EQ(mainLabel.scope, SymbolScope(N_EXT));
315 EXPECT_EQ(mainLabel.value, Hex64(0x0));
316 EXPECT_EQ(f->undefinedSymbols.size(), 1UL);
317 const Symbol& printfLabel = f->undefinedSymbols[0];
318 EXPECT_TRUE(printfLabel.name.equals("_printf"));
319 EXPECT_EQ(printfLabel.type, N_UNDF);
320 EXPECT_EQ(printfLabel.scope, SymbolScope(N_EXT));
321}
322
323
324TEST(BinaryReaderTest, hello_obj_x86) {
325 const uint8_t fileBytes[] = {
326 0xCE, 0xFA, 0xED, 0xFE, 0x07, 0x00, 0x00, 0x00,
327 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
328 0x03, 0x00, 0x00, 0x00, 0x28, 0x01, 0x00, 0x00,
329 0x00, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
330 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
331 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
332 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
333 0x37, 0x00, 0x00, 0x00, 0x44, 0x01, 0x00, 0x00,
334 0x37, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
335 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
336 0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x74, 0x65,
337 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
338 0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x54, 0x45,
339 0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
340 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
341 0x30, 0x00, 0x00, 0x00, 0x44, 0x01, 0x00, 0x00,
342 0x04, 0x00, 0x00, 0x00, 0x7C, 0x01, 0x00, 0x00,
343 0x03, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x80,
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 0x5F, 0x5F, 0x63, 0x73, 0x74, 0x72, 0x69, 0x6E,
346 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
347 0x5F, 0x5F, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
348 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
349 0x30, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
350 0x74, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
351 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
352 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
353 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
354 0x18, 0x00, 0x00, 0x00, 0x94, 0x01, 0x00, 0x00,
355 0x02, 0x00, 0x00, 0x00, 0xAC, 0x01, 0x00, 0x00,
356 0x10, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00,
357 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
358 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
359 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
360 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
361 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
362 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
363 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
364 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
365 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
366 0x00, 0x00, 0x00, 0x00, 0x55, 0x89, 0xE5, 0x83,
367 0xEC, 0x18, 0xE8, 0x00, 0x00, 0x00, 0x00, 0x58,
368 0x8D, 0x80, 0x25, 0x00, 0x00, 0x00, 0xC7, 0x45,
369 0xFC, 0x00, 0x00, 0x00, 0x00, 0x89, 0x04, 0x24,
370 0xE8, 0xDF, 0xFF, 0xFF, 0xFF, 0xB9, 0x00, 0x00,
371 0x00, 0x00, 0x89, 0x45, 0xF8, 0x89, 0xC8, 0x83,
372 0xC4, 0x18, 0x5D, 0xC3, 0x68, 0x65, 0x6C, 0x6C,
373 0x6F, 0x0A, 0x00, 0x00, 0x1D, 0x00, 0x00, 0x00,
374 0x01, 0x00, 0x00, 0x0D, 0x0E, 0x00, 0x00, 0xA4,
375 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA1,
376 0x0B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
377 0x0F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
378 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
379 0x00, 0x00, 0x00, 0x00, 0x00, 0x5F, 0x6D, 0x61,
380 0x69, 0x6E, 0x00, 0x5F, 0x70, 0x72, 0x69, 0x6E,
381 0x74, 0x66, 0x00, 0x00
382 };
Joey Gouly010b3762014-01-14 22:32:38 +0000383 std::unique_ptr<NormalizedFile> f =
384 fromBinary(fileBytes, sizeof(fileBytes), "i386");
Rui Ueyama7c13c442014-01-13 23:21:33 +0000385
Nick Kledzike34182f2013-11-06 21:36:55 +0000386 EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_x86);
387 EXPECT_EQ((int)(f->fileType), MH_OBJECT);
388 EXPECT_EQ((int)(f->flags), MH_SUBSECTIONS_VIA_SYMBOLS);
389 EXPECT_EQ(f->sections.size(), 2UL);
390 const Section& text = f->sections[0];
391 EXPECT_TRUE(text.segmentName.equals("__TEXT"));
392 EXPECT_TRUE(text.sectionName.equals("__text"));
393 EXPECT_EQ(text.type, S_REGULAR);
Rui Ueyama7c13c442014-01-13 23:21:33 +0000394 EXPECT_EQ(text.attributes,SectionAttr(S_ATTR_PURE_INSTRUCTIONS
Nick Kledzike34182f2013-11-06 21:36:55 +0000395 | S_ATTR_SOME_INSTRUCTIONS));
396 EXPECT_EQ(text.alignment, 4U);
Nick Kledzikf3e89cb2013-11-06 22:20:56 +0000397 EXPECT_EQ(text.address, Hex64(0x0));
Nick Kledzike34182f2013-11-06 21:36:55 +0000398 EXPECT_EQ(text.content.size(), 48UL);
399 EXPECT_EQ((int)(text.content[0]), 0x55);
400 EXPECT_EQ((int)(text.content[1]), 0x89);
401 EXPECT_TRUE(text.indirectSymbols.empty());
402 EXPECT_EQ(text.relocations.size(), 3UL);
403 const Relocation& call = text.relocations[0];
404 EXPECT_EQ(call.offset, Hex32(0x1D));
405 EXPECT_EQ(call.scattered, false);
406 EXPECT_EQ(call.type, GENERIC_RELOC_VANILLA);
407 EXPECT_EQ(call.pcRel, true);
408 EXPECT_EQ(call.length, 2);
409 EXPECT_EQ(call.isExtern, true);
410 EXPECT_EQ(call.symbol, 1U);
411 const Relocation& sectDiff = text.relocations[1];
412 EXPECT_EQ(sectDiff.offset, Hex32(0xE));
413 EXPECT_EQ(sectDiff.scattered, true);
414 EXPECT_EQ(sectDiff.type, GENERIC_RELOC_LOCAL_SECTDIFF);
415 EXPECT_EQ(sectDiff.pcRel, false);
416 EXPECT_EQ(sectDiff.length, 2);
417 EXPECT_EQ(sectDiff.value, 0x30U);
418 const Relocation& pair = text.relocations[2];
419 EXPECT_EQ(pair.offset, Hex32(0x0));
420 EXPECT_EQ(pair.scattered, true);
421 EXPECT_EQ(pair.type, GENERIC_RELOC_PAIR);
422 EXPECT_EQ(pair.pcRel, false);
423 EXPECT_EQ(pair.length, 2);
424 EXPECT_EQ(pair.value, 0x0BU);
Rui Ueyama7c13c442014-01-13 23:21:33 +0000425
Nick Kledzike34182f2013-11-06 21:36:55 +0000426 const Section& cstring = f->sections[1];
427 EXPECT_TRUE(cstring.segmentName.equals("__TEXT"));
428 EXPECT_TRUE(cstring.sectionName.equals("__cstring"));
429 EXPECT_EQ(cstring.type, S_CSTRING_LITERALS);
430 EXPECT_EQ(cstring.attributes, SectionAttr(0));
431 EXPECT_EQ(cstring.alignment, 0U);
Nick Kledzikf3e89cb2013-11-06 22:20:56 +0000432 EXPECT_EQ(cstring.address, Hex64(0x030));
Nick Kledzike34182f2013-11-06 21:36:55 +0000433 EXPECT_EQ(cstring.content.size(), 7UL);
434 EXPECT_EQ((int)(cstring.content[0]), 0x68);
435 EXPECT_EQ((int)(cstring.content[1]), 0x65);
436 EXPECT_EQ((int)(cstring.content[2]), 0x6c);
437 EXPECT_TRUE(cstring.indirectSymbols.empty());
438 EXPECT_TRUE(cstring.relocations.empty());
439
440 EXPECT_EQ(f->localSymbols.size(), 0UL);
441 EXPECT_EQ(f->globalSymbols.size(), 1UL);
442 const Symbol& mainLabel = f->globalSymbols[0];
443 EXPECT_TRUE(mainLabel.name.equals("_main"));
444 EXPECT_EQ(mainLabel.type, N_SECT);
445 EXPECT_EQ(mainLabel.sect, 1);
446 EXPECT_EQ(mainLabel.scope, SymbolScope(N_EXT));
447 EXPECT_EQ(mainLabel.value, Hex64(0x0));
448 EXPECT_EQ(f->undefinedSymbols.size(), 1UL);
449 const Symbol& printfLabel = f->undefinedSymbols[0];
450 EXPECT_TRUE(printfLabel.name.equals("_printf"));
451 EXPECT_EQ(printfLabel.type, N_UNDF);
452 EXPECT_EQ(printfLabel.scope, SymbolScope(N_EXT));
453}
454
455
456TEST(BinaryReaderTest, hello_obj_armv7) {
457 const uint8_t fileBytes[] = {
458 0xCE, 0xFA, 0xED, 0xFE, 0x0C, 0x00, 0x00, 0x00,
459 0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
460 0x03, 0x00, 0x00, 0x00, 0x28, 0x01, 0x00, 0x00,
461 0x00, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
462 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
463 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
464 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
465 0x31, 0x00, 0x00, 0x00, 0x44, 0x01, 0x00, 0x00,
466 0x31, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
467 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
468 0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x74, 0x65,
469 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
470 0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x54, 0x45,
471 0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
472 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
473 0x2A, 0x00, 0x00, 0x00, 0x44, 0x01, 0x00, 0x00,
474 0x02, 0x00, 0x00, 0x00, 0x78, 0x01, 0x00, 0x00,
475 0x05, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x80,
476 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
477 0x5F, 0x5F, 0x63, 0x73, 0x74, 0x72, 0x69, 0x6E,
478 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
479 0x5F, 0x5F, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
480 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
481 0x2A, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
482 0x6E, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
483 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
484 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
485 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
486 0x18, 0x00, 0x00, 0x00, 0xA0, 0x01, 0x00, 0x00,
487 0x02, 0x00, 0x00, 0x00, 0xB8, 0x01, 0x00, 0x00,
488 0x10, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00,
489 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
490 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
491 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
492 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
493 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
494 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
495 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
496 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
497 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
498 0x00, 0x00, 0x00, 0x00, 0x80, 0xB5, 0x6F, 0x46,
499 0x82, 0xB0, 0x40, 0xF2, 0x18, 0x00, 0xC0, 0xF2,
500 0x00, 0x00, 0x78, 0x44, 0x00, 0x21, 0xC0, 0xF2,
501 0x00, 0x01, 0x01, 0x91, 0xFF, 0xF7, 0xF2, 0xFF,
502 0x00, 0x21, 0xC0, 0xF2, 0x00, 0x01, 0x00, 0x90,
503 0x08, 0x46, 0x02, 0xB0, 0x80, 0xBD, 0x68, 0x65,
504 0x6C, 0x6C, 0x6F, 0x0A, 0x00, 0x00, 0x00, 0x00,
505 0x18, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x6D,
506 0x0A, 0x00, 0x00, 0xB9, 0x2A, 0x00, 0x00, 0x00,
507 0x18, 0x00, 0x00, 0xB1, 0x0E, 0x00, 0x00, 0x00,
508 0x06, 0x00, 0x00, 0xA9, 0x2A, 0x00, 0x00, 0x00,
509 0x00, 0x00, 0x00, 0xA1, 0x0E, 0x00, 0x00, 0x00,
510 0x01, 0x00, 0x00, 0x00, 0x0F, 0x01, 0x08, 0x00,
511 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
512 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
513 0x00, 0x5F, 0x6D, 0x61, 0x69, 0x6E, 0x00, 0x5F,
514 0x70, 0x72, 0x69, 0x6E, 0x74, 0x66, 0x00, 0x00
515 };
Joey Gouly010b3762014-01-14 22:32:38 +0000516 std::unique_ptr<NormalizedFile> f =
517 fromBinary(fileBytes, sizeof(fileBytes), "armv7");
Rui Ueyama7c13c442014-01-13 23:21:33 +0000518
Nick Kledzike34182f2013-11-06 21:36:55 +0000519 EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_armv7);
520 EXPECT_EQ((int)(f->fileType), MH_OBJECT);
521 EXPECT_EQ((int)(f->flags), MH_SUBSECTIONS_VIA_SYMBOLS);
522 EXPECT_EQ(f->sections.size(), 2UL);
523 const Section& text = f->sections[0];
524 EXPECT_TRUE(text.segmentName.equals("__TEXT"));
525 EXPECT_TRUE(text.sectionName.equals("__text"));
526 EXPECT_EQ(text.type, S_REGULAR);
Rui Ueyama7c13c442014-01-13 23:21:33 +0000527 EXPECT_EQ(text.attributes,SectionAttr(S_ATTR_PURE_INSTRUCTIONS
Nick Kledzike34182f2013-11-06 21:36:55 +0000528 | S_ATTR_SOME_INSTRUCTIONS));
529 EXPECT_EQ(text.alignment, 2U);
Nick Kledzikf3e89cb2013-11-06 22:20:56 +0000530 EXPECT_EQ(text.address, Hex64(0x0));
Nick Kledzike34182f2013-11-06 21:36:55 +0000531 EXPECT_EQ(text.content.size(), 42UL);
532 EXPECT_EQ((int)(text.content[0]), 0x80);
533 EXPECT_EQ((int)(text.content[1]), 0xB5);
534 EXPECT_TRUE(text.indirectSymbols.empty());
535 EXPECT_EQ(text.relocations.size(), 5UL);
536 const Relocation& call = text.relocations[0];
537 EXPECT_EQ(call.offset, Hex32(0x18));
538 EXPECT_EQ(call.scattered, false);
539 EXPECT_EQ(call.type, ARM_THUMB_RELOC_BR22);
540 EXPECT_EQ(call.length, 2);
541 EXPECT_EQ(call.isExtern, true);
542 EXPECT_EQ(call.symbol, 1U);
543 const Relocation& movt = text.relocations[1];
544 EXPECT_EQ(movt.offset, Hex32(0xA));
545 EXPECT_EQ(movt.scattered, true);
546 EXPECT_EQ(movt.type, ARM_RELOC_HALF_SECTDIFF);
547 EXPECT_EQ(movt.length, 3);
548 EXPECT_EQ(movt.value, Hex32(0x2A));
549 const Relocation& movtPair = text.relocations[2];
550 EXPECT_EQ(movtPair.offset, Hex32(0x18));
551 EXPECT_EQ(movtPair.scattered, true);
552 EXPECT_EQ(movtPair.type, ARM_RELOC_PAIR);
553 EXPECT_EQ(movtPair.length, 3);
554 EXPECT_EQ(movtPair.value, Hex32(0xE));
555 const Relocation& movw = text.relocations[3];
556 EXPECT_EQ(movw.offset, Hex32(0x6));
557 EXPECT_EQ(movw.scattered, true);
558 EXPECT_EQ(movw.type, ARM_RELOC_HALF_SECTDIFF);
559 EXPECT_EQ(movw.length, 2);
560 EXPECT_EQ(movw.value, Hex32(0x2A));
561 const Relocation& movwPair = text.relocations[4];
562 EXPECT_EQ(movwPair.offset, Hex32(0x0));
563 EXPECT_EQ(movwPair.scattered, true);
564 EXPECT_EQ(movwPair.type, ARM_RELOC_PAIR);
565 EXPECT_EQ(movwPair.length, 2);
566 EXPECT_EQ(movwPair.value, Hex32(0xE));
Rui Ueyama7c13c442014-01-13 23:21:33 +0000567
Nick Kledzike34182f2013-11-06 21:36:55 +0000568 const Section& cstring = f->sections[1];
569 EXPECT_TRUE(cstring.segmentName.equals("__TEXT"));
570 EXPECT_TRUE(cstring.sectionName.equals("__cstring"));
571 EXPECT_EQ(cstring.type, S_CSTRING_LITERALS);
572 EXPECT_EQ(cstring.attributes, SectionAttr(0));
573 EXPECT_EQ(cstring.alignment, 0U);
Nick Kledzikf3e89cb2013-11-06 22:20:56 +0000574 EXPECT_EQ(cstring.address, Hex64(0x02A));
Nick Kledzike34182f2013-11-06 21:36:55 +0000575 EXPECT_EQ(cstring.content.size(), 7UL);
576 EXPECT_EQ((int)(cstring.content[0]), 0x68);
577 EXPECT_EQ((int)(cstring.content[1]), 0x65);
578 EXPECT_EQ((int)(cstring.content[2]), 0x6c);
579 EXPECT_TRUE(cstring.indirectSymbols.empty());
580 EXPECT_TRUE(cstring.relocations.empty());
581
582 EXPECT_EQ(f->localSymbols.size(), 0UL);
583 EXPECT_EQ(f->globalSymbols.size(), 1UL);
584 const Symbol& mainLabel = f->globalSymbols[0];
585 EXPECT_TRUE(mainLabel.name.equals("_main"));
586 EXPECT_EQ(mainLabel.type, N_SECT);
587 EXPECT_EQ(mainLabel.sect, 1);
588 EXPECT_EQ(mainLabel.scope, SymbolScope(N_EXT));
589 EXPECT_EQ(mainLabel.value, Hex64(0x0));
590 EXPECT_EQ(f->undefinedSymbols.size(), 1UL);
591 const Symbol& printfLabel = f->undefinedSymbols[0];
592 EXPECT_TRUE(printfLabel.name.equals("_printf"));
593 EXPECT_EQ(printfLabel.type, N_UNDF);
594 EXPECT_EQ(printfLabel.scope, SymbolScope(N_EXT));
595}
596
597
598TEST(BinaryReaderTest, hello_obj_ppc) {
599 const uint8_t fileBytes[] = {
600 0xFE, 0xED, 0xFA, 0xCE, 0x00, 0x00, 0x00, 0x12,
601 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
602 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x01, 0x28,
603 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x01,
604 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00,
605 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
606 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
607 0x00, 0x00, 0x00, 0x4B, 0x00, 0x00, 0x01, 0x44,
608 0x00, 0x00, 0x00, 0x4B, 0x00, 0x00, 0x00, 0x07,
609 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x02,
610 0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x74, 0x65,
611 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
612 0x00, 0x00, 0x00, 0x00, 0x5F, 0x5F, 0x54, 0x45,
613 0x58, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
614 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
615 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x01, 0x44,
616 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x90,
617 0x00, 0x00, 0x00, 0x05, 0x80, 0x00, 0x04, 0x00,
618 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
619 0x5F, 0x5F, 0x63, 0x73, 0x74, 0x72, 0x69, 0x6E,
620 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
621 0x5F, 0x5F, 0x54, 0x45, 0x58, 0x54, 0x00, 0x00,
622 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
623 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x07,
624 0x00, 0x00, 0x01, 0x88, 0x00, 0x00, 0x00, 0x02,
625 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
626 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
627 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
628 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x01, 0xB8,
629 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0xD0,
630 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0B,
631 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
632 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
633 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
634 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
635 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
636 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
637 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
638 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
639 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
640 0x00, 0x00, 0x00, 0x00, 0x7C, 0x08, 0x02, 0xA6,
641 0xBF, 0xC1, 0xFF, 0xF8, 0x90, 0x01, 0x00, 0x08,
642 0x94, 0x21, 0xFF, 0xB0, 0x7C, 0x3E, 0x0B, 0x78,
643 0x42, 0x9F, 0x00, 0x05, 0x7F, 0xE8, 0x02, 0xA6,
644 0x3C, 0x5F, 0x00, 0x00, 0x38, 0x62, 0x00, 0x2C,
645 0x4B, 0xFF, 0xFF, 0xDD, 0x38, 0x00, 0x00, 0x00,
646 0x7C, 0x03, 0x03, 0x78, 0x80, 0x21, 0x00, 0x00,
647 0x80, 0x01, 0x00, 0x08, 0x7C, 0x08, 0x03, 0xA6,
648 0xBB, 0xC1, 0xFF, 0xF8, 0x4E, 0x80, 0x00, 0x20,
649 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0A, 0x00, 0x00,
650 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x01, 0xD3,
651 0xAB, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x44,
652 0xA1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18,
653 0xAC, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x44,
654 0xA1, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x18,
655 0x00, 0x00, 0x00, 0x01, 0x0F, 0x01, 0x00, 0x00,
656 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
657 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
658 0x00, 0x5F, 0x6D, 0x61, 0x69, 0x6E, 0x00, 0x5F,
659 0x70, 0x72, 0x69, 0x6E, 0x74, 0x66, 0x00, 0x00
660 };
Joey Gouly010b3762014-01-14 22:32:38 +0000661 std::unique_ptr<NormalizedFile> f =
662 fromBinary(fileBytes, sizeof(fileBytes), "ppc");
Rui Ueyama7c13c442014-01-13 23:21:33 +0000663
Nick Kledzike34182f2013-11-06 21:36:55 +0000664 EXPECT_EQ(f->arch, lld::MachOLinkingContext::arch_ppc);
665 EXPECT_EQ((int)(f->fileType), MH_OBJECT);
666 EXPECT_EQ((int)(f->flags), MH_SUBSECTIONS_VIA_SYMBOLS);
667 EXPECT_EQ(f->sections.size(), 2UL);
668 const Section& text = f->sections[0];
669 EXPECT_TRUE(text.segmentName.equals("__TEXT"));
670 EXPECT_TRUE(text.sectionName.equals("__text"));
671 EXPECT_EQ(text.type, S_REGULAR);
Rui Ueyama7c13c442014-01-13 23:21:33 +0000672 EXPECT_EQ(text.attributes,SectionAttr(S_ATTR_PURE_INSTRUCTIONS
Nick Kledzike34182f2013-11-06 21:36:55 +0000673 | S_ATTR_SOME_INSTRUCTIONS));
674 EXPECT_EQ(text.alignment, 2U);
Nick Kledzikf3e89cb2013-11-06 22:20:56 +0000675 EXPECT_EQ(text.address, Hex64(0x0));
Nick Kledzike34182f2013-11-06 21:36:55 +0000676 EXPECT_EQ(text.content.size(), 68UL);
677 EXPECT_EQ((int)(text.content[0]), 0x7C);
678 EXPECT_EQ((int)(text.content[1]), 0x08);
679 EXPECT_TRUE(text.indirectSymbols.empty());
680 EXPECT_EQ(text.relocations.size(), 5UL);
681 const Relocation& bl = text.relocations[0];
682 EXPECT_EQ(bl.offset, Hex32(0x24));
683 EXPECT_EQ(bl.type, PPC_RELOC_BR24);
684 EXPECT_EQ(bl.length, 2);
685 EXPECT_EQ(bl.isExtern, true);
686 EXPECT_EQ(bl.symbol, 1U);
687 const Relocation& lo = text.relocations[1];
688 EXPECT_EQ(lo.offset, Hex32(0x20));
689 EXPECT_EQ(lo.scattered, true);
690 EXPECT_EQ(lo.type, PPC_RELOC_LO16_SECTDIFF);
691 EXPECT_EQ(lo.length, 2);
692 EXPECT_EQ(lo.value, Hex32(0x44));
693 const Relocation& loPair = text.relocations[2];
694 EXPECT_EQ(loPair.offset, Hex32(0x0));
695 EXPECT_EQ(loPair.scattered, true);
696 EXPECT_EQ(loPair.type, PPC_RELOC_PAIR);
697 EXPECT_EQ(loPair.length, 2);
698 EXPECT_EQ(loPair.value, Hex32(0x18));
699 const Relocation& ha = text.relocations[3];
700 EXPECT_EQ(ha.offset, Hex32(0x1C));
701 EXPECT_EQ(ha.scattered, true);
702 EXPECT_EQ(ha.type, PPC_RELOC_HA16_SECTDIFF);
703 EXPECT_EQ(ha.length, 2);
704 EXPECT_EQ(ha.value, Hex32(0x44));
705 const Relocation& haPair = text.relocations[4];
706 EXPECT_EQ(haPair.offset, Hex32(0x2c));
707 EXPECT_EQ(haPair.scattered, true);
708 EXPECT_EQ(haPair.type, PPC_RELOC_PAIR);
709 EXPECT_EQ(haPair.length, 2);
710 EXPECT_EQ(haPair.value, Hex32(0x18));
Rui Ueyama7c13c442014-01-13 23:21:33 +0000711
Nick Kledzike34182f2013-11-06 21:36:55 +0000712 const Section& cstring = f->sections[1];
713 EXPECT_TRUE(cstring.segmentName.equals("__TEXT"));
714 EXPECT_TRUE(cstring.sectionName.equals("__cstring"));
715 EXPECT_EQ(cstring.type, S_CSTRING_LITERALS);
716 EXPECT_EQ(cstring.attributes, SectionAttr(0));
717 EXPECT_EQ(cstring.alignment, 2U);
Nick Kledzikf3e89cb2013-11-06 22:20:56 +0000718 EXPECT_EQ(cstring.address, Hex64(0x044));
Nick Kledzike34182f2013-11-06 21:36:55 +0000719 EXPECT_EQ(cstring.content.size(), 7UL);
720 EXPECT_EQ((int)(cstring.content[0]), 0x68);
721 EXPECT_EQ((int)(cstring.content[1]), 0x65);
722 EXPECT_EQ((int)(cstring.content[2]), 0x6c);
723 EXPECT_TRUE(cstring.indirectSymbols.empty());
724 EXPECT_TRUE(cstring.relocations.empty());
Rui Ueyama7c13c442014-01-13 23:21:33 +0000725
Nick Kledzike34182f2013-11-06 21:36:55 +0000726 EXPECT_EQ(f->localSymbols.size(), 0UL);
727 EXPECT_EQ(f->globalSymbols.size(), 1UL);
728 const Symbol& mainLabel = f->globalSymbols[0];
729 EXPECT_TRUE(mainLabel.name.equals("_main"));
730 EXPECT_EQ(mainLabel.type, N_SECT);
731 EXPECT_EQ(mainLabel.sect, 1);
732 EXPECT_EQ(mainLabel.scope, SymbolScope(N_EXT));
733 EXPECT_EQ(mainLabel.value, Hex64(0x0));
734 EXPECT_EQ(f->undefinedSymbols.size(), 1UL);
735 const Symbol& printfLabel = f->undefinedSymbols[0];
736 EXPECT_TRUE(printfLabel.name.equals("_printf"));
737 EXPECT_EQ(printfLabel.type, N_UNDF);
738 EXPECT_EQ(printfLabel.scope, SymbolScope(N_EXT));
Rui Ueyama7c13c442014-01-13 23:21:33 +0000739
Nick Kledzike34182f2013-11-06 21:36:55 +0000740 writeBinary(*f, "/tmp/foo.o");
741
742}
743