]> git.saurik.com Git - apple/ld64.git/blob - unit-tests/test-cases/efi-basic/mtoctest.py
ld64-409.12.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:],"ha:v",["help","arch=","verbose"])
13 except getopt.GetoptError:
14 print "Unkown command line args"
15 sys.exit(2)
16
17 Verbose = 0
18 EntryPoint = 0
19 for o,a in opts:
20 if o in ("-h", "--help"):
21 Usage ()
22 sys.exit ()
23 elif o in ("-a", "--arch"):
24 Arch = a
25 elif o in ("-v", "--verbose"):
26 Verbose = 1
27 else:
28 Usage ()
29 sys.exit ()
30
31 if Verbose:
32 print "\nmach-o load commands:"
33 otoolload = open("otool-load.log", "r")
34 data = otoolload.read()
35 otoolload.close()
36
37 # extract extry point from ' ss 0x00000000 eflags 0x00000000 eip 0x00000259 cs 0x00000000'
38 if Arch == "i386":
39 eip = data.find("eip")
40 if eip != -1:
41 EntryPoint = int (data[eip + 4:eip + 4 + 10], 16)
42
43 if Arch == "arm":
44 r15 = data.find("r15")
45 if r15 != -1:
46 EntryPoint = int (data[r15 + 4:r15 + 4 + 10], 16)
47
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 Arch == "arm64":
54 pc = data.find("pc")
55 if pc != -1:
56 EntryPoint = int (data[pc + 3:pc + 3 + 18], 16)
57
58 if EntryPoint == 0:
59 print "FAIL - no entry point for PE/COFF image"
60 sys.exit(-1)
61 else:
62 if Verbose:
63 print "Entry Point = 0x%08x" % EntryPoint
64
65
66 if Verbose:
67 print "\nPE/COFF dump:"
68 objdump = open("efi-pecoff-util-raw.log", "r")
69 data = objdump.read()
70 objdump.close()
71
72 # Extract 'SizeOfImage 00000360'
73 Index = data.find("SizeOfImage")
74 Index += data[Index:].find("=")
75 End = data[Index:].find("\n")
76 SizeOfImage = int (data[Index+1:Index + End], 16)
77 if Verbose:
78 print "SizeOfImage = 0x%08x" % SizeOfImage
79
80 # We used to parse output from objdump...
81 #Parse ' 0 .text 00000080 00000240 00000240 00000240 2**2'
82 # ' CONTENTS, ALLOC, LOAD, READONLY, CODE '
83 #
84 # But now we parse efi-pecoff-util
85 #Parse 'Sections:
86 # 'Name = .text
87 # 'VirtualSize = 0x00000100
88 # 'VirtualAddress = 0x00000240
89 # 'SizeOfRawData = 0x00000100
90 # 'PointerToRawData = 0x00000240
91 # 'PointerToRelocations = 0x00000000
92 # 'PointerToLinenumbers = 0x00000000
93 # 'NumberOfRelocations = 0x0000
94 # 'NumberOfLinenumbers = 0x0000
95 # 'Characteristics = 0x60000020
96 EndOfTable = data.find("PdbPointer")
97 Index = data.find("Sections:");
98 Index += data[Index:].find("\n");
99
100 Name = ""
101 Size = -1
102 VMA = -1
103 LMA = -1
104 FileOff = -1
105 PeCoffEnd = 0
106 while Index < EndOfTable:
107 End = data[Index:].find("\n")
108 Split = data[Index:Index+End].split()
109 # Split[0] Key
110 # Split[1] =
111 # Split[2] Value
112 if len(Split) == 0:
113 # blank line, we've finished reading a section. process results
114 if Name != "":
115 # make sure we've found everything
116 if Size == -1:
117 print "FAIL - %s Size missing" % Name
118 sys.exit(-1)
119 if VMA == -1:
120 print "FAIL - %s VMA missing" % Name
121 sys.exit(-1)
122 if LMA == -1:
123 print "FAIL - %s LMA missing" % Name
124 sys.exit(-1)
125 if FileOff == -1:
126 print "FAIL - %s FileOff missing" % Name
127 sys.exit(-1)
128
129 if VMA != FileOff:
130 print "FAIL - %s VMA %08x not equal File off %08x XIP will not work" % (Name, VMA, FileOff)
131 sys.exit(-1)
132
133 SecStart = VMA
134 SecEnd = VMA + Size
135 if SecEnd > PeCoffEnd:
136 PeCoffEnd = SecEnd
137
138 if Name == ".text":
139 if (EntryPoint < SecStart) or (EntryPoint > SecEnd):
140 print "FAIL - Entry point (0x%x) not in .text section (0x%x - 0x%x)" % (EntryPoint, SecStart, SecEnd)
141 sys.exit(-1)
142
143 if Verbose:
144 print "%10s %08x %016x %016x %08x" % (Name, Size, VMA, LMA, FileOff) + " End = %x" % PeCoffEnd
145
146 # clear values for next time
147 Name = ""
148 Size = -1
149 VMA = -1
150 LMA = -1
151 FileOff = -1
152 elif len(Split) == 3:
153 Key = Split[0]
154 Value = Split[2]
155
156 if Key == "Name":
157 Name = Value
158 if Key == "VirtualSize":
159 Size = int(Value,16)
160 if Key == "VirtualAddress":
161 VMA = int(Value,16)
162 LMA = VMA # BUG: on our platform the virtual memory address and the load memory address are the same?
163 if Key == "PointerToRawData":
164 FileOff = int(Value,16)
165 else:
166 print "FAIL - Line is not (key = value): '%s'" % data[Index:Index+End]
167 sys.exit(-1)
168
169 Index += data[Index:].find("\n") + 1
170
171 if SizeOfImage < PeCoffEnd:
172 print "FAIL - PE/COFF Header SizeOfImage (0x%x) is not correct. Image larger than size (0x%x)." % (SizeOfImage, PeCoffEnd)
173 sys.exit(-1)
174
175 if Verbose:
176 print "\nmach-o relocations:"
177 otoolreloc = open("otool-reloc.log", "r")
178 lines = otoolreloc.readlines()
179 otoolreloc.close()
180
181 found = False
182 for line in lines:
183 if found:
184 chunk = line.split()
185 if Verbose:
186 print chunk[0]
187 if line.find ("address") > -1:
188 found = True
189
190 if Verbose:
191 print
192
193
194 if __name__ == "__main__":
195 main()