Fix missing `google::protobuf::RepeatedPtrField<std::string>` issue in 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
pull/10979/head
Adam Cozzette 2 years ago committed by Copybara-Service
parent 21815faf0f
commit 225b936c01
  1. 2
      src/google/protobuf/repeated_field.cc
  2. 3
      src/google/protobuf/repeated_ptr_field.h

@ -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"
@ -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>;

@ -2021,9 +2021,6 @@ UnsafeArenaAllocatedRepeatedPtrFieldBackInserter(
mutable_field);
}
extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE
RepeatedPtrField<std::string>;
} // namespace protobuf
} // namespace google

Loading…
Cancel
Save