Do not access beyond the end of local symbols.

This patch resurrects code that was removed in r317007 to not access
beyond allocated memory for a symbol.

Differential Revision: https://reviews.llvm.org/D39458

llvm-svn: 317039
This commit is contained in:
Rui Ueyama 2017-10-31 21:26:42 +00:00
parent 5914ece6aa
commit 9fa2813803
1 changed files with 11 additions and 1 deletions

View File

@ -106,7 +106,17 @@ void SymbolTable::reportRemainingUndefines() {
// A weak alias may have been resolved, so check for that.
if (Defined *D = Undef->getWeakAlias()) {
memcpy(Sym, D, sizeof(SymbolUnion));
// We want to replace Sym with D. However, we can't just blindly
// copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
// internal symbol, and internal symbols are stored as "unparented"
// Symbols. For that reason we need to check which type of symbol we
// are dealing with and copy the correct number of bytes.
if (isa<DefinedRegular>(D))
memcpy(Sym, D, sizeof(DefinedRegular));
else if (isa<DefinedAbsolute>(D))
memcpy(Sym, D, sizeof(DefinedAbsolute));
else
memcpy(Sym, D, sizeof(SymbolUnion));
continue;
}