]> git.saurik.com Git - apple/ld64.git/blob - unit-tests/test-cases/efi-basic/mtoctest.py
ld64-123.2.tar.gz
[apple/ld64.git] / unit-tests / test-cases / efi-basic / mtoctest.py
1 #!/usr/bin/python
2
3 import sys
4 import getopt
5
6 def Usage():
7 print "Unkown command line args"
8
9
10 def main():
11 try:
12 opts, args = getopt.getopt(sys.argv[1:],"hav:",["help","arch=","verbose"])
13 except getopt.GetoptError:
14 print "Unkown command line args"
15 sys.exit(2)
16
17 Verbose = 0
18 for o,a in opts:
19 if o in ("-h", "--help"):
20 Usage ()
21 sys.exit ()
22 elif o in ("-a", "--arch"):
23 Arch = a
24 else:
25 Usage ()
26 sys.exit ()
27
28
29 if Verbose:
30 print "\nmach-o load commands:"
31 otoolload = open("otool-load.log", "r")
32 data = otoolload.read()
33 otoolload.close()
34
35
36 # extract extry point from ' ss 0x00000000 eflags 0x00000000 eip 0x00000259 cs 0x00000000'
37 if Arch == "i386":
38 eip = data.find("eip")
39 if eip != -1:
40 EntryPoint = int (data[eip + 4:eip + 4 + 10], 16)
41
42 if Arch == "arm":
43 r15 = data.find("r15")
44 if r15 != -1:
45 EntryPoint = int (data[r15 + 4:r15 + 4 + 10], 16)
46
47 # extract entry point from ' r15 0x0000000000000000 rip 0x0000000000000253'
48 if Arch == "x86_64":
49 rip = data.find("rip")
50 if rip != -1:
51 EntryPoint = int (data[rip + 4:rip + 4 + 18], 16)
52
53 if EntryPoint == 0:
54 print "FAIL - no entry point for PE/COFF image"
55 sys.exit(-1)
56 else:
57 if Verbose:
58 print "Entry Point = 0x%08x" % EntryPoint
59
60
61 if Verbose:
62 print "\nPE/COFF dump:"
63 objdump = open("objdump-raw.log", "r")
64 data = objdump.read()
65 objdump.close()
66
67 # Extract 'SizeOfImage 00000360'
68 Index = data.find("SizeOfImage")
69 End = data[Index:].find("\n")
70 SizeOfImage = int (data[Index+11:Index + End], 16);
71 if Verbose:
72 print "SizeOfImage = 0x%08x" % SizeOfImage
73
74 #Parse ' 0 .text 00000080 00000240 00000240 00000240 2**2'
75 # ' CONTENTS, ALLOC, LOAD, READONLY, CODE '
76 EndOfTable = data.find("SYMBOL TABLE:")
77 Index = data.find("Idx Name")
78 End = data[Index:].find("\n")
79 Index = Index + End + 1
80
81 PeCoffEnd = 0
82 while Index < EndOfTable:
83 End = data[Index:].find("\n")
84 Split = data[Index:Index+End].split()
85 # Split[0] Indx
86 # Split[1] Name i.e. .text
87 # Split[2] Size
88 # Split[3] VMA
89 # Split[4] LMA
90 # Split[5] File Off
91 # Split[6] Align
92 if int(Split[3],16) != int(Split[5],16):
93 print "FAIL - %s VMA %08x not equal File off %08x XIP will not work" % (Split[1], int(Split[3],16), int(Split[5],16))
94 sys.exit(-1)
95
96 if int(Split[3],16) + int(Split[2],16) > PeCoffEnd:
97 PeCoffEnd = int(Split[3],16) + int(Split[2],16)
98
99 if Split[1] == ".text":
100 SecStart = int(Split[3],16)
101 SecEnd = int(Split[3],16) + int(Split[2],16)
102 if (EntryPoint < SecStart) or (EntryPoint > SecEnd):
103 print "FAIL - Entry point (0x%x) not in .text section (0x%x - 0x%x)" % (EntryPoint, SecStart, SecEnd)
104 sys.exit(-1)
105
106 if Verbose:
107 print "%10s" % Split[1] + ' ' + Split[2] + ' ' + Split[3] + ' ' + Split[4] + ' ' + Split[5] + " End = %x" % PeCoffEnd
108 Index += data[Index:].find("\n") + 1
109 Index += data[Index:].find("\n") + 1
110
111 if SizeOfImage < PeCoffEnd:
112 print "FAIL - PE/COFF Header SizeOfImage (0x%x) is not correct. Image larger than size (0x%x)." % (SizeOfImage, PeCoffEnd)
113 sys.exit(-1)
114
115 if Verbose:
116 print "\nmach-o relocations:"
117 otoolreloc = open("otool-reloc.log", "r")
118 lines = otoolreloc.readlines()
119 otoolreloc.close()
120
121 found = False
122 for line in lines:
123 if found:
124 chunk = line.split()
125 if Verbose:
126 print chunk[0]
127 if line.find ("address") > -1:
128 found = True
129
130 if Verbose:
131 print
132
133
134 if __name__ == "__main__":
135 main()