Subject: "slight hack" to make objcopy ELF->ECOFF produce OMAGIC
To: None <tech-toolchain@netbsd.org>
From: Jason R Thorpe <thorpej@zembu.com>
List: tech-toolchain
Date: 06/07/2001 08:51:29
Folks..

I've been playing with BFD in order to eliminate the need for elf2ecoff,
which is decidedly cross-compile hostile.

Background: elf2ecoff is there to make an ECOFF from an ELF executable
in such a form as the DECstation PROM console will swallow.  A slightly
modified version is used for the SGI port, as well, as the ARCS console
will only swallow OMAGIC executables there.

So, with some linker flags and ldscript changes, I was able to get an
ELF with a single PT_LOAD section, which is like OMAGIC:

netbsd:     file format elf32-bigmips
netbsd
architecture: mips:3000, flags 0x00000012:
EXEC_P, HAS_SYMS
start address 0x88002000

Program Header:
0x70000000 off    0x001164a0 vaddr 0x881174a0 paddr 0x881174a0 align 2**2
         filesz 0x00000018 memsz 0x00000018 flags r--
    LOAD off    0x00001000 vaddr 0x88002000 paddr 0x88002000 align 2**12
         filesz 0x00119f60 memsz 0x00140c20 flags rwx

Using my hacked BFD, objcopy --target=ecoff-bigmips generates:

netbsd.ip22:     file format ecoff-bigmips
netbsd.ip22
architecture: mips:3000, flags 0x00000032:
EXEC_P, HAS_SYMS, HAS_LOCALS
start address 0x88002000

sulaco:thorpej 12$ file netbsd.ip22                                            
netbsd.ip22: MIPSEB COFF executable (impure) not stripped - version 0.0
sulaco:thorpej 13$ 

...and the ARCS console does indeed load and run the kernel.

The "slight hack" is required because the file offset of the PT_LOAD
section is page aligned... if it were not page aligned, BFD would clear
D_PAGED internally, which would cause the ECOFF output routine to generate
an OMAGIC executable.  If anyone has a suggestion as to how to make sure
the PT_LOAD isn't page aligned in the file, please share it with me, so
that this "slight hack" isn't necessary.

-- 
        -- Jason R. Thorpe <thorpej@zembu.com>

Index: elfcode.h
===================================================================
RCS file: /cvsroot/gnusrc/gnu/dist/bfd/elfcode.h,v
retrieving revision 1.1.1.2
diff -c -r1.1.1.2 elfcode.h
*** elfcode.h	1999/02/02 19:51:57	1.1.1.2
--- elfcode.h	2001/06/07 15:29:07
***************
*** 463,468 ****
--- 463,469 ----
    struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd);
    struct elf_obj_tdata *new_tdata = NULL;
    asection *s;
+   int nptload = 0, nptdynamic = 0, ptflags = 0;
  
    /* Read in the ELF header in external format.  */
  
***************
*** 635,641 ****
--- 636,653 ----
  	      != sizeof x_phdr)
  	    goto got_no_match;
  	  elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
+ 	  if (i_phdr->p_type == PT_LOAD)
+ 	    nptload++;
+ 	  else if (i_phdr->p_type == PT_DYNAMIC)
+ 	    nptdynamic++;
+ 	  ptflags |= i_phdr->p_flags;
  	}
+       /* If there is only one PT_LOAD section, it is at least rw-, and
+ 	 there is no PT_DYNAMIC section, assume the executable is not
+ 	 paged. */
+       if (nptload == 1 && nptdynamic == 0 &&
+           (ptflags & (PF_R|PF_W)) == (PF_R|PF_W))
+ 	abfd->flags &= ~D_PAGED;
      }
  
    /* Read in the string table containing the names of the sections.  We