Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/macho.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "English"
require_relative "macho/structure"
require_relative "macho/view"
require_relative "macho/headers"
Expand Down Expand Up @@ -39,4 +40,22 @@ def self.open(filename)

file
end

# Signs the dylib using an ad-hoc identity.
# Necessary after making any changes to a dylib, since otherwise
# changing a signed file invalidates its signature.
# @param filename [String] the file being opened
# @return [void]
# @raise [ModificationError] if the operation fails
def self.codesign!(filename)
# codesign binary is not available on Linux
return if RUBY_PLATFORM !~ /darwin/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think it might make sense to raise an UnsupportedError or similar here instead of silently returning. I can take care of that this afternoon, if that sounds good to others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking of having that be rescued in each of the Tools methods?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I guess it would have to be, to avoid breaking those methods on non-macOS platforms 😞

Hmm, that itself isn't ideal. I'm fine with leaving it as is for now 😅

raise ArgumentError, "#{filename}: no such file" unless File.file?(filename)

system("codesign", "--sign", "-", "--force",
"--preserve-metadata=entitlements,requirements,flags,runtime",
filename)

raise ModificationError, "#{filename}: signing failed!" unless $CHILD_STATUS.success?
end
end
12 changes: 12 additions & 0 deletions lib/macho/tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def self.change_dylib_id(filename, new_id, options = {})

file.change_dylib_id(new_id, options)
file.write!

MachO.codesign!(filename)
end

# Changes a shared library install name in a Mach-O or Fat binary,
Expand All @@ -41,6 +43,8 @@ def self.change_install_name(filename, old_name, new_name, options = {})

file.change_install_name(old_name, new_name, options)
file.write!

MachO.codesign!(filename)
end

# Changes a runtime path in a Mach-O or Fat binary, overwriting the source
Expand All @@ -57,6 +61,8 @@ def self.change_rpath(filename, old_path, new_path, options = {})

file.change_rpath(old_path, new_path, options)
file.write!

MachO.codesign!(filename)
end

# Add a runtime path to a Mach-O or Fat binary, overwriting the source file.
Expand All @@ -71,6 +77,8 @@ def self.add_rpath(filename, new_path, options = {})

file.add_rpath(new_path, options)
file.write!

MachO.codesign!(filename)
end

# Delete a runtime path from a Mach-O or Fat binary, overwriting the source
Expand All @@ -86,6 +94,8 @@ def self.delete_rpath(filename, old_path, options = {})

file.delete_rpath(old_path, options)
file.write!

MachO.codesign!(filename)
end

# Merge multiple Mach-Os into one universal (Fat) binary.
Expand All @@ -106,6 +116,8 @@ def self.merge_machos(filename, *files, fat64: false)

fat_macho = MachO::FatFile.new_from_machos(*machos, :fat64 => fat64)
fat_macho.write(filename)

MachO.codesign!(filename)
end
end
end