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

patchelf: Optional logging, exception API #13

Merged
merged 2 commits into from
Feb 3, 2020
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
9 changes: 9 additions & 0 deletions lib/patchelf/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# encoding: ascii-8bit
# frozen_string_literal: true

require 'elftools/exceptions'

module PatchELF
# Raised on an error during ELF modification.
class PatchError < ELFTools::ELFError ; end
end
20 changes: 16 additions & 4 deletions lib/patchelf/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require 'elftools/elf_file'

require 'patchelf/exceptions'
require 'patchelf/logger'
require 'patchelf/saver'

Expand All @@ -15,11 +16,14 @@ class Patcher
# Instantiate a {Patcher} object.
# @param [String] filename
# Filename of input ELF.
def initialize(filename)
# @param [Boolean] logging
# Whether to use a (stderr-initialized) logger for errors
def initialize(filename, logging: true)
@in_file = filename
@elf = ELFTools::ELFFile.new(File.open(filename))
@set = {}
@rpath_sym = :runpath
@logging = logging
end

# @return [String?]
Expand Down Expand Up @@ -156,9 +160,17 @@ def save(out_file = nil)

private

def log_or_raise(msg)
if @logging
PatchELF::Logger.warn(msg)
else
raise PatchELF::PatchError, msg
end
end

def interpreter_
segment = @elf.segment_by_type(:interp)
return PatchELF::Logger.warn('No interpreter found.') if segment.nil?
return log_or_raise 'No interpreter found.' if segment.nil?

segment.interp_name
end
Expand Down Expand Up @@ -191,14 +203,14 @@ def tag_name_or_log(type, log_msg)
return if segment.nil?

tag = segment.tag_by_type(type)
return PatchELF::Logger.warn(log_msg) if tag.nil?
return log_or_raise log_msg if tag.nil?

tag.name
end

def dynamic_or_log
@elf.segment_by_type(:dynamic).tap do |s|
PatchELF::Logger.warn('DYNAMIC segment not found, might be a statically-linked ELF?') if s.nil?
log_or_raise 'DYNAMIC segment not found, might be a statically-linked ELF?' if s.nil?
end
end
end
Expand Down