Comments (13)
__attribute__((retain))
marks a section so that the section will not be garbage-collected by the linker's --gc-sections
. On the other hand, --retain-symbols-file
makes the linker to keep the specified symbols in the symbol table. They serve different purposes. Could you explain a little bit more about what you are trying to achieve?
from mold.
I have a bunch of symbols in my executable that are attribute used, retain
in order to not prune them when stripping. This at least works with ld, but I'm not sure exactly why. So, I am just trying to come up with other solutions to retain these symbols automatically. There are quite a few of them, and only an automated solution will work.
My CMake build uses gc-sections, stripping and retain-symbols-file + a few --undefined= for some assembly functions that wouldn't stick otherwise.
I see that --undefined=symbol
does work, at least, and I do have a symbol file. It's just not scalable to write down all the symbols one by one. One thing I noticed, unrelated to this issue, is that I had to change -Wl,-u,symbol
to -Wl,--undefined=symbol
.
What confuses me is that retain is a symbol attribute, yet it's supposed to be applied to a section?
from mold.
in order to not prune them when stripping
By stripping, do you mean the strip
command or the --gc-sections
linker option?
__attribute__((retain))
does not work for symbols, it marks a section referred by the specified symbols to be kept during --gc-sections
.
from mold.
I took a look at LLVM lld source code and indeed the behavior implemented to lld seems different from what I did to mold. So mold's --retain-symbols-file
may be misbehaving. Let me take a look further and get back to you
from mold.
Actually it looks like lld's behavior is incompatible with GNU ld, so I filed it as llvm/llvm-project#91055.
That's not directly related to your request, I guess, though.
from mold.
Actually it looks like lld's behavior is incompatible with GNU ld, so I filed it as llvm/llvm-project#91055.
That's not directly related to your request, I guess, though.
I think this might be it, and that you hit the nail on the head. I am building static executables that I am using as a low-latency scripting backend for a game server and client. All in all a gargantuan task of creating the emulator, to custom run-times and build systems all the way down to keeping assembly and extern symbols. In your issue you describe ld retaining by storing the symbols in .symtab, and that would indeed be the way that I am looking for those symbols that I want to retain.
So, just to reiterate: Using ld, when I mark a symbol as used, retain
, it will still appear in .symtab despite -Wl,-x,-S
and even with --gc-sections
and --retain-symbols-file
.
I don't exactly know the reasoning behind it, but perhaps it's only for static executables? Either way, it solves my problem of being able to make public functions directly in the code, that cannot be stripped.
EDIT: I will test without retain, and see what happens.
I tested it, and it is indeed __attribute__((retain))
alone that somehow keeps the symbols from getting stripped. It seems that used
is for preventing the compiler from optimizing out the function.
from mold.
I'm not sure if I understand your problem correctly. I can do the followings.
- Keep sections marked with
__attribute__((retain))
even with--gc-sections
(that's what mold is already doing), and - keep symbols in the sections marked with
__attribute__((retain))
in .symtab even with--strip
(this is new)
Does this what you want?
from mold.
I think so - but I can try it out. Where would I make this change in the sources to try it? If it matches ld's behavior, it's likely it solves my problem.
from mold.
There are always subtle differences among different linkers, so "just match ld's behavior" isn't something feasible, just like "just creating a browser that match chrome's behavior" isn't feasible. I'd like to know what exactly you want mold to behave to match GNU ld's behavior.
from mold.
Minimal example:
long testPruned(void)
{
return 42;
}
__attribute__((retain))
long testRetained(void)
{
return 42;
}
void _start() {}
Then compile and link:
gcc-12 -static -O2 -nostdlib -ffunction-sections -fdata-sections -c test.c -o test.o
gcc-12 -static -O2 -nostdlib -Wl,-gc-sections test.o -o test.elf
Only the retained function remains:
$ nm test.elf
0000000000404000 R __bss_start
0000000000404000 R _edata
0000000000404000 R _end
0000000000401010 T _start
0000000000401000 T testRetained
I also tested this with GCC-13 and GCC-14 (with the matching binutils)
I'm not sure if it can be simplified even further, but this at least shows that the retained attribute must do something to either move a symbol into a separate retained section, or give the symbol a flag. I'm guessing the former?
from mold.
I was wrong about the retained symbol file. --retain-symbols-file
overrides -S and -x and will only leave the symbols listed in the file, except relocations.
It says it will leave undefined symbols, but that is not the case.
from mold.
We do support the RETAIN section flag. Please try your test case with the mold linker. So far, this bug doesn't seem to be actionable.
from mold.
Yep, it was the retain-symbols-file that got overridden on GNU ld, but not mold. I don't really need it so as far as I'm concerned it's all good, and I can use mold now! Thanks, and sorry for the inconvenience. Somehow I thought retain would also apply to the symbols file.
from mold.
Related Issues (20)
- Building with pyo3 and maturin works with default linker, but not with mold HOT 1
- XWayland segfaults when linked with mold HOT 3
- tool-x
- make install does not install libblake3.so HOT 1
- Compiling from source on CPU without AVX2 fails HOT 2
- .rodata wrong with rust HOT 26
- not able to compile in windows . HOT 3
- -lc not working HOT 3
- --wrap support for MacOS HOT 3
- x86_64-exception-multiple-ehframe fails with GCC 14 HOT 10
- [Question] Is `mold` any compatible at all with wine-tkg (and anything Wine related) to build? HOT 2
- Binaries linked with mold-2.30.0 randomly either work or crash in `_dl_relocate_object` HOT 4
- Fold sold back into mold? HOT 1
- arm-arm_abs-error failed: sorry, unimplemented: Thumb-1 'hard-float' VFP ABI HOT 3
- `mold 2.31.0` build failure when targeting `ARMv6` HOT 3
- Missing -z option rodynamic HOT 1
- riscv64-shared-abs-sym fails with 2.31.0 HOT 2
- Statically linked binaries crash with illegal hardware instructions HOT 7
- Cannot build project using tealr and Mold linker HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mold.