2 # -*- coding: utf-8 -*-
3 # kate: space-indent on; indent-width 2; mixedindent off; indent-mode python;
5 # Copyright (C) 2009 Amand 'alrj' Tihon <amand.tihon@alrj.org>
7 # This file is part of bold, the Byte Optimized Linker.
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.
13 __author__ = "Amand Tihon <amand.tihon@alrj.org>"
17 from Bold.linker import BoldLinker
18 from Bold.errors import *
19 from optparse import OptionParser
22 class BoldOptionParser(OptionParser):
23 """Bold option parser."""
25 _usage_message = "%prog [options] file..."
26 _version_message = "%%prog version %s" % __version__
27 _description_message = """A limited ELF linker for x86_64. It is
28 intended to create very small executables with the least possible overhead."""
31 OptionParser.__init__(self, usage=self._usage_message,
32 version=self._version_message, description=self._description_message,
33 add_help_option=True, prog="bold")
35 self.set_defaults(entry="_start", outfile="a.out", raw=False, ccall=False)
37 self.add_option("-e", "--entry", action="store", dest="entry",
38 metavar="SYMBOL", help="Set the entry point (default: _start)")
40 self.add_option("-l", "--library", action="append", dest="shlibs",
41 metavar="LIBNAME", help="Search for library LIBNAME")
43 self.add_option("-o", "--output", action="store", dest="outfile",
44 metavar="FILE", help="Set output file name (default: a.out)")
46 self.add_option("--raw", action="store_true", dest="raw",
47 help="Don't include the symbol resolution code (default: include it)")
49 self.add_option("-c", "--ccall", action="store_true", dest="ccall",
50 help="Make external symbol callable by C (default: no)")
54 parser = BoldOptionParser()
55 options, args = parser.parse_args()
58 print >>sys.stderr, "No input files"
65 linker.add_object(infile)
66 except UnsupportedObject, e:
73 if options.ccall and options.raw:
74 # ccall implies that we include the symbol resolution code...
75 print >>sys.stderr, "Including symbol resolution code because of -c."
79 for d in ['data', '/usr/lib/bold/', '/usr/local/lib/bold', '.']:
80 f = os.path.join(d, 'bold_ibh-x86_64.o')
84 except UnsupportedObject, e:
85 # file was found, but is not recognized
89 # not found, try next directory
92 print >>sys.stderr, "Could not find boldsymres-x86_64.o."
96 for shlib in options.shlibs:
98 linker.add_shlib(shlib)
99 except LibNotFound, e:
100 print >>sys.stderr, e
103 linker.entry_point = options.entry
106 linker.build_symbols_tables()
107 linker.build_external(with_jump=options.ccall)
110 except UndefinedSymbol, e:
111 print >>sys.stderr, e
113 except RedefinedSymbol, e:
114 print >>sys.stderr, e
117 # Remove the file if it was present
119 os.unlink(options.outfile)
121 if e.errno == 2: # No such file
125 o = open(options.outfile, "wb")
127 print >>sys.stderr, e
134 os.chmod(options.outfile, 0755)
136 print >>sys.stderr, e
142 if __name__ == "__main__":
147 print >>sys.stderr, "Unhandled error:", e