gold won't build with current gcc

Alan Modra amodra@gmail.com
Wed Dec 5 01:46:00 GMT 2018


.../symtab.h: In constructor 'gold::Symbol::Symbol()':
.../symtab.h:917:33: error: 'void* memset(void*, int, size_t)' clearing an object of type 'class gold::Symbol' with no trivial copy-assignment; use assignment or value-initialization instead [-Werror=class-memaccess]
  917 |   { memset(this, 0, sizeof *this); }

>From gcc/doc/invoke.texi:

@item -Wclass-memaccess @r{(C++ and Objective-C++ only)}
Warn when the destination of a call to a raw memory function such as
@code{memset} or @code{memcpy} is an object of class type, and when writing
into such an object might bypass the class non-trivial or deleted constructor
or copy assignment, violate const-correctness or encapsulation, or corrupt
virtual table pointers.  Modifying the representation of such objects may
violate invariants maintained by member functions of the class.  For example,
the call to @code{memset} below is undefined because it modifies a non-trivial
class object and is, therefore, diagnosed.  The safe way to either initialize
or clear the storage of objects of such types is by using the appropriate
constructor or assignment operator, if one is available.
@smallexample
std::string str = "abc";
memset (&str, 0, sizeof str);
@end smallexample
The @option{-Wclass-memaccess} option is enabled by @option{-Wall}.
Explicitly casting the pointer to the class object to @code{void *} or
to a type that can be safely accessed by the raw memory function suppresses
the warning.

So we can fix this as follows.  OK?

	* symtab.h (Symbol::Symbol): Avoid -Wclass-memaccess warning.

diff --git a/gold/symtab.h b/gold/symtab.h
index 089e156b45..16a244855d 100644
--- a/gold/symtab.h
+++ b/gold/symtab.h
@@ -914,7 +914,7 @@ class Symbol
   // Instances of this class should always be created at a specific
   // size.
   Symbol()
-  { memset(this, 0, sizeof *this); }
+  { memset(static_cast<void*>(this), 0, sizeof *this); }
 
   // Initialize the general fields.
   void

-- 
Alan Modra
Australia Development Lab, IBM



More information about the Binutils mailing list