Module: ELFShim

Defined in:
brew/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 'brew/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 'brew/Library/Homebrew/os/linux/elf.rb', line 67

def binary_executable?
  elf_type == :executable
end

#dylib?Boolean

Returns:

  • (Boolean)


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

def dylib?
  elf_type == :dylib
end

#dylib_idObject



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

def dylib_id
  .dylib_id
end

#dynamic_elf?Boolean

Returns:

  • (Boolean)


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

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



163
164
165
# File 'brew/Library/Homebrew/os/linux/elf.rb', line 163

def dynamically_linked_libraries(*)
  .dylibs
end

#elf?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'brew/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 'brew/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



155
156
157
# File 'brew/Library/Homebrew/os/linux/elf.rb', line 155

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

#read_uint16(offset) ⇒ Object



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

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

#read_uint8(offset) ⇒ Object



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

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