{simple,shoddy,smart} minsize-oriented linker
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
14 KiB

1 year ago
  1. #!/usr/bin/env python3
  2. import argparse
  3. import glob
  4. import itertools
  5. import os, os.path
  6. import shutil
  7. import subprocess
  8. import sys
  9. import tempfile
  10. from smol.shared import *
  11. from smol.parse import *
  12. from smol.emit import *
  13. from smol.cnl import *
  14. def preproc_args(args):
  15. if args.hash16 and args.crc32c: # shouldn't happen anymore
  16. error("Cannot combine --hash16 and --crc32c!")
  17. if args.debug:
  18. args.cflags.append('-g')
  19. args.ldflags.append('-g')
  20. args.asflags.append('-g')
  21. if args.hash16 or args.crc32c:
  22. args.fuse_dnload_loader = True
  23. args.fskip_zero_value = args.fskip_zero_value or args.fuse_dnload_loader
  24. if args.fskip_zero_value: args.asflags.insert(0, "-DSKIP_ZERO_VALUE")
  25. if args.fuse_nx: args.asflags.insert(0, "-DUSE_NX")
  26. if args.fskip_entries: args.asflags.insert(0, "-DSKIP_ENTRIES")
  27. if args.funsafe_dynamic: args.asflags.insert(0, "-DUNSAFE_DYNAMIC")
  28. if args.fno_start_arg: args.asflags.insert(0, "-DNO_START_ARG")
  29. if args.fuse_dl_fini: args.asflags.insert(0, "-DUSE_DL_FINI")
  30. if args.fuse_dt_debug: args.asflags.insert(0, "-DUSE_DT_DEBUG")
  31. if args.fuse_dnload_loader: args.asflags.insert(0, "-DUSE_DNLOAD_LOADER")
  32. if args.fuse_interp: args.asflags.insert(0, "-DUSE_INTERP")
  33. if args.falign_stack: args.asflags.insert(0, "-DALIGN_STACK")
  34. if args.fifunc_support: args.asflags.insert(0, "-DIFUNC_SUPPORT")
  35. if args.fifunc_strict_cconv: args.asflags.insert(0, "-DIFUNC_CORRECT_CCONV")
  36. if args.hang_on_startup: args.asflags.insert(0, "-DHANG_ON_STARTUP")
  37. for x in ['nasm','cc','readelf']:
  38. val = args.__dict__[x]
  39. if val is None or not os.path.isfile(val):
  40. error("'%s' binary%s not found" %
  41. (x, ("" if val is None else (" ('%s')" % val))))
  42. arch = args.target.tolower() if len(args.target) != 0 else decide_arch(args.input)
  43. if arch not in archmagic:
  44. error("Unknown/unsupported architecture '%s'" % str(arch))
  45. if args.verbose: eprintf("arch: %s" % str(arch))
  46. if args.hash16 and arch not in ('i386', 3):
  47. error("Cannot use --hash16 for arch `%s' (not i386)" % (arch))
  48. return args, arch
  49. def do_smol_run(args, arch):
  50. objinput = None
  51. objinputistemp = False
  52. tmp_asm_file, tmp_elf_fd, tmp_elf_file = None, None, None
  53. if not args.gen_rt_only:
  54. tmp_asm_file = tempfile.mkstemp(prefix='smoltab',suffix='.asm',text=True)
  55. tmp_asm_fd = tmp_asm_file[0]
  56. tmp_asm_file = tmp_asm_file[1]
  57. tmp_elf_file = tempfile.mkstemp(prefix='smolout',suffix='.o')
  58. os.close(tmp_elf_file[0])
  59. tmp_elf_file = tmp_elf_file[1]
  60. try:
  61. #for inp in args.input:
  62. # if not is_valid_elf(inp):
  63. # error("Input file '%s' is not a valid ELF file!" % inp)
  64. # if >1 input OR input is LTO object:
  65. if len(args.input) > 1 or has_lto_object(args.readelf, args.input):
  66. fd, objinput = tempfile.mkstemp(prefix='smolin',suffix='.o')
  67. objinputistemp = True
  68. os.close(fd)
  69. cc_relink_objs(args.verbose, args.cc, arch, args.input, objinput, args.cflags)
  70. else:
  71. objinput = args.input[0]
  72. # generate smol hashtab
  73. cc_paths = get_cc_paths(args.cc)
  74. syms = get_needed_syms(args.readelf, objinput)
  75. spaths = args.libdir + cc_paths['libraries']
  76. libraries = cc_paths['libraries']
  77. libs = find_libs(spaths, args.library)
  78. if args.verbose:
  79. eprintf("libs = %s" % str(libs))
  80. libs_symbol_map = build_symbol_map(args.readelf, libs)
  81. #symbols = {}
  82. #for symbol, reloc in syms:
  83. # if symbol not in libs_symbol_map:
  84. # error("could not find symbol: {}".format(symbol))
  85. # libs_for_symbol = libs_symbol_map[symbol]
  86. # if len(libs_for_symbol) > 1:
  87. # error("E: the symbol '%s' is provided by more than one library: %s"
  88. # % (symbol, str(libs_for_symbol)))
  89. # library = libs_for_symbol.pop()
  90. # symbols.setdefault(library, [])
  91. # symbols[library].append((symbol, reloc))
  92. symbols = resolve_extern_symbols(syms, libs_symbol_map, args)
  93. with (open(args.output,'w') if args.gen_rt_only
  94. else os.fdopen(tmp_asm_fd, mode='w')) as taf:
  95. output(arch, symbols, args.nx, get_hash_id(args.hash16, args.crc32c), taf, args.det)
  96. if args.verbose:
  97. eprintf("wrote symtab to %s" % tmp_asm_file)
  98. if not args.gen_rt_only:
  99. # assemble hash table/ELF header
  100. nasm_assemble_elfhdr(args.verbose, args.nasm, arch, args.smolrt,
  101. tmp_asm_file, tmp_elf_file, args.asflags)
  102. # link with LD into the final executable, w/ special linker script
  103. if args.debugout is not None: # do this first, so the linker map output will use the final output binary
  104. ld_link_final(args.verbose, args.cc, arch, args.smolld, [objinput, tmp_elf_file],
  105. args.debugout, args.ldflags, True)
  106. ld_link_final(args.verbose, args.cc, arch, args.smolld, [objinput, tmp_elf_file],
  107. args.output, args.ldflags, False)
  108. finally:
  109. if not args.keeptmp:
  110. if objinputistemp: os.remove(objinput)
  111. if not args.gen_rt_only: os.remove(tmp_asm_file)
  112. os.remove(tmp_elf_file)
  113. def main():
  114. parser = argparse.ArgumentParser()
  115. parser.add_argument('-m', '--target', default='', \
  116. help='architecture to generate asm code for (default: auto)')
  117. parser.add_argument('-l', '--library', default=[], metavar='LIB', action='append', \
  118. help='libraries to link against')
  119. parser.add_argument('-L', '--libdir', default=[], metavar='DIR', action='append', \
  120. help="directories to search libraries in")
  121. hashgrp = parser.add_mutually_exclusive_group()
  122. hashgrp.add_argument('-s', '--hash16', default=False, action='store_true', \
  123. help="Use 16-bit (BSD2) hashes instead of 32-bit djb2 hashes. "+\
  124. "Implies -fuse-dnload-loader. Only usable for 32-bit output.")
  125. hashgrp.add_argument('-c', '--crc32c', default=False, action='store_true', \
  126. help="Use Intel's crc32 intrinsic for hashing. "+\
  127. "Implies -fuse-dnload-loader. Conflicts with `--hash16'.")
  128. parser.add_argument('-n', '--nx', default=False, action='store_true', \
  129. help="Use NX (i.e. don't use RWE pages). Costs the size of one phdr, "+\
  130. "plus some extra bytes on i386.")
  131. parser.add_argument('-d', '--det', default=False, action='store_true', \
  132. help="Make the order of imports deterministic (default: just use " + \
  133. "whatever binutils throws at us)")
  134. parser.add_argument('-g', '--debug', default=False, action='store_true', \
  135. help="Pass `-g' to the C compiler, assembler and linker. Only useful "+\
  136. "when `--debugout' is specified.")
  137. parser.add_argument('-fuse-interp', default=True, action='store_true', \
  138. help="[Default ON] Include a program interpreter header (PT_INTERP). If not " +\
  139. "enabled, ld.so has to be invoked manually by the end user. "+\
  140. "Disable with `-fno-use-interp'.",
  141. dest="fuse_interp")
  142. parser.add_argument('-fno-use-interp', action='store_false', \
  143. dest="fuse_interp", help=argparse.SUPPRESS)
  144. parser.add_argument('-falign-stack', default=True, action='store_true', \
  145. help="[Default ON] Align the stack before running user code (_start). If not " + \
  146. "enabled, this has to be done manually. Costs 1 byte. Disable "+\
  147. "with `-fno-align-stack'.", dest="falign_stack")
  148. parser.add_argument('-fno-align-stack', action='store_false', \
  149. dest="falign_stack", help=argparse.SUPPRESS)
  150. parser.add_argument('-fskip-zero-value', default=None, action='store_true', \
  151. help="[Default: ON if `-fuse-dnload-loader' supplied, OFF otherwise] "+\
  152. "Skip an ELF symbol with a zero address (a weak symbol) when "+\
  153. "parsing libraries at runtime. Try enabling this if you're "+\
  154. "experiencing sudden breakage. However, many libraries don't use "+\
  155. "weak symbols, so this doesn't often pose a problem. Costs ~5 bytes."+\
  156. "Disable with `-fno-skip-zero-value'.", dest="fskip_zero_value")
  157. parser.add_argument('-fno-skip-zero-value', default=None, action='store_false', \
  158. dest="fskip_zero_value", help=argparse.SUPPRESS)
  159. parser.add_argument('-fifunc-support', default=True, action='store_true', \
  160. help="[Default ON] Support linking to IFUNCs. Probably needed on x86_64, but costs "+\
  161. "~16 bytes. Ignored on platforms without IFUNC support. Disable "+\
  162. "with `-fno-fifunc-support'.", dest="fifunc_support")
  163. parser.add_argument('-fno-ifunc-support', action='store_false', \
  164. dest="fifunc_support", help=argparse.SUPPRESS)
  165. parser.add_argument('-fuse-dnload-loader', default=False, action='store_true', \
  166. help="Use a dnload-style loader for resolving symbols, which doesn't "+\
  167. "depend on nonstandard/undocumented ELF and ld.so features, but "+\
  168. "is slightly larger. If not enabled, a smaller custom loader is "+\
  169. "used which assumes glibc. `-fskip-zero-value' defaults to ON if "+\
  170. "this flag is supplied.")
  171. parser.add_argument('-fuse-nx', default=False, action='store_true', \
  172. help="Don't use one big RWE segment, but use separate RW and RE ones."+\
  173. " Use this to keep strict kernels (PaX/grsec) happy. Costs at "+\
  174. "least the size of one program header entry.")
  175. parser.add_argument('-fuse-dt-debug', default=False, action='store_true', \
  176. help="Use the DT_DEBUG Dyn header to access the link_map, which doesn't"+\
  177. " depend on nonstandard/undocumented ELF and ld.so features. If "+\
  178. "not enabled, the link_map is accessed using data leaked to the "+\
  179. "entrypoint by ld.so, which assumes glibc. Costs ~10 bytes.")
  180. parser.add_argument('-fuse-dl-fini', default=False, action='store_true', \
  181. help="Pass _dl_fini to the user entrypoint, which should be done to "+\
  182. "properly comply with all standards, but is very often not "+\
  183. "needed at all. Costs 2 bytes.")
  184. parser.add_argument('-fskip-entries', default=False, action='store_true', \
  185. help="Skip the first two entries in the link map (resp. ld.so and "+\
  186. "the vDSO). Speeds up symbol resolving, but costs ~5 bytes.")
  187. parser.add_argument('-fno-start-arg', default=False, action='store_true', \
  188. help="Don't pass a pointer to argc/argv/envp to the entrypoint using "+\
  189. "the standard calling convention. This means you need to read "+\
  190. "these yourself in assembly if you want to use them! (envp is "+\
  191. "a preprequisite for X11, because it needs $DISPLAY.) Frees 3 bytes.")
  192. parser.add_argument('-funsafe-dynamic', default=False, action='store_true', \
  193. help="Don't end the ELF Dyn table with a DT_NULL entry. This might "+\
  194. "cause ld.so to interpret the entire binary as the Dyn table, "+\
  195. "so only enable this if you're sure this won't break things!")
  196. parser.add_argument('-fifunc-strict-cconv', default=False, action='store_true', \
  197. help="On i386, if -fifunc-support is specified, strictly follow the "+\
  198. "calling convention rules. Probably not needed, but you never know.")
  199. parser.add_argument('--nasm', default=os.getenv('NASM') or shutil.which('nasm'), \
  200. help="which nasm binary to use")
  201. parser.add_argument('--cc', default=os.getenv('CC') or shutil.which('cc'), \
  202. help="which cc binary to use (MUST BE GCC!)")
  203. parser.add_argument('--readelf', default=os.getenv('READELF') or shutil.which('readelf'), \
  204. help="which readelf binary to use")
  205. parser.add_argument('-Wc','--cflags', default=[], metavar='CFLAGS', action='append',
  206. help="Flags to pass to the C compiler for the relinking step")
  207. parser.add_argument('-Wa','--asflags', default=[], metavar='ASFLAGS', action='append',
  208. help="Flags to pass to the assembler when creating the ELF header and runtime startup code")
  209. parser.add_argument('-Wl','--ldflags', default=[], metavar='LDFLAGS', action='append',
  210. help="Flags to pass to the linker for the final linking step")
  211. parser.add_argument('--smolrt', default=os.getcwd()+"/rt",
  212. help="Directory containing the smol runtime sources")
  213. parser.add_argument('--smolld', default=os.getcwd()+"/ld",
  214. help="Directory containing the smol linker scripts")
  215. parser.add_argument('--gen-rt-only', default=False, action='store_true', \
  216. help="Only generate the headers/runtime assembly source file, instead"+\
  217. " of doing a full link. (I.e. fall back to pre-release behavior.)")
  218. parser.add_argument('--verbose', default=False, action='store_true', \
  219. help="Be verbose about what happens and which subcommands are invoked")
  220. parser.add_argument('--keeptmp', default=False, action='store_true', \
  221. help="Keep temp files (only useful for debugging)")
  222. parser.add_argument('--debugout', type=str, default=None, \
  223. help="Write out an additional, unrunnable debug ELF file with symbol "+\
  224. "information. (Useful for debugging with gdb, cannot be ran due "+\
  225. "to broken relocations.)")
  226. parser.add_argument('--hang-on-startup', default=False, action='store_true', \
  227. help="Hang on startup until a debugger breaks the code out of the "+\
  228. "loop. Only useful for debugging.")
  229. parser.add_argument('input', nargs='+', help="input object file")
  230. parser.add_argument('output', type=str, help="output binary")
  231. args = parser.parse_args()
  232. args, arch = preproc_args(args)
  233. do_smol_run(args, arch)
  234. if __name__ == '__main__':
  235. rv = main()
  236. if rv is None: pass
  237. else:
  238. try: sys.exit(int(rv))
  239. except: sys.exit(1)