]> git.saurik.com Git - apple/ld64.git/blob - src/ObjectDump.cpp
ce9801a97b9bfac7cd981de87158d55e5c6521d9
[apple/ld64.git] / src / ObjectDump.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include <fcntl.h>
29 #include <fcntl.h>
30 #include <mach-o/loader.h>
31 #include <mach-o/fat.h>
32 #include <mach-o/stab.h>
33
34 #include "MachOReaderRelocatable.hpp"
35
36 static bool sDumpContent= true;
37 static bool sDumpStabs = false;
38 static bool sSort = true;
39 static cpu_type_t sPreferredArch = CPU_TYPE_POWERPC64;
40 static const char* sMatchName;
41 static int sPrintRestrict;
42 static int sPrintAlign;
43 static int sPrintName;
44
45
46 __attribute__((noreturn))
47 void throwf(const char* format, ...)
48 {
49 va_list list;
50 char* p;
51 va_start(list, format);
52 vasprintf(&p, format, list);
53 va_end(list);
54
55 const char* t = p;
56 throw t;
57 }
58
59 static void dumpStabs(std::vector<ObjectFile::Reader::Stab>* stabs)
60 {
61 // debug info
62 printf("stabs: (%lu)\n", stabs->size());
63 for (std::vector<ObjectFile::Reader::Stab>::iterator it = stabs->begin(); it != stabs->end(); ++it ) {
64 ObjectFile::Reader::Stab& stab = *it;
65 const char* code = "?????";
66 switch (stab.type) {
67 case N_GSYM:
68 code = " GSYM";
69 break;
70 case N_FNAME:
71 code = "FNAME";
72 break;
73 case N_FUN:
74 code = " FUN";
75 break;
76 case N_STSYM:
77 code = "STSYM";
78 break;
79 case N_LCSYM:
80 code = "LCSYM";
81 break;
82 case N_BNSYM:
83 code = "BNSYM";
84 break;
85 case N_OPT:
86 code = " OPT";
87 break;
88 case N_RSYM:
89 code = " RSYM";
90 break;
91 case N_SLINE:
92 code = "SLINE";
93 break;
94 case N_ENSYM:
95 code = "ENSYM";
96 break;
97 case N_SSYM:
98 code = " SSYM";
99 break;
100 case N_SO:
101 code = " SO";
102 break;
103 case N_OSO:
104 code = " OSO";
105 break;
106 case N_LSYM:
107 code = " LSYM";
108 break;
109 case N_BINCL:
110 code = "BINCL";
111 break;
112 case N_SOL:
113 code = " SOL";
114 break;
115 case N_PARAMS:
116 code = "PARMS";
117 break;
118 case N_VERSION:
119 code = " VERS";
120 break;
121 case N_OLEVEL:
122 code = "OLEVL";
123 break;
124 case N_PSYM:
125 code = " PSYM";
126 break;
127 case N_EINCL:
128 code = "EINCL";
129 break;
130 case N_ENTRY:
131 code = "ENTRY";
132 break;
133 case N_LBRAC:
134 code = "LBRAC";
135 break;
136 case N_EXCL:
137 code = " EXCL";
138 break;
139 case N_RBRAC:
140 code = "RBRAC";
141 break;
142 case N_BCOMM:
143 code = "BCOMM";
144 break;
145 case N_ECOMM:
146 code = "ECOMM";
147 break;
148 case N_LENG:
149 code = "LENG";
150 break;
151 }
152 printf(" [atom=%20s] %02X %04X %s %s\n", ((stab.atom != NULL) ? stab.atom->getDisplayName() : ""), stab.other, stab.desc, code, stab.string);
153 }
154 }
155
156
157 static void dumpAtom(ObjectFile::Atom* atom)
158 {
159 if(sMatchName && strcmp(sMatchName, atom->getDisplayName()))
160 return;
161
162 //printf("atom: %p\n", atom);
163
164 // name
165 if(!sPrintRestrict || sPrintName)
166 printf("name: %s\n", atom->getDisplayName());
167
168 // scope
169 if(!sPrintRestrict)
170 switch ( atom->getScope() ) {
171 case ObjectFile::Atom::scopeTranslationUnit:
172 printf("scope: translation unit\n");
173 break;
174 case ObjectFile::Atom::scopeLinkageUnit:
175 printf("scope: linkage unit\n");
176 break;
177 case ObjectFile::Atom::scopeGlobal:
178 printf("scope: global\n");
179 break;
180 default:
181 printf("scope: unknown\n");
182 }
183
184 // kind
185 if(!sPrintRestrict)
186 switch ( atom->getDefinitionKind() ) {
187 case ObjectFile::Atom::kRegularDefinition:
188 printf("kind: regular\n");
189 break;
190 case ObjectFile::Atom::kWeakDefinition:
191 printf("kind: weak\n");
192 break;
193 case ObjectFile::Atom::kTentativeDefinition:
194 printf("kind: tentative\n");
195 break;
196 case ObjectFile::Atom::kExternalDefinition:
197 printf("kind: import\n");
198 break;
199 case ObjectFile::Atom::kExternalWeakDefinition:
200 printf("kind: weak import\n");
201 break;
202 case ObjectFile::Atom::kAbsoluteSymbol:
203 printf("kind: absolute symbol\n");
204 break;
205 default:
206 printf("kind: unknown\n");
207 }
208
209 // segment and section
210 if(!sPrintRestrict)
211 printf("section: %s,%s\n", atom->getSegment().getName(), atom->getSectionName());
212
213 // attributes
214 if(!sPrintRestrict) {
215 printf("attrs: ");
216 if ( atom->dontDeadStrip() )
217 printf("dont-dead-strip ");
218 if ( atom->isZeroFill() )
219 printf("zero-fill ");
220 printf("\n");
221 }
222
223 // size
224 if(!sPrintRestrict)
225 printf("size: 0x%012llX\n", atom->getSize());
226
227 // alignment
228 if(!sPrintRestrict || sPrintAlign)
229 printf("align: %u mod %u\n", atom->getAlignment().modulus, (1 << atom->getAlignment().powerOf2) );
230
231 // content
232 if (!sPrintRestrict && sDumpContent ) {
233 uint64_t size = atom->getSize();
234 if ( size < 4096 ) {
235 uint8_t content[size];
236 atom->copyRawContent(content);
237 printf("content: ");
238 if ( strcmp(atom->getSectionName(), "__cstring") == 0 ) {
239 printf("\"");
240 for (unsigned int i=0; i < size; ++i) {
241 if(content[i]<'!' || content[i]>=127)
242 printf("\\%o", content[i]);
243 else
244 printf("%c", content[i]);
245 }
246 printf("\"");
247 }
248 else {
249 for (unsigned int i=0; i < size; ++i)
250 printf("%02X ", content[i]);
251 }
252 }
253 printf("\n");
254 }
255
256 // references
257 if(!sPrintRestrict) {
258 std::vector<ObjectFile::Reference*>& references = atom->getReferences();
259 const int refCount = references.size();
260 printf("references: (%u)\n", refCount);
261 for (int i=0; i < refCount; ++i) {
262 ObjectFile::Reference* ref = references[i];
263 printf(" %s\n", ref->getDescription());
264 }
265 }
266
267 // line info
268 if(!sPrintRestrict) {
269 std::vector<ObjectFile::LineInfo>* lineInfo = atom->getLineInfo();
270 if ( (lineInfo != NULL) && (lineInfo->size() > 0) ) {
271 printf("line info: (%lu)\n", lineInfo->size());
272 for (std::vector<ObjectFile::LineInfo>::iterator it = lineInfo->begin(); it != lineInfo->end(); ++it) {
273 printf(" offset 0x%04X, line %d, file %s\n", it->atomOffset, it->lineNumber, it->fileName);
274 }
275 }
276 }
277
278 if(!sPrintRestrict)
279 printf("\n");
280 }
281
282 struct AtomSorter
283 {
284 bool operator()(const ObjectFile::Atom* left, const ObjectFile::Atom* right)
285 {
286 if ( left == right )
287 return false;
288 return (strcmp(left->getDisplayName(), right->getDisplayName()) < 0);
289 }
290 };
291
292
293 static void dumpFile(ObjectFile::Reader* reader)
294 {
295 // stabs debug info
296 if ( sDumpStabs && (reader->getDebugInfoKind() == ObjectFile::Reader::kDebugInfoStabs) ) {
297 std::vector<ObjectFile::Reader::Stab>* stabs = reader->getStabs();
298 if ( stabs != NULL )
299 dumpStabs(stabs);
300 }
301
302 // get all atoms
303 std::vector<ObjectFile::Atom*> atoms = reader->getAtoms();
304
305 // make copy of vector and sort (so output is canonical)
306 std::vector<ObjectFile::Atom*> sortedAtoms(atoms);
307 if ( sSort )
308 std::sort(sortedAtoms.begin(), sortedAtoms.end(), AtomSorter());
309
310 for(std::vector<ObjectFile::Atom*>::iterator it=sortedAtoms.begin(); it != sortedAtoms.end(); ++it)
311 dumpAtom(*it);
312 }
313
314
315 static ObjectFile::Reader* createReader(const char* path, const ObjectFile::ReaderOptions& options)
316 {
317 struct stat stat_buf;
318
319 int fd = ::open(path, O_RDONLY, 0);
320 if ( fd == -1 )
321 throwf("cannot open file: %s", path);
322 ::fstat(fd, &stat_buf);
323 uint8_t* p = (uint8_t*)::mmap(NULL, stat_buf.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
324 ::close(fd);
325 const mach_header* mh = (mach_header*)p;
326 if ( mh->magic == OSSwapBigToHostInt32(FAT_MAGIC) ) {
327 const struct fat_header* fh = (struct fat_header*)p;
328 const struct fat_arch* archs = (struct fat_arch*)(p + sizeof(struct fat_header));
329 for (unsigned long i=0; i < OSSwapBigToHostInt32(fh->nfat_arch); ++i) {
330 if ( OSSwapBigToHostInt32(archs[i].cputype) == (uint32_t)sPreferredArch ) {
331 p = p + OSSwapBigToHostInt32(archs[i].offset);
332 mh = (struct mach_header*)p;
333 }
334 }
335 }
336 if ( mach_o::relocatable::Reader<x86>::validFile(p) )
337 return new mach_o::relocatable::Reader<x86>::Reader(p, path, 0, options, 0);
338 else if ( mach_o::relocatable::Reader<ppc>::validFile(p) )
339 return new mach_o::relocatable::Reader<ppc>::Reader(p, path, 0, options, 0);
340 else if ( mach_o::relocatable::Reader<ppc64>::validFile(p) )
341 return new mach_o::relocatable::Reader<ppc64>::Reader(p, path, 0, options, 0);
342 else if ( mach_o::relocatable::Reader<x86_64>::validFile(p) )
343 return new mach_o::relocatable::Reader<x86_64>::Reader(p, path, 0, options, 0);
344 throwf("not a mach-o object file: %s", path);
345 }
346
347 static
348 void
349 usage()
350 {
351 fprintf(stderr, "ObjectDump options:\n"
352 "\t-no_content\tdon't dump contents\n"
353 "\t-stabs\t\tdump stabs\n"
354 "\t-arch aaa\tonly dump info about arch aaa\n"
355 "\t-only sym\tonly dump info about sym\n"
356 "\t-align\t\tonly print alignment info\n"
357 "\t-name\t\tonly print symbol names\n"
358 );
359 }
360
361 int main(int argc, const char* argv[])
362 {
363 if(argc<2) {
364 usage();
365 return 0;
366 }
367
368 ObjectFile::ReaderOptions options;
369 try {
370 for(int i=1; i < argc; ++i) {
371 const char* arg = argv[i];
372 if ( arg[0] == '-' ) {
373 if ( strcmp(arg, "-no_content") == 0 ) {
374 sDumpContent = false;
375 }
376 else if ( strcmp(arg, "-stabs") == 0 ) {
377 sDumpStabs = true;
378 }
379 else if ( strcmp(arg, "-no_sort") == 0 ) {
380 sSort = false;
381 }
382 else if ( strcmp(arg, "-arch") == 0 ) {
383 const char* arch = ++i<argc? argv[i]: "";
384 if ( strcmp(arch, "ppc64") == 0 )
385 sPreferredArch = CPU_TYPE_POWERPC64;
386 else if ( strcmp(arch, "ppc") == 0 )
387 sPreferredArch = CPU_TYPE_POWERPC;
388 else if ( strcmp(arch, "i386") == 0 )
389 sPreferredArch = CPU_TYPE_I386;
390 else if ( strcmp(arch, "x86_64") == 0 )
391 sPreferredArch = CPU_TYPE_X86_64;
392 else
393 throwf("unknown architecture %s", arch);
394 }
395 else if ( strcmp(arg, "-only") == 0 ) {
396 sMatchName = ++i<argc? argv[i]: NULL;
397 }
398 else if ( strcmp(arg, "-align") == 0 ) {
399 sPrintRestrict = true;
400 sPrintAlign = true;
401 }
402 else if ( strcmp(arg, "-name") == 0 ) {
403 sPrintRestrict = true;
404 sPrintName = true;
405 }
406 else {
407 usage();
408 throwf("unknown option: %s\n", arg);
409 }
410 }
411 else {
412 ObjectFile::Reader* reader = createReader(arg, options);
413 dumpFile(reader);
414 }
415 }
416 }
417 catch (const char* msg) {
418 fprintf(stderr, "ObjDump failed: %s\n", msg);
419 return 1;
420 }
421
422 return 0;
423 }