]> git.alrj.org Git - bold.git/blob - bold.py
02f6626611aa4fdaca94ee2e6c6f2100b9ce7416
[bold.git] / bold.py
1 #! /usr/bin/python
2 # -*- coding: utf-8 -*-
3 # kate: space-indent on; indent-width 2; mixedindent off; indent-mode python;
4
5 # Copyright (C) 2009 Amand 'alrj' Tihon <amand.tihon@alrj.org>
6 #
7 # This file is part of bold, the Byte Optimized Linker.
8 #
9 # You can redistribute this file and/or modify it under the terms of the
10 # GNU Lesser General Public License as published by the Free Software
11 # Foundation, version 2.1.
12
13 #from bold.constants import *
14 #from bold.elf import Elf64, Elf64_Phdr, TextSegment, DataSegment, Dynamic, Interpreter
15
16 __author__ = "Amand Tihon <amand.tihon@alrj.org>"
17 __version__ = "0.0.1"
18
19
20 from Bold.linker import BoldLinker
21 from Bold.errors import *
22 from optparse import OptionParser
23 import os, sys
24
25 class BoldOptionParser(OptionParser):
26   """Bold option parser."""
27   global __version__
28   _usage_message = "%prog [options] file..."
29   _version_message = "%%prog version %s" % __version__
30   _description_message = """A limited ELF linker for x86_64. It is
31 intended to create very small executables with the least possible overhead."""
32
33   def __init__(self):
34     OptionParser.__init__(self, usage=self._usage_message,
35       version=self._version_message, description=self._description_message,
36       add_help_option=True, prog="bold")
37
38     self.set_defaults(entry="_start", outfile="a.out")
39
40     self.add_option("-e", "--entry", action="store", dest="entry",
41       metavar="SYMBOL", help="Set the entry point (default: _start)")
42     self.add_option("-l", "--library", action="append", dest="shlibs",
43       metavar="LIBNAME", help="Search for library LIBNAME")
44     self.add_option("-o", "--output", action="store", dest="outfile",
45       metavar="FILE", help="Set output file name (default: a.out)")
46
47
48 def main():
49   parser = BoldOptionParser()
50   options, args = parser.parse_args()
51
52   linker = BoldLinker()
53
54   if options.shlibs:
55     for shlib in options.shlibs:
56       try:
57         linker.add_shlib(shlib)
58       except LibNotFound, e:
59         print >>sys.stderr, e
60         return 1
61
62   if not args:
63     print >>sys.stderr, "No input files"
64     return 1
65
66   for infile in args:
67     try:
68       linker.add_object(infile)
69     except UnsupportedObject, e:
70       print >>sys.stderr, e
71       return 1
72     except IOError, e:
73       print >>sys.stderr, e
74       return 1
75
76   linker.entry_point = options.entry
77
78   try:
79     linker.link()
80   except UndefinedSymbol, e:
81     print >>sys.stderr, e
82     return 1
83   except RedefinedSymbol, e:
84     print >>sys.stderr, e
85     return 1
86
87   # Remove the file if it was present
88   try:
89     os.unlink(options.outfile)
90   except os.error, e:
91     if e.errno == 2: # No such file
92       pass
93
94   try:
95     o = open(options.outfile, "wb")
96   except IOError, e:
97     print >>sys.stderr, e
98     return 1
99
100   linker.tofile(o)
101   o.close()
102   
103   try:
104     os.chmod(options.outfile, 0755)
105   except IOError, e:
106     print >>sys.stderr, e
107     return 1
108
109   return 0
110
111
112 if __name__ == "__main__":
113   try:
114     rcode = main()
115   except Exception, e:
116     raise
117     print >>sys.stderr, "Unhandled error:", e
118     rcode = 1
119
120   exit(rcode)
121