Skip to content
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

Improved stringification for load commands #447

Merged
merged 4 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
70 changes: 70 additions & 0 deletions lib/macho/load_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ def uuid_string
segs.join("-")
end

# @return [String] an alias for uuid_string
def to_s
uuid_string
end

# @return [Hash] returns a hash representation of this {UUIDCommand}
def to_h
{
Expand Down Expand Up @@ -527,6 +532,11 @@ def guess_align
align
end

# @return [String] a string representation of the segment name
def to_s
segname
end

# @return [Hash] a hash representation of this {SegmentCommand}
def to_h
{
Expand Down Expand Up @@ -605,6 +615,11 @@ def serialize(context)
compatibility_version].pack(format) + string_payload
end

# @return [String] a string representation of the library's pathname
def to_s
name.to_s
end

# @return [Hash] a hash representation of this {DylibCommand}
def to_h
{
Expand Down Expand Up @@ -651,6 +666,11 @@ def serialize(context)
[cmd, cmdsize, string_offsets[:name]].pack(format) + string_payload
end

# @return [String] a string representation of the dynamic linker's pathname
def to_s
name.to_s
end

# @return [Hash] a hash representation of this {DylinkerCommand}
def to_h
{
Expand Down Expand Up @@ -688,6 +708,11 @@ def initialize(view, cmd, cmdsize, name, nmodules, linked_modules)
@linked_modules = linked_modules
end

# @return [String] a string representation of the library's pathname
def to_s
name.to_s
end

# @return [Hash] a hash representation of this {PreboundDylibCommand}
def to_h
{
Expand Down Expand Up @@ -810,6 +835,11 @@ def initialize(view, cmd, cmdsize, umbrella)
@umbrella = LCStr.new(self, umbrella)
end

# @return [String] a string represenation of the umbrella framework name
def to_s
umbrella.to_s
end

# @return [Hash] a hash representation of this {SubFrameworkCommand}
def to_h
{
Expand Down Expand Up @@ -838,6 +868,11 @@ def initialize(view, cmd, cmdsize, sub_umbrella)
@sub_umbrella = LCStr.new(self, sub_umbrella)
end

# @return [String] a string represenation of the sub-umbrella framework name
def to_s
sub_umbrella.to_s
end

# @return [Hash] a hash representation of this {SubUmbrellaCommand}
def to_h
{
Expand Down Expand Up @@ -866,6 +901,11 @@ def initialize(view, cmd, cmdsize, sub_library)
@sub_library = LCStr.new(self, sub_library)
end

# @return [String] a string represenation of the sub-library name
def to_s
sublibrary.to_s
end

# @return [Hash] a hash representation of this {SubLibraryCommand}
def to_h
{
Expand Down Expand Up @@ -894,6 +934,11 @@ def initialize(view, cmd, cmdsize, sub_client)
@sub_client = LCStr.new(self, sub_client)
end

# @return [String] a string represenation of the sub-client name
def to_s
sub_client.to_s
end

# @return [Hash] a hash representation of this {SubClientCommand}
def to_h
{
Expand Down Expand Up @@ -1205,6 +1250,11 @@ def serialize(context)
[cmd, cmdsize, string_offsets[:path]].pack(format) + string_payload
end

# @return [String] a string representation of the run path
def to_s
path.to_s
end

# @return [Hash] a hash representation of this {RpathCommand}
def to_h
{
Expand Down Expand Up @@ -1652,6 +1702,11 @@ def version_string
segs.join(".")
end

# @return [String] an alias for version_string
def to_s
version_string
end

# @return [Hash] a hash representation of this {SourceVersionCommand}
def to_h
{
Expand Down Expand Up @@ -1730,6 +1785,11 @@ def initialize(view, cmd, cmdsize, name, header_addr)
@header_addr = header_addr
end

# @return [String] a string representation of the pathname
def to_s
name.to_s
end

# @return [Hash] a hash representation of this {FvmfileCommand}
def to_h
{
Expand Down Expand Up @@ -1766,6 +1826,11 @@ def initialize(view, cmd, cmdsize, name, minor_version, header_addr)
@header_addr = header_addr
end

# @return [String] a string representation of the target pathname
def to_s
name.to_s
end

# @return [Hash] a hash representation of this {FvmlibCommand}
def to_h
{
Expand Down Expand Up @@ -1803,6 +1868,11 @@ def initialize(view, cmd, cmdsize, data_owner, offset, size)
@size = size
end

# @return [String] a string representation of data owner of this note
def to_s
data_owner
end

# @return [Hash] a hash representation of this {NoteCommand}
def to_h
{
Expand Down
2 changes: 2 additions & 0 deletions test/test_create_load_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_create_dylib_commands
assert lc.name
assert_kind_of MachO::LoadCommands::LoadCommand::LCStr, lc.name
assert_equal "test", lc.name.to_s
assert_equal lc.name.to_s, lc.to_s
assert_equal 0, lc.timestamp
assert_equal 0, lc.current_version
assert_equal 0, lc.compatibility_version
Expand All @@ -43,5 +44,6 @@ def test_create_rpath_command
assert lc.path
assert_kind_of MachO::LoadCommands::LoadCommand::LCStr, lc.path
assert_equal "test", lc.path.to_s
assert_equal lc.path.to_s, lc.to_s
end
end
9 changes: 5 additions & 4 deletions test/test_macho.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def test_segments_and_sections
assert_kind_of MachO::LoadCommands::SegmentCommand, seg if file.magic32?
assert_kind_of MachO::LoadCommands::SegmentCommand64, seg if file.magic64?
assert_kind_of String, seg.segname
assert_equal seg.segname, seg.to_s
assert_kind_of Integer, seg.vmaddr
assert_kind_of Integer, seg.vmsize
assert_kind_of Integer, seg.fileoff
Expand All @@ -121,7 +122,7 @@ def test_segments_and_sections
assert_kind_of Integer, seg.nsects
assert_kind_of Integer, seg.flags
refute seg.flag?(:THIS_IS_A_MADE_UP_FLAG)
assert MachO::LoadCommands::SEGMENT_FLAGS.keys.one? { |sf| seg.flag?(sf) } if seg.flags != 0
assert(MachO::LoadCommands::SEGMENT_FLAGS.keys.one? { |sf| seg.flag?(sf) }) if seg.flags != 0

sections = seg.sections

Expand All @@ -144,9 +145,9 @@ def test_segments_and_sections
refute sect.flag?(:THIS_IS_A_MADE_UP_FLAG)
assert_kind_of Integer, sect.type
assert MachO::Sections::SECTION_TYPES.values.include?(sect.type)
assert MachO::Sections::SECTION_TYPES.keys.one? { |st| sect.type?(st) }
assert(MachO::Sections::SECTION_TYPES.keys.one? { |st| sect.type?(st) })
assert_kind_of Integer, sect.attributes
assert MachO::Sections::SECTION_ATTRIBUTES.keys.any? { |sa| sect.attribute?(sa) }
assert(MachO::Sections::SECTION_ATTRIBUTES.keys.any? { |sa| sect.attribute?(sa) })
assert_kind_of Integer, sect.reserved1
assert_kind_of Integer, sect.reserved2
assert_kind_of Integer, sect.reserved3 if sect.is_a? MachO::Sections::Section64
Expand Down Expand Up @@ -592,7 +593,7 @@ def test_rpath_exceptions
end

def test_fail_loading_fat
filename = fixture(['i386', 'x86_64'], 'libhello.dylib')
filename = fixture(%w[i386 x86_64], "libhello.dylib")

ex = assert_raises(MachO::FatBinaryError) do
MachO::MachOFile.new_from_bin File.read(filename)
Expand Down