[LLD] `--mmap-output-file` does not behave as expected
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Run this script with LLD 21.1.8:
```bash
#!/bin/sh
dd if=/dev/zero of=blob bs=1M count=256 status=none
printf '' | cc -xc -c -o empty.o -
ulimit -d $((100 * 1024)) # 100 MiB anonymous-memory cap
for flag in --no-mmap-output-file --mmap-output-file; do
if ld.lld -shared -o out.so empty.o --format=binary blob --format=elf --threads=1 $flag 2>err; then
echo "$flag: OK"
else
echo "$flag: FAILED ($(head -c80 err))"
fi
done
```
outputs:
```
--no-mmap-output-file: OK
--mmap-output-file: FAILED (ld.lld: error: failed to open out.so: Cannot allocate memory)
```
Somehow `--mmap-output-file` causes the file not to be memory-mapped and causes higher resident memory usage.
This behavior has changed in LLD 21. On prior versions it gives:
```
--no-mmap-output-file: FAILED (ld.lld: error: failed to open out.so: Cannot allocate memory)
--mmap-output-file: OK
```
Probably due to https://github.com/llvm/llvm-project/pull/139836.
I am not sure I understand the "misnamed" rationale in that PR, but it seems quite confusing that `--no-mmap-output-file` is the flag that mmaps the output file. It's true that`--mmap-output-file` also calls `mmap` but only to mmap anonymous memory.
Contributor guide
Research direction
Run the supplied shell reproducer with LLD 21.1.8 and compare both flags under the 100 MiB memory cap. Trace the --mmap-output-file handling associated with PR 139836; done means the flags no longer show the reversed success and resident-memory behavior reported here.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- shell
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100