public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Problem with D_PAGED handling for ELF
@ 2005-01-25 21:46 Daniel Jacobowitz
  2005-01-26  0:21 ` Alan Modra
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Jacobowitz @ 2005-01-25 21:46 UTC (permalink / raw)
  To: binutils

I was handed an interesting bug where objcopy caused corruption of the
program headers for a binary produced by a third party toolchain.  The VMAs
and alignments got eaten.  After a lot of banging my head on the ground, I
discovered that this happened because the producing toolchain differed from
binutils on its opinion of the page size.

Here's how BFD verifies that a binary is "usefully" dynamic paged:

          /* If the section is loaded, but not page aligned, clear
             D_PAGED.  */
          if (i_shdrp[shindex].sh_size != 0
              && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
              && i_shdrp[shindex].sh_type != SHT_NOBITS
              && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
                   % ebd->maxpagesize)
                  != 0))
            abfd->flags &= ~D_PAGED;

The problem is, binutils was configured to generate binaries that would work
on any page size up to 64k, and the third party toolchain was configured to
generate binaries that would work on a particular system where the toolchain
was known to be 4k.  So it only aligned the segments to 4k.

There's two plausible solutions I can see for this:
  - Add a "minimum page size" to the ELF backend, and use that instead for
    this check.  Generate binaries that will work up to MAX, accept binaries
    designed for at least MIN.
  - Disable the check entirely.  Assume any ELF binary with program headers
    is demand paged.  I am not sure about the consequences of this.

(I tried working around it by just "fixing" the page size.  Unfortunately,
the target systems have configurable page sizes, and I don't have a good
interface for telling things like objcopy or strip which one is currently in
use.)

Any comments on the two options?

-- 
Daniel Jacobowitz

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with D_PAGED handling for ELF
  2005-01-25 21:46 Problem with D_PAGED handling for ELF Daniel Jacobowitz
@ 2005-01-26  0:21 ` Alan Modra
  2005-01-26 16:27   ` Daniel Jacobowitz
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Modra @ 2005-01-26  0:21 UTC (permalink / raw)
  To: binutils

On Tue, Jan 25, 2005 at 04:46:25PM -0500, Daniel Jacobowitz wrote:
>   - Add a "minimum page size" to the ELF backend, and use that instead for
>     this check.  Generate binaries that will work up to MAX, accept binaries
>     designed for at least MIN.

Sounds reasonable.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with D_PAGED handling for ELF
  2005-01-26  0:21 ` Alan Modra
@ 2005-01-26 16:27   ` Daniel Jacobowitz
  2005-01-27  7:56     ` Alan Modra
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Jacobowitz @ 2005-01-26 16:27 UTC (permalink / raw)
  To: binutils

On Wed, Jan 26, 2005 at 10:50:31AM +1030, Alan Modra wrote:
> On Tue, Jan 25, 2005 at 04:46:25PM -0500, Daniel Jacobowitz wrote:
> >   - Add a "minimum page size" to the ELF backend, and use that instead for
> >     this check.  Generate binaries that will work up to MAX, accept binaries
> >     designed for at least MIN.
> 
> Sounds reasonable.

How's this?  This patch allows ARM and PPC toolchains to support
binaries linked for 4K pages.

-- 
Daniel Jacobowitz

2005-01-26  Daniel Jacobowitz  <dan@codesourcery.com>

	* elf-bfd.h (struct elf_backend_data): Add minpagesize.
	* elf32-arm.c (ELF_MINPAGESIZE): Define.
	* elf32-ppc.c (ELF_MINPAGESIZE): Define.
	* elfcode.h (elf_object_p): Use minpagesize instead of
	maxpagesize.
	* elfxx-target.h (ELF_MINPAGESIZE): Default to ELF_MAXPAGESIZE.
	(elfNN_bed): Include ELF_MINPAGESIZE.

Index: bfd/elf-bfd.h
===================================================================
RCS file: /big/fsf/rsync/src-cvs/src/bfd/elf-bfd.h,v
retrieving revision 1.166
diff -u -p -r1.166 elf-bfd.h
--- bfd/elf-bfd.h	10 Dec 2004 14:04:56 -0000	1.166
+++ bfd/elf-bfd.h	26 Jan 2005 15:42:17 -0000
@@ -1,6 +1,6 @@
 /* BFD back-end data structures for ELF files.
    Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
-   2002, 2003, 2004 Free Software Foundation, Inc.
+   2002, 2003, 2004, 2005 Free Software Foundation, Inc.
    Written by Cygnus Support.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -551,6 +551,11 @@ struct elf_backend_data
   /* The maximum page size for this backend.  */
   bfd_vma maxpagesize;
 
+  /* The minimum page size for this backend.  An input object will not be
+     considered page aligned unless its sections are correctly aligned for
+     pages at least this large.  May be smaller than maxpagesize.  */
+  bfd_vma minpagesize;
+
   /* The BFD flags applied to sections created for dynamic linking.  */
   flagword dynamic_sec_flags;
 
