From owner-svn-src-all@freebsd.org Thu Feb 23 00:02:50 2017 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 92B51CEAA94; Thu, 23 Feb 2017 00:02:50 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 52536A75; Thu, 23 Feb 2017 00:02:50 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v1N02n3v047001; Thu, 23 Feb 2017 00:02:49 GMT (envelope-from jhb@FreeBSD.org) Received: (from jhb@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v1N02nA3047000; Thu, 23 Feb 2017 00:02:49 GMT (envelope-from jhb@FreeBSD.org) Message-Id: <201702230002.v1N02nA3047000@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org using -f From: John Baldwin Date: Thu, 23 Feb 2017 00:02:49 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r314115 - head/libexec/rtld-elf/mips X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Feb 2017 00:02:50 -0000 Author: jhb Date: Thu Feb 23 00:02:49 2017 New Revision: 314115 URL: https://svnweb.freebsd.org/changeset/base/314115 Log: Fully handle the special encoding of GOT[1] on mips64. The MIPS ABI does not require the second GOT entry to be reserved for use by the runtime linker as on other architectures. Instead, static linkers use a special value in the second GOT entry to indicate if the entry is reserved. This value is supposed to consist of an address with the MSB set and the rest of the bits all zero which is an invalid user address. However, the old binutils currently in the tree uses the 32-bit mask value (2^31) on 64-bit MIPS instead of 2^63. This was fixed in upstream binutils in 2008 to use 2^63 on 64-bit MIPS. The first part of this change changes the runtime check in init_pltgot() to check for both values (2^31 and 2^63) when deciding whether to store the current object pointer in GOT[1] which fixes dynamic N64 binaries compiled with modern binutils. However, the initial version of this fix exposed another related bug in that _rtld_relocate_nonplt_self() was only checking for the new value (2^63) in GOT[1] and incorrectly treated GOT[1] as a local GOT entry (and did not relocate the final local GOT entry). To handle this, fix all of the places that check for GOT[1]'s status to use the same macro that checks for both values on N64. Reviewed by: kan, imp Sponsored by: DARPA / AFRL Differential Revision: https://reviews.freebsd.org/D9708 Modified: head/libexec/rtld-elf/mips/reloc.c Modified: head/libexec/rtld-elf/mips/reloc.c ============================================================================== --- head/libexec/rtld-elf/mips/reloc.c Wed Feb 22 23:58:54 2017 (r314114) +++ head/libexec/rtld-elf/mips/reloc.c Thu Feb 23 00:02:49 2017 (r314115) @@ -51,12 +51,28 @@ __FBSDID("$FreeBSD$"); #define GOT1_MASK 0x80000000UL #endif +/* + * Determine if the second GOT entry is reserved for rtld or if it is + * the first "real" GOT entry. + * + * This must be a macro rather than a function so that + * _rtld_relocate_nonplt_self doesn't trigger a GOT invocation trying + * to use it before the local GOT entries in rtld are adjusted. + */ +#ifdef __mips_n64 +/* Old binutils uses the 32-bit GOT1 mask value for N64. */ +#define GOT1_RESERVED_FOR_RTLD(got) \ + (((got)[1] == 0x80000000) || (got)[1] & GOT1_MASK) +#else +#define GOT1_RESERVED_FOR_RTLD(got) ((got)[1] & GOT1_MASK) +#endif + void init_pltgot(Obj_Entry *obj) { if (obj->pltgot != NULL) { obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start; - if (obj->pltgot[1] & 0x80000000) + if (GOT1_RESERVED_FOR_RTLD(obj->pltgot)) obj->pltgot[1] = (Elf_Addr) obj | GOT1_MASK; } } @@ -175,7 +191,7 @@ _rtld_relocate_nonplt_self(Elf_Dyn *dynp } } - i = (got[1] & GOT1_MASK) ? 2 : 1; + i = GOT1_RESERVED_FOR_RTLD(got) ? 2 : 1; /* Relocate the local GOT entries */ got += i; for (; i < local_gotno; i++) { @@ -294,7 +310,7 @@ reloc_non_plt(Obj_Entry *obj, Obj_Entry dbg("%s: broken=%d", obj->path, broken); #endif - i = (got[1] & GOT1_MASK) ? 2 : 1; + i = GOT1_RESERVED_FOR_RTLD(got) ? 2 : 1; /* Relocate the local GOT entries */ got += i;