-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
elf.rb
227 lines (182 loc) 路 5.92 KB
/
elf.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# typed: true
# frozen_string_literal: true
require "os/linux/ld"
# {Pathname} extension for dealing with ELF files.
# @see https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header
#
# @api private
module ELFShim
MAGIC_NUMBER_OFFSET = 0
private_constant :MAGIC_NUMBER_OFFSET
MAGIC_NUMBER_ASCII = "\x7fELF"
private_constant :MAGIC_NUMBER_ASCII
OS_ABI_OFFSET = 0x07
private_constant :OS_ABI_OFFSET
OS_ABI_SYSTEM_V = 0
private_constant :OS_ABI_SYSTEM_V
OS_ABI_LINUX = 3
private_constant :OS_ABI_LINUX
TYPE_OFFSET = 0x10
private_constant :TYPE_OFFSET
TYPE_EXECUTABLE = 2
private_constant :TYPE_EXECUTABLE
TYPE_SHARED = 3
private_constant :TYPE_SHARED
ARCHITECTURE_OFFSET = 0x12
private_constant :ARCHITECTURE_OFFSET
ARCHITECTURE_I386 = 0x3
private_constant :ARCHITECTURE_I386
ARCHITECTURE_POWERPC = 0x14
private_constant :ARCHITECTURE_POWERPC
ARCHITECTURE_ARM = 0x28
private_constant :ARCHITECTURE_ARM
ARCHITECTURE_X86_64 = 0x3E
private_constant :ARCHITECTURE_X86_64
ARCHITECTURE_AARCH64 = 0xB7
private_constant :ARCHITECTURE_AARCH64
def read_uint8(offset)
read(1, offset).unpack1("C")
end
def read_uint16(offset)
read(2, offset).unpack1("v")
end
def elf?
return @elf if defined? @elf
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
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
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
def dylib?
elf_type == :dylib
end
def binary_executable?
elf_type == :executable
end
# The runtime search path, such as:
# "/lib:/usr/lib:/usr/local/lib"
def rpath
return @rpath if defined? @rpath
@rpath = rpath_using_patchelf_rb
end
# An array of runtime search path entries, such as:
# ["/lib", "/usr/lib", "/usr/local/lib"]
def rpaths
Array(rpath&.split(":"))
end
def interpreter
return @interpreter if defined? @interpreter
@interpreter = patchelf_patcher.interpreter
end
def patch!(interpreter: nil, rpath: nil)
return if interpreter.blank? && rpath.blank?
save_using_patchelf_rb interpreter, rpath
end
def dynamic_elf?
return @dynamic_elf if defined? @dynamic_elf
@dynamic_elf = patchelf_patcher.elf.segment_by_type(:DYNAMIC).present?
end
# Helper class for reading metadata from an ELF file.
#
# @api private
class Metadata
attr_reader :path, :dylib_id, :dylibs
def initialize(path)
@path = path
@dylibs = []
@dylib_id, needed = needed_libraries path
return if needed.empty?
@dylibs = needed.map { |lib| find_full_lib_path(lib).to_s }
end
private
def needed_libraries(path)
return [nil, []] unless path.dynamic_elf?
needed_libraries_using_patchelf_rb path
end
def needed_libraries_using_patchelf_rb(path)
patcher = path.patchelf_patcher
[patcher.soname, patcher.needed]
end
def find_full_lib_path(basename)
local_paths = (path.patchelf_patcher.runpath || path.patchelf_patcher.rpath)&.split(":")
# Search for dependencies in the runpath/rpath first
local_paths&.each do |local_path|
candidate = Pathname(local_path)/basename
return candidate if candidate.exist? && candidate.elf?
end
# Check if DF_1_NODEFLIB is set
dt_flags_1 = path.patchelf_patcher.elf.segment_by_type(:dynamic)&.tag_by_type(:flags_1)
nodeflib_flag = if dt_flags_1.nil?
false
else
dt_flags_1.value & ELFTools::Constants::DF::DF_1_NODEFLIB != 0
end
linker_library_paths = OS::Linux::Ld.library_paths
linker_system_dirs = OS::Linux::Ld.system_dirs
# If DF_1_NODEFLIB is set, exclude any library paths that are subdirectories
# of the system dirs
if nodeflib_flag
linker_library_paths = linker_library_paths.reject do |lib_path|
linker_system_dirs.any? { |system_dir| Utils::Path.child_of? system_dir, lib_path }
end
end
# If not found, search recursively in the paths listed in ld.so.conf (skipping
# paths that are subdirectories of the system dirs if DF_1_NODEFLIB is set)
linker_library_paths.each do |linker_library_path|
candidate = Pathname(linker_library_path)/basename
return candidate if candidate.exist? && candidate.elf?
end
# If not found, search in the system dirs, unless DF_1_NODEFLIB is set
unless nodeflib_flag
linker_system_dirs.each do |linker_system_dir|
candidate = Pathname(linker_system_dir)/basename
return candidate if candidate.exist? && candidate.elf?
end
end
basename
end
end
private_constant :Metadata
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
def rpath_using_patchelf_rb
patchelf_patcher.runpath || patchelf_patcher.rpath
end
def patchelf_patcher
require "patchelf"
@patchelf_patcher ||= ::PatchELF::Patcher.new to_s, on_error: :silent
end
def metadata
@metadata ||= Metadata.new(self)
end
private :metadata
def dylib_id
metadata.dylib_id
end
def dynamically_linked_libraries(*)
metadata.dylibs
end
end