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 General Public License as published by the Free Software Foundation,
11 # either version 3 of the License or (at your option) any later version.
13 __author__ = "Amand Tihon <amand.tihon@alrj.org>"
17 from Bold.linker import BoldLinker
18 from Bold.errors import *
19 from optparse import OptionParser
23 class BoldOptionParser(OptionParser):
24 """Bold option parser."""
26 _usage_message = "%prog [options] objfile..."
27 _version_message = "%%prog version %s" % __version__
28 _description_message = """A limited ELF linker for x86_64. It is
29 intended to create very small executables with the least possible overhead."""
32 OptionParser.__init__(self, usage=self._usage_message,
33 version=self._version_message, description=self._description_message,
34 add_help_option=True, prog="bold")
36 self.set_defaults(entry=None, outfile="a.out", raw=False, ccall=False,
39 self.add_option("-e", "--entry", action="store", dest="entry",
40 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")
45 self.add_option("-L", "--library-path", action="append", dest="libpath",
47 help="Add DIRECTORY to library search path. (Ignored, for compatibility only.")
49 self.add_option("-o", "--output", action="store", dest="outfile",
50 metavar="FILE", help="Set output file name (default: a.out)")
52 self.add_option("--raw", action="store_true", dest="raw",
53 help="Don't include the builtin external symbols resolution code")
55 self.add_option("-c", "--ccall", action="store_true", dest="ccall",
56 help="Make external symbol callable by C (default: no)")
58 self.add_option("-a", "--align-ccall", action="store_true", dest="align",
59 help="Align C callable symbols with actual functions pointers")
63 parser = BoldOptionParser()
64 options, args = parser.parse_args()
67 print >>sys.stderr, "No input files"
73 if options.align and not options.ccall:
74 print >>sys.stderr, "Making external symbols callable by C because of -a."
77 if options.ccall and options.raw:
78 # ccall implies that we include the symbol resolution code...
79 print >>sys.stderr, "Including symbol resolution code because of -c."
83 for d in ['.', 'runtime', '/usr/lib/bold/', '/usr/local/lib/bold']:
84 f = os.path.join(d, 'bold_ibh-x86_64.o')
89 print >>sys.stderr, "Could not find bold_ibh-x86_64.o."
93 # Try reordering objects ?
97 for infile in objects:
99 linker.add_object(infile)
100 except UnsupportedObject, e:
101 print >>sys.stderr, e
104 print >>sys.stderr, e
109 for shlib in options.shlibs:
111 linker.add_shlib(shlib)
112 except LibNotFound, e:
113 print >>sys.stderr, e
116 if options.entry is not None:
117 linker.entry_point = options.entry
120 linker.entry_point = "_bold__ibh_start"
123 linker.build_symbols_tables()
124 linker.build_external(with_jump=options.ccall, align_jump=options.align)
127 except UndefinedSymbol, e:
128 print >>sys.stderr, e
130 except RedefinedSymbol, e:
131 print >>sys.stderr, e
134 # Remove the file if it was present
136 os.unlink(options.outfile)
138 if e.errno == 2: # No such file
142 o = open(options.outfile, "wb")
144 print >>sys.stderr, e
151 os.chmod(options.outfile, 0755)
153 print >>sys.stderr, e
159 if __name__ == "__main__":
164 print >>sys.stderr, "Unhandled error:", e