114 Commits

Author SHA1 Message Date
Michael Cho
f5c11fa342
Check host libstdc++ for brew gcc dependency
For most formulae, the bottles need a minimum libstdc++ rather than a
minimum GCC version. This is particularly important when building on
Ubuntu where the default compiler version is older than libstdc++.

So, checking the host libstdc++ version is a more accurate way to
determine whether brew GCC is needed at runtime. This can be improved in
the future to check symbol versions (e.g. GLIBCXX, CXXABI, GLIBC) which
can allow some bottles to be installed even with older glibc/libstdc++.
2025-09-12 14:15:56 -04:00
Michael Cho
896edb4451
os/linux/ld: add support for using system ld.so 2025-09-05 09:04:21 -04:00
Ruoyu Zhong
c98505baf7
os/linux/glibc: typed: strict 2025-08-21 01:28:16 +08:00
Ruoyu Zhong
26021c9e36
os/linux/elf: bump to typed: strict
Also, clean up an unused method (`elf_parser`) from a previous conflict
resolution.

Co-authored-by: Mike McQuaid <mike@mikemcquaid.com>
2025-08-12 10:25:40 +08:00
Mike McQuaid
97c0bc7d0b
Merge pull request #20408 from Homebrew/fd-leak-fix
os/linux/elf: fix file descriptor leak
2025-08-11 12:10:16 +00:00
Xuehai Pan
b2d621ce6f os/linux/ld: do not crash the program if the ld.so.conf entry is not readable 2025-08-11 19:40:41 +08:00
Ruoyu Zhong
42b332055a
os/linux/elf: fix rpath precedence 2025-08-11 18:28:58 +08:00
Ruoyu Zhong
66737b5e82
os/linux/elf: fix file descriptor leak
On Linux, we occasionally see `EMFILE` ("too many open files") errors
especially when installing a large formula like `llvm`. Currently, this
can be reliably reproduced in a Homebrew/brew GitHub codespace (where
`ulimit -n` seems to be 1024 by default) with `brew install geeqie`,
with the following error message:

    Error: Too many open files @ rb_sysopen - /home/linuxbrew/.linuxbrew/Cellar/llvm/20.1.8/bin/tblgen-lsp-server

The reason is that each instance of `PatchELF::Patcher` keeps the ELF
file open. We prepend the `ELFShim` module to the `Pathname` class and
cache the patcher as an instance variable, which means that the ELF file
remains open so long as the `Pathname` instance is still alive even if
we don't need to access the ELF metadata anymore. When performing
certain checks (e.g., linkage), we also store these `Pathname`
instances, so the number of open file descriptors simply keeps
increasing.

We can fix that by not caching the patcher and only use it when
necessary. We create a patcher instance whenever we need to read or
write ELF metadata, and reading of metadata is consolidated into the
existing `ELFShim::Metadata` class so that we don't repeatedly create
patcher instances.

A fix for a file descriptor leak issue in patchelf.rb has been submitted
at https://github.com/david942j/patchelf.rb/pull/48. Together with that,
this fixes #19177, #19866, #20223, #20302.
2025-08-11 18:01:32 +08:00
Carlo Cabrera
a41de81890
Skip rpath relocation of ELF files with protodesc_cold sections
We have multiple formulae (e.g. `ola`, `openvino`, `opencv`, `or-tools`,
`pytorch`) that seem to be broken by `patchelf.rb` on x86_64 Linux.[^1] The
common thread seems to be the presence of a `protodesc_cold` section in
the ELF header.

Let's avoid breaking these bottles by skipping relocation when a binary
file has a `protodesc_cold` section. It will probably hurt
relocatability of these bottles, but that's better shipping broken
binaries.

[^1]: https://github.com/Homebrew/homebrew-core/pull/210860#issue-2918569212
2025-08-11 03:10:07 +08:00
Mike McQuaid
23971854b0
Fix file descriptor leak in Linux LD library path parsing
The library_paths method was using readlines which could leave file
descriptors open due to Ruby's garbage collection behavior. When
processing many packages during 'brew upgrade' or 'brew linkage',
this caused "Too many open files" errors on Linux systems.

Changes:
- Replace readlines with explicit file.open block to ensure proper closure
- Add caching to avoid repeatedly reading /etc/ld.so.conf during a session
- Cache included files as well to optimize recursive include processing

