From nobody@FreeBSD.org  Sun Jan  9 00:23:36 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id BE776106564A
	for <freebsd-gnats-submit@FreeBSD.org>; Sun,  9 Jan 2011 00:23:36 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (unknown [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id AE7408FC0C
	for <freebsd-gnats-submit@FreeBSD.org>; Sun,  9 Jan 2011 00:23:36 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p090Na6l096166
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 9 Jan 2011 00:23:36 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p090NaTe096165;
	Sun, 9 Jan 2011 00:23:36 GMT
	(envelope-from nobody)
Message-Id: <201101090023.p090NaTe096165@red.freebsd.org>
Date: Sun, 9 Jan 2011 00:23:36 GMT
From: Gleb Kurtsou <gleb.kurtsou@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [patch] btxld produces incorrect ELF binaries
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         153801
>Category:       bin
>Synopsis:       [patch] btxld(8) produces incorrect ELF binaries
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    brueffer
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jan 09 00:30:05 UTC 2011
>Closed-Date:    Thu Mar 27 21:30:46 CET 2014
>Last-Modified:  Thu Mar 27 21:30:46 CET 2014
>Originator:     Gleb Kurtsou
>Release:        
>Organization:
>Environment:
FreeBSD 9.0-CURRENT #11 r216922+3950ba5
>Description:
ELF binaries linked by btxld have invalid .data section virtual address.

In a.out binaries .data section is aligned at page size, but it's not the case with ELF. btxldr expects kernel to be on next page (.data section):

see sys/boot/i386/btx/btxldr.S:

 .set MEM_DATA,start+0x1000      # Data segment

With the patch applied resulted binaries can be booted by grub (as well as a.out binaries). I didn't check if booting ELF loader is supported by FreeBSD boot2 code.
>How-To-Repeat:

>Fix:


Patch attached with submission follows:

diff --git a/usr.sbin/btxld/btxld.c b/usr.sbin/btxld/btxld.c
index 1102b5c..7984c4c 100644
--- a/usr.sbin/btxld/btxld.c
+++ b/usr.sbin/btxld/btxld.c
@@ -426,7 +426,7 @@ puthdr(int fd, struct hdr *hdr)
 	    le32toh(eh.p[0].p_filesz));
 	eh.p[1].p_vaddr = eh.p[1].p_paddr =
 	    htole32(align(le32toh(eh.p[0].p_paddr) + le32toh(eh.p[0].p_memsz),
-	    4));
+	    4096));
 	eh.p[1].p_filesz = eh.p[1].p_memsz = htole32(hdr->data);
 	eh.sh[2].sh_addr = eh.p[0].p_vaddr;
 	eh.sh[2].sh_offset = eh.p[0].p_offset;


>Release-Note:
>Audit-Trail:

From: Ruben Kerkhof <ruben@rubenkerkhof.com>
To: bug-followup@freebsd.org, gleb.kurtsou@gmail.com
Cc:  
Subject: Re: bin/153801: [patch] btxld(8) produces incorrect ELF binaries
Date: Mon, 28 May 2012 22:17:33 +0200

 With this patch I can successfully load /boot/loader with an
 ELF-header (instead of a.out) from gptboot or boot2.
 
 To test this, one has to change btxld -v -f aout to btxld -v -f elf in
 sys/boot/i386/loader/Makefile.
 
 Regards,
 
 Ruben Kerkhof
State-Changed-From-To: open->patched 
State-Changed-By: brueffer 
State-Changed-When: Tue Feb 25 18:14:48 CET 2014 
State-Changed-Why:  
Committed, thanks! 


