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:



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

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.

Parameters:

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
# File 'os/linux/elf.rb', line 97

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)


123
124
125
# File 'os/linux/elf.rb', line 123

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)


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

def dylib?
  elf_type == :dylib
end

#dylib_idString?

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:



291
292
293
# File 'os/linux/elf.rb', line 291

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)


154
155
156
# File 'os/linux/elf.rb', line 154

def dynamic_elf?
  .dynamic_elf?
end

#dynamically_linked_libraries(except: :none, resolve_variable_references: true) ⇒ Array<String>

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.

Parameters:

  • except (Symbol) (defaults to: :none)
  • resolve_variable_references (Boolean) (defaults to: true)

Returns:



296
297
298
# File 'os/linux/elf.rb', line 296

def dynamically_linked_libraries(except: :none, resolve_variable_references: true)
  .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)


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

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:



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

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(path) ⇒ void

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.

Parameters:

  • path (T.anything)


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

def initialize(path)
  @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))

  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:



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

def interpreter
  .interpreter
end

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

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.

This method returns an undefined value.

Parameters:

  • interpreter (String, nil) (defaults to: nil)
  • rpath (String, nil) (defaults to: nil)


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

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

  save_using_patchelf_rb interpreter, rpath
end

#patchelf_patcher::PatchELF::Patcher

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.

Don't cache the patcher; it keeps the ELF file open so long as it is alive. Instead, for read-only access to the ELF file's metadata, fetch it and cache it with Metadata.

Returns:

  • (::PatchELF::Patcher)


279
280
281
282
# File 'os/linux/elf.rb', line 279

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

#read_uint16(offset) ⇒ Integer

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.

Parameters:

  • offset (Integer)

Returns:

  • (Integer)


66
67
68
# File 'os/linux/elf.rb', line 66

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

#read_uint8(offset) ⇒ Integer

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.

Parameters:

  • offset (Integer)

Returns:

  • (Integer)


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

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:



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

def rpath
  .rpath
end

#rpathsArray<String>

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"]

Returns:



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

def rpaths
  Array(rpath&.split(":"))
end

#save_using_patchelf_rb(new_interpreter, new_rpath) ⇒ void

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.

This method returns an undefined value.

Parameters:



268
269
270
271
272
273
# File 'os/linux/elf.rb', line 268

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

#section_namesArray<String>

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:



159
160
161
# File 'os/linux/elf.rb', line 159

def section_names
  .section_names
end