From owner-svn-src-all@FreeBSD.ORG Sat Aug 9 22:51:27 2014 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 78EE115E for ; Sat, 9 Aug 2014 22:51:27 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4D87423D9 for ; Sat, 9 Aug 2014 22:51:27 +0000 (UTC) Received: from imp (uid 547) (envelope-from imp@FreeBSD.org) id 2fbb by svn.freebsd.org (DragonFly Mail Agent v0.9+); Sat, 09 Aug 2014 22:51:27 +0000 From: Warner Losh Date: Sat, 9 Aug 2014 22:51:27 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r269767 - head/sys/arm/arm X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Message-Id: <53e6a5ef.2fbb.3bbe0e71@svn.freebsd.org> X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.18 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: Sat, 09 Aug 2014 22:51:27 -0000 Author: imp Date: Sat Aug 9 22:51:26 2014 New Revision: 269767 URL: http://svnweb.freebsd.org/changeset/base/269767 Log: Per discussion on arm@, the compiler generates misaligned relocations. Cope with memcpy when needed. Submitted by: fabient@ (plus changes suggested by thread) Modified: head/sys/arm/arm/elf_machdep.c Modified: head/sys/arm/arm/elf_machdep.c ============================================================================== --- head/sys/arm/arm/elf_machdep.c Sat Aug 9 21:01:24 2014 (r269766) +++ head/sys/arm/arm/elf_machdep.c Sat Aug 9 22:51:26 2014 (r269767) @@ -120,6 +120,34 @@ elf32_dump_thread(struct thread *td __un { } +/* + * It is possible for the compiler to emit relocations for unaligned data. + * We handle this situation with these inlines. + */ +#define RELOC_ALIGNED_P(x) \ + (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0) + +static __inline Elf_Addr +load_ptr(Elf_Addr *where) +{ + Elf_Addr res; + + if (RELOC_ALIGNED_P(where)) + return *where; + memcpy(&res, where, sizeof(res)); + return (res); +} + +static __inline void +store_ptr(Elf_Addr *where, Elf_Addr val) +{ + if (RELOC_ALIGNED_P(where)) + *where = val; + else + memcpy(where, &val, sizeof(val)); +} +#undef RELOC_ALIGNED_P + /* Process one elf relocation with addend. */ static int @@ -137,7 +165,7 @@ elf_reloc_internal(linker_file_t lf, Elf case ELF_RELOC_REL: rel = (const Elf_Rel *)data; where = (Elf_Addr *) (relocbase + rel->r_offset); - addend = *where; + addend = load_ptr(where); rtype = ELF_R_TYPE(rel->r_info); symidx = ELF_R_SYM(rel->r_info); break; @@ -155,8 +183,8 @@ elf_reloc_internal(linker_file_t lf, Elf if (local) { if (rtype == R_ARM_RELATIVE) { /* A + B */ addr = elf_relocaddr(lf, relocbase + addend); - if (*where != addr) - *where = addr; + if (load_ptr(where) != addr) + store_ptr(where, addr); } return (0); } @@ -170,7 +198,7 @@ elf_reloc_internal(linker_file_t lf, Elf addr = lookup(lf, symidx, 1); if (addr == 0) return -1; - *where += addr; + store_ptr(where, addr + load_ptr(where)); break; case R_ARM_COPY: /* none */ @@ -185,7 +213,7 @@ elf_reloc_internal(linker_file_t lf, Elf case R_ARM_JUMP_SLOT: addr = lookup(lf, symidx, 1); if (addr) { - *where = addr; + store_ptr(where, addr); return (0); } return (-1);