|
5 | 5 |
|
6 | 6 | module Caotral |
7 | 7 | class Linker |
8 | | - def self.link!(inputs:, output: "a.out", linker: "mold", debug: false, shared: false) = new(inputs:, output:, linker:, debug:, shared:).link |
| 8 | + def self.link!(inputs:, output: "a.out", linker: "mold", debug: false, shared: false, executable: true) |
| 9 | + new(inputs:, output:, linker:, debug:, shared:, executable:).link |
| 10 | + end |
9 | 11 |
|
10 | | - def initialize(inputs:, output: "a.out", linker: "mold", linker_options: [], shared: false, debug: false) |
| 12 | + def initialize(inputs:, output: "a.out", linker: "mold", linker_options: [], executable: true, shared: false, debug: false) |
11 | 13 | @inputs, @output, @linker = inputs, output, linker |
12 | 14 | @options = linker_options |
13 | | - @debug, @shared = debug, shared |
| 15 | + @executable, @debug, @shared = executable, debug, shared |
14 | 16 | end |
15 | 17 |
|
16 | | - def link(inputs: @inputs, output: @output, debug: @debug, shared: @shared) |
17 | | - return to_elf(inputs:, output:, debug:) if @linker == "self" |
| 18 | + def link(inputs: @inputs, output: @output, debug: @debug, shared: @shared, executable: @executable) |
| 19 | + return to_elf(inputs:, output:, debug:, shared:, executable:) if @linker == "self" |
18 | 20 |
|
19 | 21 | IO.popen(link_command).close |
20 | 22 | end |
@@ -47,12 +49,14 @@ def link_command(inputs: @inputs, output: @output) |
47 | 49 | def libpath = @libpath ||= File.dirname(Dir.glob("/usr/lib*/**/crti.o").last) |
48 | 50 | def gcc_libpath = @gcc_libpath ||= File.dirname(Dir.glob("/usr/lib/gcc/x86_64-*/*/crtbegin.o").last) |
49 | 51 |
|
50 | | - def to_elf(inputs: @inputs, output: @output, debug: @debug) |
| 52 | + def to_elf(inputs: @inputs, output: @output, debug: @debug, shared: @shared, executable: @executable) |
51 | 53 | elf_objs = inputs.map { |input| Caotral::Binary::ELF::Reader.new(input:, debug:).read } |
52 | | - builder = Caotral::Linker::Builder.new(elf_objs:) |
| 54 | + builder = Caotral::Linker::Builder.new(elf_objs:, debug:, shared:, executable:) |
53 | 55 | builder.resolve_symbols |
54 | 56 | elf_obj = builder.build |
55 | | - Caotral::Linker::Writer.new(elf_obj:, output:, debug:).write |
| 57 | + Caotral::Linker::Writer.new(elf_obj:, output:, debug:, shared:, executable:).write |
| 58 | + File.chmod(0755, output) if executable |
| 59 | + output |
56 | 60 | end |
57 | 61 | end |
58 | 62 | end |
0 commit comments