Layout fixed area using linker instead of assembler.
[seabios.git] / tools / layoutrom.py
1 #!/usr/bin/env python
2 # Script to arrange sections to ensure fixed offsets.
3 #
4 # Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
5 #
6 # This file may be distributed under the terms of the GNU GPLv3 license.
7
8 import sys
9
10 def main():
11     # Read in section names and sizes
12
13     # sections = [(idx, name, size, align), ...]
14     sections = []
15     for line in sys.stdin.readlines():
16         try:
17             idx, name, size, vma, lma, fileoff, align = line.split()
18             if align[:3] != '2**':
19                 continue
20             sections.append((
21                 int(idx), name, int(size, 16), int(align[3:])))
22         except:
23             pass
24
25     # fixedsections = [(addr, sectioninfo), ...]
26     fixedsections = []
27     textsections = []
28     rodatasections = []
29     datasections = []
30
31     # Find desired sections.
32     for section in sections:
33         name = section[1]
34         if name[:11] == '.fixedaddr.':
35             addr = int(name[11:], 16)
36             fixedsections.append((addr, section))
37         if name[:6] == '.text.':
38             textsections.append(section)
39         if name[:17] == '.rodata.__func__.' or name == '.rodata.str1.1':
40             rodatasections.append(section)
41         if name[:8] == '.data16.':
42             datasections.append(section)
43
44     # Write regular sections
45     for section in textsections:
46         name = section[1]
47         sys.stdout.write("*(%s)\n" % (name,))
48     sys.stdout.write("code16_rodata = . ;\n")
49     for section in rodatasections:
50         name = section[1]
51         sys.stdout.write("*(%s)\n" % (name,))
52     for section in datasections:
53         name = section[1]
54         sys.stdout.write("*(%s)\n" % (name,))
55
56     # Write fixed sections
57     sys.stdout.write("freespace1_start = . ;\n")
58     first = 1
59     for addr, section in fixedsections:
60         name = section[1]
61         sys.stdout.write(". = ( 0x%x - code16_start ) ;\n" % (addr,))
62         if first:
63             first = 0
64             sys.stdout.write("freespace1_end = . ;\n")
65         sys.stdout.write("*(%s)\n" % (name,))
66
67 if __name__ == '__main__':
68     main()