llvm.org GIT mirror llvm / 885eb98
[llvm-readobj] MachO: Dump segment command. Example output: File: <stdin> Format: Mach-O arm Arch: arm AddressSize: 32bit Segment { Cmd: LC_SEGMENT Name: Size: 260 vmaddr: 0x0 vmsize: 0x10 fileoff: 408 filesize: 408 maxprot: rwx initprot: rwx nsects: 3 flags: 0x0 } Differential Revision: http://reviews.llvm.org/D12542 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246665 91177308-0d34-0410-b5e6-96231b3b80d8 Davide Italiano 5 years ago
3 changed file(s) with 87 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
4343 void printMachODataInCode() override;
4444 void printMachOVersionMin() override;
4545 void printMachODysymtab() override;
46 void printMachOSegment() override;
4647
4748 private:
4849 template
262263 uint32_t Reserved3;
263264 };
264265
266 struct MachOSegment {
267 StringRef CmdName;
268 StringRef SegName;
269 uint64_t cmdsize;
270 uint64_t vmaddr;
271 uint64_t vmsize;
272 uint64_t fileoff;
273 uint64_t filesize;
274 uint32_t maxprot;
275 uint32_t initprot;
276 uint32_t nsects;
277 uint32_t flags;
278 };
279
265280 struct MachOSymbol {
266281 uint32_t StringIndex;
267282 uint8_t Type;
269284 uint16_t Flags;
270285 uint64_t Value;
271286 };
287 }
288
289 static std::string getMask(uint32_t prot)
290 {
291 // TODO (davide): This always assumes prot is valid.
292 // Catch mistakes and report if needed.
293 std::string Prot;
294 Prot = "";
295 Prot += (prot & MachO::VM_PROT_READ) ? "r" : "-";
296 Prot += (prot & MachO::VM_PROT_WRITE) ? "w" : "-";
297 Prot += (prot & MachO::VM_PROT_EXECUTE) ? "x" : "-";
298 return Prot;
272299 }
273300
274301 static void getSection(const MachOObjectFile *Obj,
300327 Section.Reserved3 = Sect.reserved3;
301328 }
302329
330 static void getSegment(const MachOObjectFile *Obj,
331 const MachOObjectFile::LoadCommandInfo &L,
332 MachOSegment &Segment) {
333 if (!Obj->is64Bit()) {
334 MachO::segment_command SC = Obj->getSegmentLoadCommand(L);
335 Segment.CmdName = "LC_SEGMENT";
336 Segment.SegName = SC.segname;
337 Segment.cmdsize = SC.cmdsize;
338 Segment.vmaddr = SC.vmaddr;
339 Segment.vmsize = SC.vmsize;
340 Segment.fileoff = SC.fileoff;
341 Segment.filesize = SC.filesize;
342 Segment.maxprot = SC.maxprot;
343 Segment.initprot = SC.initprot;
344 Segment.nsects = SC.nsects;
345 Segment.flags = SC.flags;
346 return;
347 }
348 MachO::segment_command_64 SC = Obj->getSegment64LoadCommand(L);
349 Segment.CmdName = "LC_SEGMENT_64";
350 Segment.SegName = SC.segname;
351 Segment.cmdsize = SC.cmdsize;
352 Segment.vmaddr = SC.vmaddr;
353 Segment.vmsize = SC.vmsize;
354 Segment.fileoff = SC.fileoff;
355 Segment.filesize = SC.filesize;
356 Segment.maxprot = SC.maxprot;
357 Segment.initprot = SC.initprot;
358 Segment.nsects = SC.nsects;
359 Segment.flags = SC.flags;
360 }
303361
304362 static void getSymbol(const MachOObjectFile *Obj,
305363 DataRefImpl DRI,
694752 }
695753 }
696754 }
755
756 void MachODumper::printMachOSegment() {
757 for (const auto &Load : Obj->load_commands()) {
758 if (Load.C.cmd == MachO::LC_SEGMENT || Load.C.cmd == MachO::LC_SEGMENT_64) {
759 MachOSegment MOSegment;
760 getSegment(Obj, Load, MOSegment);
761 DictScope Group(W, "Segment");
762 W.printString("Cmd", MOSegment.CmdName);
763 W.printString("Name", MOSegment.SegName);
764 W.printNumber("Size", MOSegment.cmdsize);
765 W.printHex("vmaddr", MOSegment.vmaddr);
766 W.printHex("vmsize", MOSegment.vmsize);
767 W.printNumber("fileoff", MOSegment.fileoff);
768 W.printNumber("filesize", MOSegment.fileoff);
769 W.printString("maxprot", getMask(MOSegment.maxprot));
770 W.printString("initprot", getMask(MOSegment.initprot));
771 W.printNumber("nsects", MOSegment.nsects);
772 W.printHex("flags", MOSegment.flags);
773 }
774 }
775 }
5858 virtual void printMachODataInCode() { }
5959 virtual void printMachOVersionMin() { }
6060 virtual void printMachODysymtab() { }
61 virtual void printMachOSegment() { }
6162
6263 virtual void printStackMap() const = 0;
6364
185185 cl::opt
186186 MachODataInCode("macho-data-in-code",
187187 cl::desc("Display MachO Data in Code command"));
188
189 // -macho-segment
190 cl::opt
191 MachOSegment("macho-segment",
192 cl::desc("Display MachO Segment command"));
188193
189194 // -macho-version-min
190195 cl::opt
330335 if (Obj->isMachO()) {
331336 if (opts::MachODataInCode)
332337 Dumper->printMachODataInCode();
338 if (opts::MachOSegment)
339 Dumper->printMachOSegment();
333340 if (opts::MachOVersionMin)
334341 Dumper->printMachOVersionMin();
335342 if (opts::MachODysymtab)