Index: bfd/elf32-arm.c
===================================================================
RCS file: /big/fsf/rsync/src-cvs/src/bfd/elf32-arm.c,v
retrieving revision 1.14
diff -u -p -r1.14 elf32-arm.c
--- bfd/elf32-arm.c	31 Dec 2004 16:22:25 -0000	1.14
+++ bfd/elf32-arm.c	26 Jan 2005 15:42:19 -0000
@@ -1,5 +1,5 @@
 /* 32-bit ELF support for ARM
-   Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004
+   Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
    Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -5695,6 +5695,7 @@ const struct elf_size_info elf32_arm_siz
 #else
 #define ELF_MAXPAGESIZE			0x8000
 #endif
+#define ELF_MINPAGESIZE			0x1000
 
 #define bfd_elf32_bfd_copy_private_bfd_data	elf32_arm_copy_private_bfd_data
 #define bfd_elf32_bfd_merge_private_bfd_data	elf32_arm_merge_private_bfd_data
Index: bfd/elf32-ppc.c
===================================================================
RCS file: /big/fsf/rsync/src-cvs/src/bfd/elf32-ppc.c,v
retrieving revision 1.131
diff -u -p -r1.131 elf32-ppc.c
--- bfd/elf32-ppc.c	10 Dec 2004 14:04:56 -0000	1.131
+++ bfd/elf32-ppc.c	26 Jan 2005 15:42:22 -0000
@@ -1,6 +1,6 @@
 /* PowerPC-specific support for 32-bit ELF
    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
-   2004 Free Software Foundation, Inc.
+   2004, 2005 Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Cygnus Support.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -6176,6 +6176,7 @@ static struct bfd_elf_special_section co
 #else
 #define ELF_MAXPAGESIZE		0x10000
 #endif
+#define ELF_MINPAGESIZE		0x1000
 #define elf_info_to_howto	ppc_elf_info_to_howto
 
 #ifdef  EM_CYGNUS_POWERPC
Index: bfd/elfcode.h
===================================================================
RCS file: /big/fsf/rsync/src-cvs/src/bfd/elfcode.h,v
retrieving revision 1.61
diff -u -p -r1.61 elfcode.h
--- bfd/elfcode.h	11 Dec 2004 04:32:35 -0000	1.61
+++ bfd/elfcode.h	26 Jan 2005 15:42:25 -0000
@@ -1,6 +1,6 @@
 /* ELF executable support for BFD.
    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
-   2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+   2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
    Written by Fred Fish @ Cygnus Support, from information published
    in "UNIX System V Release 4, Programmers Guide: ANSI C and
@@ -680,7 +680,7 @@ elf_object_p (bfd *abfd)
 	      && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
 	      && i_shdrp[shindex].sh_type != SHT_NOBITS
 	      && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
-		   % ebd->maxpagesize)
+		   % ebd->minpagesize)
 		  != 0))
 	    abfd->flags &= ~D_PAGED;
 	}
Index: bfd/elfxx-target.h
===================================================================
RCS file: /big/fsf/rsync/src-cvs/src/bfd/elfxx-target.h,v
retrieving revision 1.73
diff -u -p -r1.73 elfxx-target.h
--- bfd/elfxx-target.h	8 Oct 2004 14:53:59 -0000	1.73
+++ bfd/elfxx-target.h	26 Jan 2005 15:42:37 -0000
@@ -1,6 +1,6 @@
 /* Target definitions for NN-bit ELF
    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
-   2003, 2004 Free Software Foundation, Inc.
+   2003, 2004, 2005 Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
@@ -283,6 +283,10 @@
 #define ELF_MAXPAGESIZE 1
 #endif
 
+#ifndef ELF_MINPAGESIZE
+#define ELF_MINPAGESIZE ELF_MAXPAGESIZE
+#endif
+
 #ifndef ELF_DYNAMIC_SEC_FLAGS
 /* Note that we set the SEC_IN_MEMORY flag for these sections.  */
 #define ELF_DYNAMIC_SEC_FLAGS			\
@@ -519,6 +523,7 @@ static const struct elf_backend_data elf
   ELF_ARCH,			/* arch */
   ELF_MACHINE_CODE,		/* elf_machine_code */
   ELF_MAXPAGESIZE,		/* maxpagesize */
+  ELF_MINPAGESIZE,		/* minpagesize */
   ELF_DYNAMIC_SEC_FLAGS,        /* dynamic_sec_flags */
   elf_info_to_howto,
   elf_info_to_howto_rel,

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with D_PAGED handling for ELF
  2005-01-26 16:27   ` Daniel Jacobowitz
@ 2005-01-27  7:56     ` Alan Modra
  0 siblings, 0 replies; 4+ messages in thread
From: Alan Modra @ 2005-01-27  7:56 UTC (permalink / raw)
  To: binutils

On Wed, Jan 26, 2005 at 11:27:14AM -0500, Daniel Jacobowitz wrote:
> 	* elf-bfd.h (struct elf_backend_data): Add minpagesize.
> 	* elf32-arm.c (ELF_MINPAGESIZE): Define.
> 	* elf32-ppc.c (ELF_MINPAGESIZE): Define.
> 	* elfcode.h (elf_object_p): Use minpagesize instead of
> 	maxpagesize.
> 	* elfxx-target.h (ELF_MINPAGESIZE): Default to ELF_MAXPAGESIZE.
> 	(elfNN_bed): Include ELF_MINPAGESIZE.

Looks good to me.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-01-27  7:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-25 21:46 Problem with D_PAGED handling for ELF Daniel Jacobowitz
2005-01-26  0:21 ` Alan Modra
2005-01-26 16:27   ` Daniel Jacobowitz
2005-01-27  7:56     ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).