Subversion Repositories HelenOS

Rev

Rev 3023 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3023 Rev 3027
Line 34... Line 34...
34
import os
34
import os
35
import subprocess
35
import subprocess
36
 
36
 
37
def usage(prname):
37
def usage(prname):
38
    "Print usage syntax"
38
    "Print usage syntax"
39
    print prname + " <OBJCOPY> <FORMAT> <OUTPUT_FORMAT> <ARCH> <ALIGNMENT>"
39
    print prname + " <OBJCOPY> <FORMAT> <ARCH> <ALIGNMENT> <UINTPTR>"
40
 
40
 
41
def main():
41
def main():
42
    if (len(sys.argv) < 6):
42
    if (len(sys.argv) < 6):
43
        usage(sys.argv[0])
43
        usage(sys.argv[0])
44
        return
44
        return
45
   
45
   
46
    if (not sys.argv[5].isdigit()):
46
    if (not sys.argv[4].isdigit()):
47
        print "<ALIGNMENT> must be a number"
47
        print "<ALIGNMENT> must be a number"
48
        return
48
        return
49
   
49
   
50
    templatename = "_link.ld.in"
-
 
51
    workdir = os.getcwd()
-
 
52
    objcopy = sys.argv[1]
50
    objcopy = sys.argv[1]
53
    format = sys.argv[2]
51
    format = sys.argv[2]
54
    output_format = sys.argv[3]
52
    arch = sys.argv[3]
55
    arch = sys.argv[4]
53
    align = int(sys.argv[4], 0)
56
    align = int(sys.argv[5], 0)
54
    uintptr = sys.argv[5]
-
 
55
   
-
 
56
    workdir = os.getcwd()
57
   
57
   
58
    link = file("_link.ld", "w")
-
 
59
    header = file("_components.h", "w")
58
    header = file("_components.h", "w")
60
    data = file("_components.c", "w")
59
    data = file("_components.c", "w")
61
   
60
   
62
    link.write("OUTPUT_FORMAT(\"" + output_format + "\")\n")
-
 
63
    link.write("OUTPUT_ARCH(" + arch + ")\n")
-
 
64
    link.write("ENTRY(start)\n\n")
-
 
65
    link.write("SECTIONS {\n")
-
 
66
   
-
 
67
    size = os.path.getsize(templatename)
-
 
68
    rd = 0
-
 
69
    template = file(templatename, "r")
-
 
70
   
-
 
71
    while (rd < size):
-
 
72
        buf = template.read(4096)
-
 
73
        link.write(buf)
-
 
74
        rd += len(buf)
-
 
75
   
-
 
76
    template.close()
-
 
77
   
-
 
78
    link.write("\n")
-
 
79
   
-
 
80
    header.write("#ifndef ___COMPONENTS_H__\n")
61
    header.write("#ifndef ___COMPONENTS_H__\n")
81
    header.write("#define ___COMPONENTS_H__\n\n")
62
    header.write("#define ___COMPONENTS_H__\n\n")
82
   
63
   
83
    data.write("#include \"_components.h\"\n\n")
64
    data.write("#include \"_components.h\"\n\n")
84
    data.write("void init_components(component_t *components)\n")
65
    data.write("void init_components(component_t *components)\n")
85
    data.write("{\n")
66
    data.write("{\n")
86
   
67
   
87
    cnt = 0
68
    cnt = 0
-
 
69
    link = ""
88
    for task in sys.argv[6:]:
70
    for task in sys.argv[6:]:
89
        basename = os.path.basename(task)
71
        basename = os.path.basename(task)
90
        plainname = os.path.splitext(basename)[0]
72
        plainname = os.path.splitext(basename)[0]
91
        path = os.path.dirname(task)
73
        path = os.path.dirname(task)
92
        object = plainname + ".o"
74
        object = plainname + ".o"
93
        symbol = "_binary_" + basename.replace(".", "_")
75
        symbol = "_binary_" + basename.replace(".", "_")
94
        macro = plainname.upper()
76
        macro = plainname.upper()
95
       
77
       
96
        print task + " -> " + object
78
        print task + " -> " + object
97
       
79
       
-
 
80
        if (align > 1):
98
        link.write("\t\t. = ALIGN(" + ("%d" % align) + ");\n")
81
            link += "\t\t. = ALIGN(" + ("%d" % align) + ");\n"
-
 