Fixes: #19866, #20302, #19177, #20223

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-05 15:19:07 +01:00
Mike McQuaid
d57efd9ea2
Improve some Pathname instance variable handling
Will fix or at least partly address:
```
/home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/os/linux/elf.rb:225: warning: The class Pathname reached 8 shape variations, instance variables accesses will be slower and memory usage increased.
35
It is recommended to define instance variables in a consistent order, for instance by eagerly defining them all in the #initialize method.
```
2025-07-11 08:02:27 +00:00
Douglas Eichelberger
d3d25beb35 Use requires_ancestor consistently 2024-10-06 09:25:57 -07:00
Bo Anderson
c76c902dbc
os/linux/elf: fix arch compat check on ppc 2024-10-01 05:32:58 +01:00
Carlo Cabrera
f0513c0d64
Merge pull request #18456 from Homebrew/skip-incompatible-arch 2024-09-30 22:54:50 +08:00
Carlo Cabrera
65731faf1d
os/linux/ld: handle nonexistent ld.so.conf more gracefully
Fixes #18458
2024-09-30 22:33:09 +08:00
Carlo Cabrera
861d7b9087
linkage_checker: skip files with incompatible architectures
Some formulae include these files, and they can't always be removed.
However, they can cause spurious linkage failures, so let's skip them
when checking for linkage. See, for example, faust at
Homebrew/homebrew-core#191308.
2024-09-29 05:15:36 +08:00
Carlo Cabrera
1656d08629
os/linux/ld: drop retry logic
This is a hack, so let's see if we can get away with skipping it for
now.
2024-09-17 11:51:27 +08:00
Carlo Cabrera
70ab4932d3
Add link to issue with context 2024-09-17 01:03:34 +08:00
Carlo Cabrera
bb20b3c720
os/linux/ld: harden brewed_ld_so_diagnostics against TypeError
I think this is a bug in Ruby, but I've no idea how to track it down. I
can reproduce it intermittently in a codespace when `brew install`ing a
large number of formulae.

To work around this:
- cache the return value of `brewed_ld_so_diagnostics` so that we can
  limit the number of calls to `IO.popen`
- retry once when we see a `TypeError`

Closes #17828.
2024-09-16 20:26:35 +08:00
Issy Long
45978435e7
rubocop: Use Sorbet/StrictSigil as it's better than comments
- Previously I thought that comments were fine to discourage people from
  wasting their time trying to bump things that used `undef` that Sorbet
  didn't support. But RuboCop is better at this since it'll complain if
  the comments are unnecessary.

- Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501.

- I've gone for a mixture of `rubocop:disable` for the files that can't
  be `typed: strict` (use of undef, required before everything else, etc)
  and `rubocop:todo` for everything else that should be tried to make
  strictly typed. There's no functional difference between the two as
  `rubocop:todo` is `rubocop:disable` with a different name.

- And I entirely disabled the cop for the docs/ directory since
  `typed: strict` isn't going to gain us anything for some Markdown
  linting config files.

- This means that now it's easier to track what needs to be done rather
  than relying on checklists of files in our big Sorbet issue:

```shell
$ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l
    268
```

- And this is confirmed working for new files:

```shell
$ git status
On branch use-rubocop-for-sorbet-strict-sigils
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Library/Homebrew/bad.rb
        Library/Homebrew/good.rb

nothing added to commit but untracked files present (use "git add" to track)

$ brew style
Offenses:

bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true.
^^^^^^^^^^^^^

1340 files inspected, 1 offense detected
```
2024-08-12 15:24:27 +01:00
Carlo Cabrera
5e027bffd6
Merge pull request #17149 from alebcay/elf-expand-origin
elf: expand $ORIGIN in RUNPATH/RPATH entries
2024-04-28 01:14:54 +08:00
Markus Reiter
0f0055ede4
Make documentation @api private by default. 2024-04-26 19:04:20 +02:00
Caleb Xu
6ee34832d1
elf: expand $ORIGIN in RUNPATH/RPATH entries 2024-04-25 22:52:25 -04:00
Caleb Xu
100c7f36b1
os/linux/kernel: enable strict typing 2024-04-25 21:36:40 -04:00
Ruoyu Zhong
916b37388d
Revert "Revert "os/linux/elf: avoid using ldd for listing dynamic dependencies"" 2024-04-24 02:23:13 +08:00
Ruoyu Zhong
852c5acb65
Revert "os/linux/elf: avoid using ldd for listing dynamic dependencies" 2024-04-16 02:50:12 +08:00
Caleb Xu
a4e0ccc1f6
os/linux/elf: avoid using ldd for listing dynamic dependencies 2024-04-13 19:35:30 -04:00
Caleb Xu
c6b98d0b8c
os/linux/ld: add functions for querying dynamic linker 2024-04-13 19:35:29 -04:00
Issy Long
f4218a6316
Fix RuboCop Performance/MapCompact offenses
- Rename an iterator variable since it would make the line too long.
2024-02-25 22:59:59 +00:00
Douglas Eichelberger
08af78a2a5 brew style --fix 2023-04-25 09:26:24 -07:00
Douglas Eichelberger
24cf6076e8 brew style --fix 2023-04-24 20:42:39 -07:00
Douglas Eichelberger
adfd12cfd0 Enable more typing 2023-03-21 21:42:51 -07:00
Bo Anderson
1882ae4f6e
Update Linux requirements
* Glibc 2.26+ (we use 2.35) require Linux kernel 3.2 or later.
* Remove GCC requirement given it doesn't matter for bottles (we install GCC ourselves if too old) and is probably wrong for the build-from-source case.
* Remove libxcrypt-compat requirement given we don't use libcrypt.so.1 anymore.
2022-10-16 17:08:17 +01:00
Carlo Cabrera
25910b9c68
Use Array.
Co-authored-by: Mike McQuaid <mike@mikemcquaid.com>
2022-08-26 22:29:55 +08:00
Carlo Cabrera
ab696cbbb7
linux/diagnostic: fix rpath returning nil.
Fixes #13762.

