Force hidden visibility for public symbols?

Hi swift users!
We have a project that has many swift modules to keep project structured. All of them compiles down to a static lib (framework), and then linked together to main executable. As I know, all the swift public declaration will result in public symbols, so it can be shared across linkage units. Even if we strip the symbol names, relocation info will still remain, and also those symbols can not participate in link time optimizations.

Is it possible to say to swift compiler that it should keep all the public symbols hidden, like we do with -fvisibility=hidden in clang?

Please correct me if I'm wrong or saying something silly :slight_smile:

It's reasonable to expect that public symbols from statically linked libraries be given hidden visibility by default. It seems like ideally, this is something the build system such as swiftpm could control automatically when it knows it's building for a static library.

There's a fairly old bug about this topic: SR-1021

The last time I did any hacking on this (a couple years ago), symbol visibility wasn't the only issue that I saw, but public symbols were also tossed into the @llvm.used list so they wouldn't get stripped ever. I don't know if that's still the case—back then, I tried building a version of the compiler that removed the "if public, make it used" condition to see if things would break horribly if I started stripping the resulting executables, but things worked ok. To be clear, this was with one or two extremely small and trivial test cases so it was by no means thorough.

It's not clear why a free function would need to be marked "used" just because it's public, but I'd be more worried about other edge cases that rely on dynamic/runtime dispatch: types exported to Objective-C, protocols, class vtables, etc. What are the risks there?

1 Like
Terms of Service

Privacy Policy

Cookie Policy