Skip to content

Commit 7916aa5

Browse files
authored
Merge pull request #33 from katsyoshi/chmod-755-in-linker
add executable flag to linker
2 parents e0e5ed0 + e52274c commit 7916aa5

6 files changed

Lines changed: 31 additions & 22 deletions

File tree

lib/caotral/linker.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55

66
module Caotral
77
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
911

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)
1113
@inputs, @output, @linker = inputs, output, linker
1214
@options = linker_options
13-
@debug, @shared = debug, shared
15+
@executable, @debug, @shared = executable, debug, shared
1416
end
1517

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"
1820

1921
IO.popen(link_command).close
2022
end
@@ -47,12 +49,14 @@ def link_command(inputs: @inputs, output: @output)
4749
def libpath = @libpath ||= File.dirname(Dir.glob("/usr/lib*/**/crti.o").last)
4850
def gcc_libpath = @gcc_libpath ||= File.dirname(Dir.glob("/usr/lib/gcc/x86_64-*/*/crtbegin.o").last)
4951

50-
def to_elf(inputs: @inputs, output: @output, debug: @debug)
52+
def to_elf(inputs: @inputs, output: @output, debug: @debug, shared: @shared, executable: @executable)
5153
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:)
5355
builder.resolve_symbols
5456
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
5660
end
5761
end
5862
end

lib/caotral/linker/builder.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class Builder
1414

1515
attr_reader :symbols, :executable, :debug
1616

17-
def initialize(elf_objs:, executable: true, debug: false)
17+
def initialize(elf_objs:, executable: true, debug: false, shared: false)
1818
@elf_objs = elf_objs
19+
@executable, @debug, @shared = executable, debug, shared
1920
@symbols = { locals: Set.new, globals: Set.new, weaks: Set.new }
20-
@executable, @debug = executable, debug
2121
end
2222

2323
def build

lib/caotral/linker/writer.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ class Writer
99
ALLOW_RELOCATION_TYPES = [R_X86_64_PC32, R_X86_64_PLT32].freeze
1010
RELOCATION_SECTION_NAMES = [".rela.text", ".rel.text"].freeze
1111
attr_reader :elf_obj, :output, :entry, :debug
12-
def self.write!(elf_obj:, output:, entry: nil, debug: false)
13-
new(elf_obj:, output:, entry:, debug:).write
12+
def self.write!(elf_obj:, output:, entry: nil, debug: false, executable: true, shared: false)
13+
new(elf_obj:, output:, entry:, debug:, shared:, executable:).write
1414
end
15-
def initialize(elf_obj:, output:, entry: nil, debug: false)
16-
@elf_obj, @output, @entry, @debug = elf_obj, output, entry, debug
15+
def initialize(elf_obj:, output:, entry: nil, debug: false, executable: true, shared: false)
16+
@elf_obj, @output, @entry, @debug, @executable, @shared = elf_obj, output, entry, debug, executable, shared
1717
@write_sections = write_order_sections
1818
end
1919
def write

sig/caotral/linker.rbs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ class Caotral::Linker
33
@output: String
44
@linker: String
55
@options: Array[String]
6+
@executable: bool
67
@shared: bool
78
@debug: bool
89

9-
def self.link!: (inputs: Array[String], ?output: String, ?linker: String, ?debug: bool, ?shared: bool) -> void
10-
def initialize: (inputs: Array[String], ?output: String, ?linker: String, ?linker_options: Array[String], ?shared: bool, ?debug: bool) -> void
11-
def link: (inputs: Array[String], ?output: String, ?shared: bool, ?debug: bool) -> void
10+
def self.link!: (inputs: Array[String], ?output: String, ?linker: String, ?debug: bool, ?shared: bool, ?executable: bool) -> void
11+
def initialize: (inputs: Array[String], ?output: String, ?linker: String, ?linker_options: Array[String], ?executable: bool, ?shared: bool, ?debug: bool) -> void
12+
def link: (inputs: Array[String], ?output: String, ?shared: bool, ?debug: bool, ?executable: bool) -> void
1213

1314
def link_command: (inputs: Array[String], ?output: String, ?shared: bool, ?debug: bool) -> String
1415
def libpath: () -> String
1516
def gcc_libpath: () -> String
16-
def to_elf: (inputs: Array[String], ?output: String, ?debug: bool) -> String
17+
def to_elf: (inputs: Array[String], ?output: String, ?debug: bool, ?shared: bool, ?executable: bool) -> String
1718
end

sig/caotral/linker/builder.rbs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
class Caotral::Linker::Builder
2-
@elf_obj: Caotral::Binary::ELF
2+
@elf_objs: Array[Caotral::Binary::ELF]
33
@symbols: Hash[Symbol, Set[String]]
4+
@executable: bool
5+
@debug: bool
6+
@shared: bool
47

58
attr_reader symbols: Hash[Symbol, Set[String]]
69

7-
def initialize: (elf_obj: Caotral::Binary::ELF) -> void
10+
def initialize: (elf_objs: Array[Caotral::Binary::ELF], ?executable: bool, ?debug: bool, ?shared: bool) -> void
11+
def build: () -> Caotral::Binary::ELF
812
def resolve_symbols: () -> Hash[Symbol, Set[String]]
913
end

sig/caotral/linker/writer.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Caotral::Linker::Writer
2-
def self.write!: (elf_obj: Caotral::Binary::ELF, output: String, ?entry: Integer, ?debug: bool) -> String
3-
def initialize: (elf_obj: Caotral::Binary::ELF, output: String, ?entry: Integer, ?debug: bool) -> void
2+
def self.write!: (elf_obj: Caotral::Binary::ELF, output: String, ?entry: Integer, ?debug: bool, ?executable: bool, ?shared: bool) -> String
3+
def initialize: (elf_obj: Caotral::Binary::ELF, output: String, ?entry: Integer, ?debug: bool, ?executable: bool, ?shared: bool) -> void
44
def write: () -> String
55
end

0 commit comments

Comments
 (0)