Module: ELFShim

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

Defined Under Namespace

Classes: Metadata

Constant Summary

MAGIC_NUMBER_OFFSET =
0
MAGIC_NUMBER_ASCII =
"\x7fELF".freeze
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



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

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)


65
66
67
# File 'Library/Homebrew/os/linux/elf.rb', line 65

def binary_executable?
  elf_type == :executable
end

#dylib?Boolean

Returns:

  • (Boolean)


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

def dylib?
  elf_type == :dylib
end

#dylib_idObject



153
154
155
# File 'Library/Homebrew/os/linux/elf.rb', line 153

def dylib_id
  .dylib_id
end

#dynamic_elf?Boolean

Returns:

  • (Boolean)


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

def dynamic_elf?
  return @dynamic_elf if defined? @dynamic_elf

  if 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



157
158
159
# File 'Library/Homebrew/os/linux/elf.rb', line 157

def dynamically_linked_libraries(*)
  .dylibs
end

#elf?Boolean

Returns:

  • (Boolean)


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

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



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

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



149
150
151
# File 'Library/Homebrew/os/linux/elf.rb', line 149

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

#read_uint16(offset) ⇒ Object



25
26
27
# File 'Library/Homebrew/os/linux/elf.rb', line 25

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

#read_uint8(offset) ⇒ Object



21
22
23
# File 'Library/Homebrew/os/linux/elf.rb', line 21

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