There's still a bug in the GCC linkage check, but I'll need a bit more
time to work on a fix. This at least makes sure `brew doctor` will not
return the error in the issue linked above.
2022-08-26 22:23:02 +08:00
Michka Popoff
d271614872
install glibc/gcc automatically if too old.
Right now this is done through the gcc@5 formula.
See 9692318ca6/Formula/gcc%405.rb (L33)

This is fragile because when we will migrate to gcc@11
we have to think about migrating the installation from one gcc formula to another..
Also, not having the right glibc version results in a non-functional brew
installation on an older Linux: the glibc installation needs
to be done by brew, and not by a workaround in a specific formula

Co-Authored-By: Mike McQuaid <mike@mikemcquaid.com>
Co-Authored-By: Bo Anderson <mail@boanderson.me>
Co-Authored-By: Shaun Jackman <sjackman@gmail.com>
2022-08-25 11:04:37 +01:00
Chris Araman
caf310038f
linkage: display executables with missing rpath
An executable that links against @rpath-prefixed dylibs must include at least
one runtime path. This will prevent issues like the one resolved in #91485.

Caveats:
1. This won't find executables that have only recursive dylib dependencies with
   @rpath prefixes.
2. This makes no attempt to resolve @rpath, @executable_path or @loader_path
   dylibs, or to verify the correctness of any LC_RPATH entries, or to
   otherwise simulate dlopen logic.
3. Weakly-linked dylibs are still excluded from the search for broken linkage.

The scope is narrow in order to focus on this particular problem. It is meant
only as a sanity check.
2021-12-20 13:44:35 -08:00
Bo Anderson
9493cbb80a
os/linux/elf: don't report missing libraries to dependents 2021-10-31 17:03:29 +00:00
Mike McQuaid
0cda6a0657
Only use patchelf.rb
Remove support for using the `patchelf` formula for writing as it's been
off by default for a long time now.
2021-10-19 15:15:56 +01:00
fn ⌃ ⌥
9638e3e8c0 development_tools: update type signatures 2021-09-29 17:44:26 -07:00
Carlo Cabrera
d696250ddb
Fix ELF architecture offset for x86_64
The current offset results in ELF binaries returning an `#arch` of
`:dunno`.

Also, skip the `check_binary_arches` audit on the generic OS.
2021-07-18 16:55:57 +08:00
Shaun Jackman
70292829f3
Cache glibc.system_version 2021-04-02 10:58:01 -07:00
Shaun Jackman
393027597b
Cache glibc.version
Co-authored-by: Mike McQuaid <mike@mikemcquaid.com>
2021-04-02 10:53:47 -07:00
Shaun Jackman
95e7e010ee Add OS::Linux::Glibc.version 2021-04-01 22:16:33 -07:00
Mike McQuaid
e65e310fd0
Fix brew style 2021-01-29 20:56:59 +00:00
EricFromCanada
2ed324ebc0 use backticks around suggested input, flags, and binaries 2021-01-26 16:19:47 -05:00
Seeker
32ebc02f2f version: enable Sorbet type checking 2021-01-19 09:28:37 -08:00
Maxim Belkin
45d67ef13e
brew.sh: define minimum required Glibc version 2020-12-24 09:10:53 -06:00
Shaun Jackman
81716100ae Enable HOMEBREW_PATCHELF_RB_WRITE by default
It may be disabled using HOMEBREW_NO_PATCHELF_RB_WRITE.

It is currently disabled in brew test-bot when building bottles.
2020-12-11 18:08:10 -08:00
Mike McQuaid
0f9bad0052
Fix bottle prefix and repository handling
We were previously only looking at the `cellar` value when pouring
bottles and ignoring the `prefix` and (implicit) `repository`.

Actually look at these values and set the defaults for each platform.

Also, when we're relocating to create or pour bottles when `prefix`
and `repository` are equal then skip relocating the `repository` and
always use references to the `prefix` instead.

Fixes #9453
2020-12-09 13:53:10 +00:00