Responsible-Changed-From-To: freebsd-bugs->brueffer 
Responsible-Changed-By: brueffer 
Responsible-Changed-When: Tue Feb 25 18:14:48 CET 2014 
Responsible-Changed-Why:  
MFC reminder. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=153801 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/153801: commit references a PR
Date: Tue, 25 Feb 2014 17:13:51 +0000 (UTC)

 Author: brueffer
 Date: Tue Feb 25 17:13:42 2014
 New Revision: 262484
 URL: http://svnweb.freebsd.org/changeset/base/262484
 
 Log:
   In puthdr(), start the ELF .data section on a new page, as this is
   what btxldr expects (.set MEM_DATA,start+0x1000 in btxldr.S).
   
   This makes resulting ELF binaries bootable with grub, gptboot and boot2.
   
   PR:		153801
   Submitted by:	Gleb Kurtsou <gleb.kurtsou at gmail.com>
   Tested by:	Ruben Kerkhof <ruben at rubenkerkhof.com>
   Glanced at by:	jhb, peter
   MFC after:	1 month
 
 Modified:
   head/usr.sbin/btxld/btxld.c
 
 Modified: head/usr.sbin/btxld/btxld.c
 ==============================================================================
 --- head/usr.sbin/btxld/btxld.c	Tue Feb 25 17:02:11 2014	(r262483)
 +++ head/usr.sbin/btxld/btxld.c	Tue Feb 25 17:13:42 2014	(r262484)
 @@ -426,7 +426,7 @@ puthdr(int fd, struct hdr *hdr)
  	    le32toh(eh.p[0].p_filesz));
  	eh.p[1].p_vaddr = eh.p[1].p_paddr =
  	    htole32(align(le32toh(eh.p[0].p_paddr) + le32toh(eh.p[0].p_memsz),
 -	    4));
 +	    4096));
  	eh.p[1].p_filesz = eh.p[1].p_memsz = htole32(hdr->data);
  	eh.sh[2].sh_addr = eh.p[0].p_vaddr;
  	eh.sh[2].sh_offset = eh.p[0].p_offset;
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/153801: commit references a PR
Date: Thu, 27 Mar 2014 20:19:14 +0000 (UTC)

 Author: brueffer
 Date: Thu Mar 27 20:19:11 2014
 New Revision: 263835
 URL: http://svnweb.freebsd.org/changeset/base/263835
 
 Log:
   MFC: r262484
   
   In puthdr(), start the ELF .data section on a new page, as this is
   what btxldr expects (.set MEM_DATA,start+0x1000 in btxldr.S).
   
   This makes resulting ELF binaries bootable with grub, gptboot and boot2.
   
   PR:		153801
   Submitted by:	Gleb Kurtsou <gleb.kurtsou at gmail.com>
   Tested by:	Ruben Kerkhof <ruben at rubenkerkhof.com>
   Glanced at by:	jhb, peter
 
 Modified:
   stable/10/usr.sbin/btxld/btxld.c
 Directory Properties:
   stable/10/   (props changed)
 
 Modified: stable/10/usr.sbin/btxld/btxld.c
 ==============================================================================
 --- stable/10/usr.sbin/btxld/btxld.c	Thu Mar 27 20:15:35 2014	(r263834)
 +++ stable/10/usr.sbin/btxld/btxld.c	Thu Mar 27 20:19:11 2014	(r263835)
 @@ -426,7 +426,7 @@ puthdr(int fd, struct hdr *hdr)
  	    le32toh(eh.p[0].p_filesz));
  	eh.p[1].p_vaddr = eh.p[1].p_paddr =
  	    htole32(align(le32toh(eh.p[0].p_paddr) + le32toh(eh.p[0].p_memsz),
 -	    4));
 +	    4096));
  	eh.p[1].p_filesz = eh.p[1].p_memsz = htole32(hdr->data);
  	eh.sh[2].sh_addr = eh.p[0].p_vaddr;
  	eh.sh[2].sh_offset = eh.p[0].p_offset;
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/153801: commit references a PR
Date: Thu, 27 Mar 2014 20:21:16 +0000 (UTC)

 Author: brueffer
 Date: Thu Mar 27 20:21:13 2014
 New Revision: 263837
 URL: http://svnweb.freebsd.org/changeset/base/263837
 
 Log:
   MFC: r262484
   
   In puthdr(), start the ELF .data section on a new page, as this is
   what btxldr expects (.set MEM_DATA,start+0x1000 in btxldr.S).
   
   This makes resulting ELF binaries bootable with grub, gptboot and boot2.
   
   PR:             153801
   Submitted by:   Gleb Kurtsou <gleb.kurtsou at gmail.com>
   Tested by:      Ruben Kerkhof <ruben at rubenkerkhof.com>
   Glanced at by:  jhb, peter
 
 Modified:
   stable/8/usr.sbin/btxld/btxld.c
 Directory Properties:
   stable/8/usr.sbin/btxld/   (props changed)
 
 Modified: stable/8/usr.sbin/btxld/btxld.c
 ==============================================================================
 --- stable/8/usr.sbin/btxld/btxld.c	Thu Mar 27 20:20:20 2014	(r263836)
 +++ stable/8/usr.sbin/btxld/btxld.c	Thu Mar 27 20:21:13 2014	(r263837)
 @@ -426,7 +426,7 @@ puthdr(int fd, struct hdr *hdr)
  	    le32toh(eh.p[0].p_filesz));
  	eh.p[1].p_vaddr = eh.p[1].p_paddr =
  	    htole32(align(le32toh(eh.p[0].p_paddr) + le32toh(eh.p[0].p_memsz),
 -	    4));
 +	    4096));
  	eh.p[1].p_filesz = eh.p[1].p_memsz = htole32(hdr->data);
  	eh.sh[2].sh_addr = eh.p[0].p_vaddr;
  	eh.sh[2].sh_offset = eh.p[0].p_offset;
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/153801: commit references a PR
Date: Thu, 27 Mar 2014 20:20:24 +0000 (UTC)

 Author: brueffer
 Date: Thu Mar 27 20:20:20 2014
 New Revision: 263836
 URL: http://svnweb.freebsd.org/changeset/base/263836
 
 Log:
   MFC: r262484
   
   In puthdr(), start the ELF .data section on a new page, as this is
   what btxldr expects (.set MEM_DATA,start+0x1000 in btxldr.S).
   
   This makes resulting ELF binaries bootable with grub, gptboot and boot2.
   
   PR:             153801
   Submitted by:   Gleb Kurtsou <gleb.kurtsou at gmail.com>
   Tested by:      Ruben Kerkhof <ruben at rubenkerkhof.com>
   Glanced at by:  jhb, peter
 
 Modified:
   stable/9/usr.sbin/btxld/btxld.c
 Directory Properties:
   stable/9/usr.sbin/btxld/   (props changed)
 
 Modified: stable/9/usr.sbin/btxld/btxld.c
 ==============================================================================
 --- stable/9/usr.sbin/btxld/btxld.c	Thu Mar 27 20:19:11 2014	(r263835)
 +++ stable/9/usr.sbin/btxld/btxld.c	Thu Mar 27 20:20:20 2014	(r263836)
 @@ -426,7 +426,7 @@ puthdr(int fd, struct hdr *hdr)
  	    le32toh(eh.p[0].p_filesz));
  	eh.p[1].p_vaddr = eh.p[1].p_paddr =
  	    htole32(align(le32toh(eh.p[0].p_paddr) + le32toh(eh.p[0].p_memsz),
 -	    4));
 +	    4096));
  	eh.p[1].p_filesz = eh.p[1].p_memsz = htole32(hdr->data);
  	eh.sh[2].sh_addr = eh.p[0].p_vaddr;
  	eh.sh[2].sh_offset = eh.p[0].p_offset;
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: brueffer 
State-Changed-When: Thu Mar 27 21:30:28 CET 2014 
State-Changed-Why:  
Merge to stable branches done. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=153801 
>Unformatted:
