-
Notifications
You must be signed in to change notification settings - Fork 0
Support dlopen in ruby #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
242eaa8
c93eef4
1b63bf7
02f81f4
046a0f6
ae3df61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ class Linker | |
| class Builder | ||
| include Caotral::Binary::ELF::Utils | ||
| REL_TYPES = Caotral::Binary::ELF::Section::Rel::TYPES | ||
| DYNAMIC_TAGS = Caotral::Binary::ELF::Section::Dynamic::TAG_TYPES | ||
| SYMTAB_BIND = { locals: 0, globals: 1, weaks: 2, }.freeze | ||
| BIND_BY_VALUE = SYMTAB_BIND.invert.freeze | ||
| RELOCATION_SECTION_NAMES = [".rela.text", ".rel.text", ".rela.data", ".rel.data"].freeze | ||
|
|
@@ -18,6 +19,11 @@ class Builder | |
| REL_TYPES[:AMD64_GOTPCRELX], | ||
| REL_TYPES[:AMD64_REX_GOTPCRELX], | ||
| ].freeze | ||
| REJECT_DYNAMIC_TAGS = [ | ||
| DYNAMIC_TAGS[:PLTRELSZ], | ||
| DYNAMIC_TAGS[:PLTREL], | ||
| DYNAMIC_TAGS[:JMPREL], | ||
| ].freeze | ||
|
|
||
| attr_reader :symbols | ||
|
|
||
|
|
@@ -176,22 +182,22 @@ def build | |
| first_insertion = got_plt_offsets[sym].nil? | ||
| got_plt_offsets[sym] ||= got_plt_offset.tap { got_plt_offset += 8 } | ||
| if dynamic? && undefined && first_insertion | ||
| got_plt_section.body << [0].pack("Q<") | ||
| rps = Caotral::Binary::ELF::Section::Rel.new.set!( | ||
| offset: got_plt_offsets[sym], | ||
| info: ((sym) << 32) | REL_TYPES[:AMD64_JUMP_SLOT] | ||
| got_plt_section.body << [0].pack("Q<") | ||
| rps = Caotral::Binary::ELF::Section::Rel.new.set!( | ||
| offset: got_plt_offsets[sym], | ||
| info: ((sym) << 32) | REL_TYPES[:AMD64_JUMP_SLOT] | ||
| ) | ||
| name = symtab_section.body[sym].name_string | ||
| dynstr_index = dynstr.body.offset_of(name) | ||
| if dynstr_index.nil? | ||
| dynstr.body.names += name + "\0" | ||
| dynsym.body << Caotral::Binary::ELF::Section::Symtab.new.set!( | ||
| name: dynstr.body.offset_of(name), | ||
| info: (1 << 4) | 2, | ||
| ) | ||
| name = symtab_section.body[sym].name_string | ||
| dynstr_index = dynstr.body.offset_of(name) | ||
| if dynstr_index.nil? | ||
| dynstr.body.names += name + "\0" | ||
| dynsym.body << Caotral::Binary::ELF::Section::Symtab.new.set!( | ||
| name: dynstr.body.offset_of(name), | ||
| info: (1 << 4) | 2, | ||
| ) | ||
| end | ||
| rela_plt_section.body << rps | ||
| next | ||
| end | ||
| rela_plt_section.body << rps | ||
| next | ||
| end | ||
| elsif UNSUPPORTED_REL_TYPES.include?(rel.type) | ||
| raise Caotral::Binary::ELF::Error, "unsupported relocation type: #{rel.type_name}" | ||
|
|
@@ -260,13 +266,40 @@ def build | |
| if dynamic? | ||
| sections << dynstr | ||
| sections << dynsym | ||
| sections << build_hash_section if @pie | ||
| hash_section = build_hash_section | ||
| sections << hash_section | ||
| sections << rela_dyn_section | ||
| sections << rela_plt_section | ||
| sym = sections.index(dynsym) | ||
| rela_dyn_section.header.set!(link: sym, type: rel_type(rela_dyn_section), entsize: rel_entsize(rela_dyn_section)) | ||
| rela_plt_section.header.set!(link: sym, type: rel_type(rela_plt_section), info: ref_index(sections, got_plt_section.section_name)) | ||
| sections << build_dynamic_section | ||
| symtab_section.body.each do |sym| | ||
| next unless [SYMTAB_BIND[:globals], SYMTAB_BIND[:weaks]].include?(sym.bind) | ||
| next if sym.shndx == 0 | ||
| copy_sym = sym.dup | ||
| shndx = copy_sym.shndx | ||
| name = dynstr.body.offset_of(sym.name_string) | ||
| if name.nil? | ||
| dynstr.body.names += copy_sym.name_string + "\0" | ||
| name = dynstr.body.offset_of(copy_sym.name_string) | ||
| end | ||
| copy_sym.name_string = sym.name_string | ||
| dynsym.body << copy_sym.set!(name:, shndx:, value: sym.value) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Defined symbols copied into Useful? React with 👍 / 👎. |
||
| end | ||
| hash = Caotral::Binary::ELF::Section::Hash.new(nchain: dynsym.body.size) | ||
| hash.bucket[0] = num2bytes(1, 4) if dynsym.body.size > 1 | ||
| dynsym.body.each_with_index do |sym, i| | ||
| next if i == 0 | ||
| hash.chain[i] = num2bytes(0, 4) | ||
| end | ||
|
Comment on lines
+290
to
+294
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new Useful? React with 👍 / 👎. |
||
| hash_section.body = hash | ||
| dynamic_section = build_dynamic_section | ||
| if rela_plt_section.body.size == 0 && dynamic? | ||
| bodies = dynamic_section.body.reject { |ent| REJECT_DYNAMIC_TAGS.include?(ent.tag) } | ||
| dynamic_section.body = bodies | ||
| end | ||
|
|
||
| sections << dynamic_section | ||
| end | ||
| sections << symtab_section | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| int add(int x, int y) { | ||
| return x + y; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| require "fiddle/import" | ||
|
|
||
| module X | ||
| extend Fiddle::Importer | ||
| dlload "./libtmp.so" | ||
| extern "int add(int, int)" | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| require_relative "../../test_suite" | ||
|
|
||
| class Caotral::Linker::FiddleMethodTest < Test::Unit::TestCase | ||
| include TestProcessHelper | ||
| def setup | ||
| @generated = [] | ||
| omit("Ruby::Box is not supported in this environment") unless supported_ruby_box? | ||
| end | ||
|
|
||
| def teardown | ||
| @generated.each do |file| | ||
| File.delete(file) if File.exist?(file) | ||
| end | ||
| end | ||
|
|
||
| def test_sample_call_add_method | ||
| @generated = ["libtmp.so", "libtmp.so.o"] | ||
| @file = "sample/C/add.c" | ||
| IO.popen(["gcc", "-fPIC", "-c", "-o", "libtmp.so.o", "%s" % @file]).close | ||
| Caotral::Linker.link!(inputs: ["libtmp.so.o"], output: "libtmp.so", linker: "self", shared: true, executable: false) | ||
| elf = Caotral::Binary::ELF::Reader.read!(input: "./libtmp.so") | ||
| box = Ruby::Box.new | ||
| box.require("./sample/fiddle_add.rb") | ||
| assert_equal(10, box::X.add(3, 7)) | ||
| dynsym = elf.find_by_name(".dynsym") | ||
| rela_plt = elf.find_by_name(".rela.plt") | ||
| dynamic = elf.find_by_name(".dynamic") | ||
| dynstr = elf.find_by_name(".dynstr") | ||
| dynstrs = dynstr.body.names.split("\x00") | ||
| assert(dynstrs.include?("add")) | ||
| assert_equal(2, dynsym.body.size) | ||
| assert_equal("add", dynstr.body.lookup(dynsym.body[1].name_offset)) | ||
| assert_equal(0, rela_plt.body.size) | ||
| assert_equal(nil, dynamic.body.find { |dt| dt.plt_rel? }) | ||
| assert_equal(nil, dynamic.body.find { |dt| dt.plt_rel_size? }) | ||
| assert_equal(nil, dynamic.body.find { |dt| dt.jmp_rel? }) | ||
| end | ||
|
|
||
| private def supported_ruby_box? = RUBY_VERSION >= "4.0.0" && ENV["RUBY_BOX"] == "1" && defined?(Ruby::Box) | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validate_relocationsnow computesrela_plt_exists = !rela_plt.body.empty?before confirming the section exists, so valid ELF files that have.dynamic/.rela.dynbut no.rela.plt(common when there are no PLT relocations) now raiseNoMethodErrorinstead of being validated. This is a regression from the previous behavior where PLT checks were skipped when.rela.pltwas absent.Useful? React with 👍 / 👎.