82
       
99
        link.write("\t\t*(." + plainname + "_image);\n\n")
83
        link += "\t\t*(." + plainname + "_image);\n"
100
       
84
       
101
        header.write("extern int " + symbol + "_start;\n")
85
        header.write("extern int " + symbol + "_start;\n")
102
        header.write("extern int " + symbol + "_end;\n\n")
86
        header.write("extern int " + symbol + "_end;\n\n")
103
        header.write("#define " + macro + "_START ((void *) &" + symbol + "_start)\n")
87
        header.write("#define " + macro + "_START ((void *) &" + symbol + "_start)\n")
104
        header.write("#define " + macro + "_END ((void *) &" + symbol + "_end)\n")
88
        header.write("#define " + macro + "_END ((void *) &" + symbol + "_end)\n")
105
        header.write("#define " + macro + "_SIZE ((unsigned int) " + macro + "_END - (unsigned int) " + macro + "_START)\n\n")
89
        header.write("#define " + macro + "_SIZE ((" + uintptr + ") " + macro + "_END - (" + uintptr + ") " + macro + "_START)\n\n")
106
       
90
       
107
        data.write("\tcomponents[" + ("%d" % cnt) + "].name = \"" + plainname + "\";\n")
91
        data.write("\tcomponents[" + ("%d" % cnt) + "].name = \"" + plainname + "\";\n")
108
        data.write("\tcomponents[" + ("%d" % cnt) + "].start = " + macro + "_START;\n")
92
        data.write("\tcomponents[" + ("%d" % cnt) + "].start = " + macro + "_START;\n")
109
        data.write("\tcomponents[" + ("%d" % cnt) + "].end = " + macro + "_END;\n")
93
        data.write("\tcomponents[" + ("%d" % cnt) + "].end = " + macro + "_END;\n")
110
        data.write("\tcomponents[" + ("%d" % cnt) + "].size = " + macro + "_SIZE;\n\n")
94
        data.write("\tcomponents[" + ("%d" % cnt) + "].size = " + macro + "_SIZE;\n\n")
Line 116... Line 100...
116
            "-B", arch,
100
            "-B", arch,
117
            "--rename-section", ".data=." + plainname + "_image",
101
            "--rename-section", ".data=." + plainname + "_image",
118
            basename, os.path.join(workdir, object)])
102
            basename, os.path.join(workdir, object)])
119
        os.chdir(workdir)
103
        os.chdir(workdir)
120
       
104
       
121
        cnt = cnt + 1
105
        cnt += 1
122
       
106
       
123
    link.write("\t}\n")
-
 
124
    link.write("}\n")
-
 
125
   
-
 
126
    header.write("#define COMPONENTS " + ("%d" % cnt) + "\n\n")
107
    header.write("#define COMPONENTS " + ("%d" % cnt) + "\n\n")
127
    header.write("typedef struct {\n")
108
    header.write("typedef struct {\n")
128
    header.write("\tchar *name;\n\n")
109
    header.write("\tchar *name;\n\n")
129
    header.write("\tvoid *start;\n")
110
    header.write("\tvoid *start;\n")
130
    header.write("\tvoid *end;\n")
111
    header.write("\tvoid *end;\n")
131
    header.write("\tunsigned int size;\n")
112
    header.write("\t" + uintptr + " size;\n")
132
    header.write("} component_t;\n\n")
113
    header.write("} component_t;\n\n")
133
    header.write("extern void init_components(component_t *components);\n\n")
114
    header.write("extern void init_components(component_t *components);\n\n")
134
    header.write("#endif\n")
115
    header.write("#endif\n")
135
   
116
   
136
    data.write("}\n")
117
    data.write("}\n")
137
   
118
   
138
    link.close()
-
 
139
    header.close()
119
    header.close()
140
    data.close()
120
    data.close()
-
 
121
   
-
 
122
    linkname = "_link.ld"
-
 
123
    link_in = file(linkname + ".in", "r")
-
 
124
    template = link_in.read(os.path.getsize(linkname + ".in"))
-
 
125
    link_in.close()
-
 
126
   
-
 
127
    link_out = file(linkname, "w")
-
 
128
    link_out.write(template.replace("[[COMPONENTS]]", link))
-
 
129
    link_out.close()
141
 
130
 
142
if __name__ == '__main__':
131
if __name__ == '__main__':
143
    main()
132
    main()