Module: ELFShim Private

Extended by:
T::Helpers
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

#archSymbol

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:



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'os/linux/elf.rb', line 80

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 :ppc32
  when ARCHITECTURE_POWERPC64 then :ppc64
  when ARCHITECTURE_ARM then :arm
  when ARCHITECTURE_AARCH64 then :arm64
  else :dunno
  end
end

#arch_compatible?(wanted_arch) ⇒ 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)


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

def arch_compatible?(wanted_arch)
  return true unless elf?

  # Treat ppc64le and ppc64 the same
  wanted_arch = :ppc64 if wanted_arch == :ppc64le

  wanted_arch == arch
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)


118
119
120
# File 'os/linux/elf.rb', line 118

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)


114
115
116
# File 'os/linux/elf.rb', line 114

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.



257
258
259
# File 'os/linux/elf.rb', line 257

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)


147
148
149
# File 'os/linux/elf.rb', line 147

def 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.



261
262
263
# File 'os/linux/elf.rb', line 261

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)


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

def elf?
  return @elf unless @elf.nil?

  return @elf = false if 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_typeSymbol

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:



104
105
106
107
108
109
110
111
112
# File 'os/linux/elf.rb', line 104

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

#initialize(*args) ⇒ 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.



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

def initialize(*args)
  @elf = T.let(nil, T.nilable(T::Boolean))
  @arch = T.let(nil, T.nilable(Symbol))
  @elf_type = T.let(nil, T.nilable(Symbol))
  @rpath = T.let(nil, T.nilable(String))
  @interpreter = T.let(nil, T.nilable(String))
  @dynamic_elf = T.let(nil, T.nilable(T::Boolean))
  @metadata = T.let(nil, T.nilable(Metadata))
  @patchelf_patcher = nil

  super
end

#interpreterString?

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:



136
137
138
# File 'os/linux/elf.rb', line 136

def 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.



140
141
142
143
144
# File 'os/linux/elf.rb', line 140

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

  save_using_patchelf_rb interpreter, rpath
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.



246
247
248
249
# File 'os/linux/elf.rb', line 246

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.



64
65
66
# File 'os/linux/elf.rb', line 64

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.



60
61
62
# File 'os/linux/elf.rb', line 60

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

#rpathString?

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.

The runtime search path, such as: "/lib:/usr/lib:/usr/local/lib"

Returns:



125
126
127
# File 'os/linux/elf.rb', line 125

def 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.



242
243
244
# File 'os/linux/elf.rb', line 242

def rpath_using_patchelf_rb
  patchelf_patcher.runpath || patchelf_patcher.rpath
end

#rpathsObject

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.

An array of runtime search path entries, such as: ["/lib", "/usr/lib", "/usr/local/lib"]



131
132
133
# File 'os/linux/elf.rb', line 131

def rpaths
  Array(rpath&.split(":"))
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.



235
236
237
238
239
240
# File 'os/linux/elf.rb', line 235

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