apple-support-ar-wrapper: ar r drops the members already in the archive

Open
#685 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp, macos
Domain
build-system

Research direction

Start with the ar mode handling in crosstool/libtool.cc, where the wrapper decides between a fresh libtool -static write and the q path that passes the existing archive back in. Reproduce with the two cr calls in an empty directory and check ar t lib.a; expected output is a.o b.o. Done means r, cr and crs keep earlier members with matching names, and the q behaviour from #670 still holds.

Written by the indexing model from the issue text.

Description

Since #601, apple-support-ar-wrapper accepts ar arguments. For r (with or without c, s, D) it runs libtool -static -D -o lib.a <new objects>, which writes a new archive. The members that an earlier call added are lost. The command exits 0, so the first sign is undefined symbols at link time.

#670 fixed the same problem for q: it passes the existing archive back to libtool as an input. That approach is not correct for r. r replaces each member that has the same name, so if the whole archive is passed back, the old member stays next to its replacement, and the linker takes the first one, which is the stale one.

Repro

Build the wrapper from the 2.11.0 tag (crosstool:exec_libtool, or xcrun clang -std=c++17 -lc++ -o apple-support-ar-wrapper crosstool/libtool.cc). Set DEVELOPER_DIR and SDKROOT (any values; the wrapper only requires them). Use any two objects a.o and b.o, and start each row of the table in an empty directory.

apple-support-ar-wrapper cr lib.a a.o
apple-support-ar-wrapper cr lib.a b.o
ar t lib.a

Expected: a.o and b.o. Actual: only b.o. (ar t also lists __.SYMDEF SORTED in both cases.)

Members after two calls (a.o, then b.o), from crosstool/libtool.cc at 2.11.0 and from /usr/bin/ar:

Mode wrapper /usr/bin/ar
r b.o a.o b.o
cr b.o a.o b.o
crs b.o a.o b.o
crD b.o (D not supported)
q a.o b.o a.o b.o
cq a.o b.o a.o b.o
Notes for a fix
  • ar r puts each new member in the place of the first old member with the same name, and appends the new members that replace nothing.
  • To match by name, the wrapper must name members after their files in ar mode, as ar does. Today it renames duplicate basenames to <stem>_<hash>.o.

Versions: apple_support 2.11.0, macOS 26.6.2 arm64, Xcode.

Dominant language
Starlark
Stars
109
Forks
79
Avg merge
4h 29m
Merged PRs (30d)
11

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bazelbuild/apple_support

All issues in bazelbuild/apple_support

Similar issues

More Build System issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.