Module: ELFShim

Defined in:
Library/Homebrew/os/linux/elf.rb

Overview

Defined Under Namespace

Classes: Metadata

Constant Summary collapse

MAGIC_NUMBER_OFFSET =
0
MAGIC_NUMBER_ASCII =
"\x7fELF"
OS_ABI_OFFSET =
0x07
OS_ABI_SYSTEM_V =
0
OS_ABI_LINUX =
3
TYPE_OFFSET =
0x10
TYPE_EXECUTABLE =
2
TYPE_SHARED =
3
ARCHITECTURE_OFFSET =
0x12
ARCHITECTURE_I386 =
0x3
ARCHITECTURE_POWERPC =
0x14
ARCHITECTURE_ARM =
0x28
ARCHITECTURE_X86_64 =
0x62
ARCHITECTURE_AARCH64 =
0xB7

Instance Method Summary collapse

Instance Method Details

#archObject


40
41
42
43
44
45
46
47
48
49
50
51
# File 'Library/Homebrew/os/linux/elf.rb', line 40

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

Returns:

  • (Boolean)

67
68
69
# File 'Library/Homebrew/os/linux/elf.rb', line 67

def binary_executable?
  elf_type == :executable
end

#dylib?Boolean

Returns:

  • (Boolean)

63
64
65
# File 'Library/Homebrew/os/linux/elf.rb', line 63

def dylib?
  elf_type == :dylib
end

#dylib_idObject


219
220
221
# File 'Library/Homebrew/os/linux/elf.rb', line 219

def dylib_id
  .dylib_id
end

#dynamic_elf?Boolean

Returns:

  • (Boolean)

96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'Library/Homebrew/os/linux/elf.rb', line 96

def dynamic_elf?
  return @dynamic_elf if defined? @dynamic_elf

  @dynamic_elf = if HOMEBREW_PATCHELF_RB
    patchelf_patcher.instance_variable_get(:@elf).segment_by_type(:DYNAMIC).present?
  elsif which "readelf"
    Utils.popen_read("readelf", "-l", to_path).include?(" DYNAMIC ")
  elsif which "file"
    !Utils.popen_read("file", "-L", "-b", to_path)[/dynamic|shared/].nil?
  else
    raise "Please install either readelf (from binutils) or file."
  end
end

#dynamically_linked_librariesObject


223
224
225
# File 'Library/Homebrew/os/linux/elf.rb', line 223

def dynamically_linked_libraries(*)
  .dylibs
end

#elf?Boolean

Returns:

  • (Boolean)

31
32
33
34
35
36
37
38
# File 'Library/Homebrew/os/linux/elf.rb', line 31

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


53
54
55
56
57
58
59
60
61
# File 'Library/Homebrew/os/linux/elf.rb', line 53

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

#metadataObject


215
216
217
# File 'Library/Homebrew/os/linux/elf.rb', line 215

def 
  @metadata ||= Metadata.new(self)
end

#patchelf_patcherObject


205
206
207
208
209
210
211
212
213
# File 'Library/Homebrew/os/linux/elf.rb', line 205

def patchelf_patcher
  return unless HOMEBREW_PATCHELF_RB

  @patchelf_patcher ||= begin
    Homebrew.install_bundler_gems!
    require "patchelf"
    PatchELF::Patcher.new to_s, logging: false
  end
end

#read_uint16(offset) ⇒ Object


27
28
29
# File 'Library/Homebrew/os/linux/elf.rb', line 27

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

#read_uint8(offset) ⇒ Object


23
24
25
# File 'Library/Homebrew/os/linux/elf.rb', line 23

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

#with_interpreter?Boolean

Returns:

  • (Boolean)

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'Library/Homebrew/os/linux/elf.rb', line 71

def with_interpreter?
  return @with_interpreter if defined? @with_interpreter

  @with_interpreter = if binary_executable?
    true
  elsif dylib?
    if HOMEBREW_PATCHELF_RB
      begin
        patchelf_patcher.interpreter.present?
      rescue PatchELF::PatchError => e
        opoo e unless e.to_s.start_with? "No interpreter found"
        false
      end
    elsif which "readelf"
      Utils.popen_read("readelf", "-l", to_path).include?(" INTERP ")
    elsif which "file"
      Utils.popen_read("file", "-L", "-b", to_path).include?(" interpreter ")
    else
      raise "Please install either readelf (from binutils) or file."
    end
  else
    false
  end
end