[PATCH] implement MachODumper::printFileHeaders

Frederic Riss friss at apple.com
Tue Nov 11 15:55:43 PST 2014


Thanks for working on this!

This needs some test coverage, you need to update test/tools/llvm-readobj/file-headers.test. Other comments inline.

================
Comment at: tools/llvm-readobj/MachODumper.cpp:207-208
@@ -206,3 +206,4 @@
 
 void MachODumper::printFileHeaders() {
-  W.startLine() << "FileHeaders not implemented.\n";
+  DictScope H(W, "Header");
+  if (!Obj->is64Bit()) {
----------------
Not sure about the convention of llvm-readobj, but you might want to call that MachHeader instead (The ELF one is called ElfHeader, and moreover this would nicely match name of the structure it is describing)

================
Comment at: tools/llvm-readobj/MachODumper.cpp:209-228
@@ -209,1 +208,22 @@
+  DictScope H(W, "Header");
+  if (!Obj->is64Bit()) {
+    const MachO::mach_header Header = Obj->getHeader();
+    W.printHex("Magic", Header.magic);
+    W.printNumber("CpuType", Header.cputype);
+    W.printNumber("CpuSubtype", Header.cpusubtype);
+    W.printNumber("FileType", Header.filetype);
+    W.printNumber("NumOfCommands", Header.ncmds);
+    W.printNumber("SizeOfCommands", Header.sizeofcmds);
+    W.printHex("Flags", Header.flags);
+  } else {
+    const MachO::mach_header_64 Header64 = Obj->getHeader64();
+    W.printHex("Magic", Header64.magic);
+    W.printNumber("CpuType", Header64.cputype);
+    W.printNumber("CpuSubtype", Header64.cpusubtype);
+    W.printNumber("FileType", Header64.filetype);
+    W.printNumber("NumOfCommands", Header64.ncmds);
+    W.printNumber("SizeOfCommands", Header64.sizeofcmds);
+    W.printHex("Flags", Header64.flags);
+    W.printHex("Reserved", Header64.reserved);
+  }
 }
----------------
I think this would be nicer with a little template helper to use the same code for dumping the common fields. And then just special case the Reserved field that appears only in the 64bits version (given the name, I'm wondering if it should be dumped at all).

And most of the fields could be pretty-printed, you can take inspiration from what is done in ELFDumper and use the various enum/helpers in llvm/Support/MachO.h.

http://reviews.llvm.org/D6163






More information about the llvm-commits mailing list