New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement linkage for Linux #3517
Conversation
Library/Homebrew/os/linux/elf.rb
Outdated
return @elf = false unless read(4) == "\x7fELF" | ||
# Check that the ELF file is for Linux. | ||
@elf = case read(1, 7).unpack("C")[0] | ||
when 0, 3 then true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above two lines aren't particularly obvious in what they are doing. Can you add a comment and then we can hopefully figure out how to make them more readable?
Library/Homebrew/os/linux/elf.rb
Outdated
def elf? | ||
return @elf if defined? @elf | ||
# See: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header | ||
return @elf = false unless read(4) == "\x7fELF" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say pretty much every number in this file should be a constant explaining what it means otherwise they are a bit "magic".
Library/Homebrew/os/linux/elf.rb
Outdated
elsif which "file" | ||
!Utils.popen_read("file", "-L", "-b", to_path)[/dynamic|shared/].nil? | ||
else | ||
raise StandardError, "Neither `readelf` nor `file` is available "\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is StandardError
not the default for raise? I can't remember. Also, make this message no more than 80 characters or use
<<~EOS`
Library/Homebrew/os/linux/elf.rb
Outdated
end | ||
|
||
class Metadata | ||
LDD_RX = /\t.* => (.*) \(.*\)|\t(.*) => not found/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't know what LDD_RX
means
Library/Homebrew/os/linux/elf.rb
Outdated
@dylib_id, needed = needed_libraries path | ||
return if needed.empty? | ||
# ldd requires that the file be executable, and all ELF files should be executable. | ||
FileUtils.chmod "+x", path unless path.executable? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A Metadata.new
shouldn't be modifying files.
Library/Homebrew/os/linux/elf.rb
Outdated
lines.each do |s| | ||
case s | ||
when /\(SONAME\)/ | ||
soname = s[/\[(.*)\]/, 1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regex is repeated so consider making it a constant
Library/Homebrew/os/linux/elf.rb
Outdated
raise ErrorDuringExecution, command unless $CHILD_STATUS.success? | ||
lines.each do |s| | ||
case s | ||
when /\(SONAME\)/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't need to be a regex.
Library/Homebrew/os/linux/elf.rb
Outdated
case s | ||
when /\(SONAME\)/ | ||
soname = s[/\[(.*)\]/, 1] | ||
when /\(NEEDED\)/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't need to be a regex.
Library/Homebrew/os/linux/elf.rb
Outdated
metadata.dylib_id | ||
end | ||
|
||
def dynamically_linked_libraries(**_options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can probably just make this (*)
40668f6
to
1af0516
Compare
Thanks for the feedback, Mike. I've addressed your comments. |
69baaf6
to
4cfe957
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking much better! A few more nits, thanks.
Library/Homebrew/os/linux/elf.rb
Outdated
@@ -0,0 +1,151 @@ | |||
module ELFShim | |||
# See: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header | |||
MAGIC_OFFSET = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MAGIC_NUMBER_OFFSET
Library/Homebrew/os/linux/elf.rb
Outdated
module ELFShim | ||
# See: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header | ||
MAGIC_OFFSET = 0 | ||
MAGIC_SIZE = 4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use MAGIC_NUMBER.size
Library/Homebrew/os/linux/elf.rb
Outdated
MAGIC_SIZE = 4 | ||
MAGIC_NUMBER = "\x7fELF".freeze | ||
|
||
OSABI_OFFSET = 0x07 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OS_ABI_OFFSET
Library/Homebrew/os/linux/elf.rb
Outdated
MAGIC_NUMBER = "\x7fELF".freeze | ||
|
||
OSABI_OFFSET = 0x07 | ||
OSABI_SIZE = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use OSABI_LINUX.to_s.size
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a read_uint8
helper function.
Library/Homebrew/os/linux/elf.rb
Outdated
OSABI_LINUX = 3 | ||
|
||
TYPE_OFFSET = 0x10 | ||
TYPE_SIZE = 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use TYPE_EXECUTABLE.to_s.size
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are not equivalent.
TYPE_EXECUTABLE.to_s.size
≠ TYPE_SIZE
TYPE_EXECUTABLE.to_s.size
= 1
TYPE_SIZE
= 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a read_uint16
helper function.
Library/Homebrew/os/linux/elf.rb
Outdated
return @elf = false unless read(MAGIC_SIZE, MAGIC_OFFSET) == MAGIC_NUMBER | ||
# Check that this ELF file is for Linux or System V. | ||
# OSABI is often set to 0 (System V), regardless of the target platform. | ||
osabi = read(OSABI_SIZE, OSABI_OFFSET).unpack("C")[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.first
rather than [0]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
os_abi
Library/Homebrew/os/linux/elf.rb
Outdated
|
||
def arch | ||
return :dunno unless elf? | ||
@arch ||= case read(MACHINE_SIZE, MACHINE_OFFSET).unpack("v")[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.first
rather than [0]
Library/Homebrew/os/linux/elf.rb
Outdated
# Check that this ELF file is for Linux or System V. | ||
# OSABI is often set to 0 (System V), regardless of the target platform. | ||
osabi = read(OSABI_SIZE, OSABI_OFFSET).unpack("C")[0] | ||
@elf = osabi == OSABI_LINUX || osabi == OSABI_SYSTEM_V |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[OSABI_SYSTEM_V, OSABI_LINUX].include?(osabi)
Library/Homebrew/os/linux/elf.rb
Outdated
|
||
def elf_type | ||
return :dunno unless elf? | ||
@elf_type ||= case read(TYPE_SIZE, TYPE_OFFSET).unpack("v")[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.first
rather than [0]
@path = path | ||
@dylibs = [] | ||
@dylib_id, needed = needed_libraries path | ||
return if needed.empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stick a newline after each return
; makes this easier to follow/scan (and consider doing this elsewhere in the file).
Fixed. Thanks again for the feedback, Mike. |
Thanks again @sjackman! |
@sjackman Merging this because there's been a lot of back and forth but ideally we'd get some tests to cover a bit more of the diff, here. Can do that in follow-up PR. |
It looks as though Codecov doesn't count lines-of-code as covered that are covered by tests that run only on Linux. Is that the case? |
Thanks for merging, Mike! |
@sjackman Correct. If we can figure out a way to upload and combine the coverage that would be good. @reitermarkus looked at this before I think; any thoughts Markus? |
extend/pathname: Add os/linux/elf.rb
brew tests
with your changes locally?