Module: ELFShim Private

Included in:
Pathname
Defined in:
os/linux/elf.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Pathname extension for dealing with ELF files.

Instance Method Summary collapse

Instance Method Details

#archObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'os/linux/elf.rb', line 58

def arch
  return :dunno unless elf?

  @arch ||= case read_uint16(ARCHITECTURE_OFFSET)
  when ARCHITECTURE_I386 then :i386
  when ARCHITECTURE_X86_64 then :x86_64
  when ARCHITECTURE_POWERPC then :powerpc
  when ARCHITECTURE_ARM then :arm
  when ARCHITECTURE_AARCH64 then :arm64
  else :dunno
  end
end

#binary_executable?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


85
86
87
# File 'os/linux/elf.rb', line 85

def binary_executable?
  elf_type == :executable
end

#dylib?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


81
82
83
# File 'os/linux/elf.rb', line 81

def dylib?
  elf_type == :dylib
end

#dylib_idObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



192
193
194
# File 'os/linux/elf.rb', line 192

def dylib_id
  .dylib_id
end

#dynamic_elf?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


111
112
113
114
115
# File 'os/linux/elf.rb', line 111

def dynamic_elf?
  return @dynamic_elf if defined? @dynamic_elf

  @dynamic_elf = patchelf_patcher.elf.segment_by_type(:DYNAMIC).present?
end

#dynamically_linked_librariesObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



196
197
198
# File 'os/linux/elf.rb', line 196

def dynamically_linked_libraries(*)
  .dylibs
end

#elf?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
# File 'os/linux/elf.rb', line 49

def elf?
  return @elf if defined? @elf
  return @elf = false unless read(MAGIC_NUMBER_ASCII.size, MAGIC_NUMBER_OFFSET) == MAGIC_NUMBER_ASCII

  # Check that this ELF file is for Linux or System V.
  # OS_ABI is often set to 0 (System V), regardless of the target platform.
  @elf = [OS_ABI_LINUX, OS_ABI_SYSTEM_V].include? read_uint8(OS_ABI_OFFSET)
end

#elf_typeObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



71
72
73
74
75
76
77
78
79
# File 'os/linux/elf.rb', line 71

def elf_type
  return :dunno unless elf?

  @elf_type ||= case read_uint16(TYPE_OFFSET)
  when TYPE_EXECUTABLE then :executable
  when TYPE_SHARED then :dylib
  else :dunno
  end
end

#interpreterObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



95
96
97
98
99
# File 'os/linux/elf.rb', line 95

def interpreter
  return @interpreter if defined? @interpreter

  @interpreter = patchelf_patcher.interpreter
end

#patch!(interpreter: nil, rpath: nil) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



101
102
103
104
105
106
107
108
109
# File 'os/linux/elf.rb', line 101

def patch!(interpreter: nil, rpath: nil)
  return if interpreter.blank? && rpath.blank?

  if HOMEBREW_PATCHELF_RB_WRITE
    save_using_patchelf_rb interpreter, rpath
  else
    save_using_patchelf interpreter, rpath
  end
end

#patchelf_patcherObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



182
183
184
185
# File 'os/linux/elf.rb', line 182

def patchelf_patcher
  require "patchelf"
  @patchelf_patcher ||= ::PatchELF::Patcher.new to_s, on_error: :silent
end

#read_uint16(offset) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



45
46
47
# File 'os/linux/elf.rb', line 45

def read_uint16(offset)
  read(2, offset).unpack1("v")
end

#read_uint8(offset) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



41
42
43
# File 'os/linux/elf.rb', line 41

def read_uint8(offset)
  read(1, offset).unpack1("C")
end

#rpathObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



89
90
91
92
93
# File 'os/linux/elf.rb', line 89

def rpath
  return @rpath if defined? @rpath

  @rpath = rpath_using_patchelf_rb
end

#rpath_using_patchelf_rbObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



178
179
180
# File 'os/linux/elf.rb', line 178

def rpath_using_patchelf_rb
  patchelf_patcher.runpath || patchelf_patcher.rpath
end

#save_using_patchelf(new_interpreter, new_rpath) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



161
162
163
164
165
166
167
168
169
# File 'os/linux/elf.rb', line 161

def save_using_patchelf(new_interpreter, new_rpath)
  patchelf = DevelopmentTools.locate "patchelf"
  odie "Could not locate `patchelf`; please run `brew install patchelf`" if patchelf.blank?
  args = []
  args << "--set-interpreter" << new_interpreter if new_interpreter.present?
  args << "--force-rpath" << "--set-rpath" << new_rpath if new_rpath.present?

  Homebrew.safe_system(patchelf, *args, to_s)
end

#save_using_patchelf_rb(new_interpreter, new_rpath) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



171
172
173
174
175
176
# File 'os/linux/elf.rb', line 171

def save_using_patchelf_rb(new_interpreter, new_rpath)
  patcher = patchelf_patcher
  patcher.interpreter = new_interpreter if new_interpreter.present?
  patcher.rpath = new_rpath if new_rpath.present?
  patcher.save(patchelf_compatible: true)
end