Skip to content

Commit

Permalink
Fix missing google::protobuf::RepeatedPtrField<std::string> issue i…
Browse files Browse the repository at this point in the history
…n GCC

When we build DSOs in CMake, we use the `-fvisibility=hidden` flag to hide
symbols by default and then explicitly export public-facing symbols using
`__attribute__((visibility ("default")))` (for example via the
`PROTOBUF_EXPORT_TEMPLATE_DECLARE` macro).

A bunch of symbols related to `google::protobuf::RepeatedPtrField<std::string>`
were not visible on libprotobuf.so for GCC builds, and it turned out to be
because GCC was ignoring the visibility attribute due to it appearing after
`RepeatedPtrField<std::string>` had already been instantiated.

I tried moving the declaration up in the file, but found that if I move it up
high enough to fix GCC then it breaks Clang. So instead, this change just
disables the code size optimization that required us to centralize the template
definition in one .cc file. Now any usage of `RepeatedPtrField<std::string>`
will be inlined into each translation unit that uses it, so we don't have to
worry about the definition being visible in libprotobuf.so.

Fixes #10833.

PiperOrigin-RevId: 487885077
  • Loading branch information
acozzette authored and copybara-github committed Nov 11, 2022
1 parent 21815fa commit 225b936
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 4 deletions.
2 changes: 1 addition & 1 deletion src/google/protobuf/repeated_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "google/protobuf/repeated_field.h"

#include <algorithm>
#include <string>

#include "google/protobuf/stubs/logging.h"
#include "google/protobuf/stubs/common.h"
Expand All @@ -54,7 +55,6 @@ template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField<int64_t>;
template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField<uint64_t>;
template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField<float>;
template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField<double>;
template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedPtrField<std::string>;

namespace internal {
template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedIterator<bool>;
Expand Down
3 changes: 0 additions & 3 deletions src/google/protobuf/repeated_ptr_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -2021,9 +2021,6 @@ UnsafeArenaAllocatedRepeatedPtrFieldBackInserter(
mutable_field);
}

extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE
RepeatedPtrField<std::string>;

} // namespace protobuf
} // namespace google

Expand Down

0 comments on commit 225b936

Please sign in to comment.