[PATCH] implement MachODumper::printFileHeaders

Chilledheart rwindz0 at gmail.com
Thu Nov 13 07:24:28 PST 2014


>>! In D6163#26, @nrieck wrote:
> Since llvm-readobj is a low-level inspection tool, the Reserved field should also be printed (even more so than unused fields).

Yeah. As I know, macho-dump does dump the reserved field for 64-bit version of Mach-O format, while ``otool -h`` and ``llvm-objdump`` don't.

================
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()) {
----------------
friss wrote:
> 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)
Yeah, MachHeader is better.

================
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()) {
----------------
chilledheart wrote:
> friss wrote:
> > 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)
> Yeah, MachHeader is better.
Maybe I am a bit overconcerned about this. Since it (the header) belongs to Mach-O format, is MachOHeader more suitable than MachHeader?

================
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);
+  }
 }
----------------
friss wrote:
> 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.
Thanks for pointing it out. I will work on it soon.

http://reviews.llvm.org/D6163






More information about the llvm-commits mailing list