Subject: Re: objcopy making a mess of converting kernels to a.out format
To: None <Richard.Earnshaw@buzzard.freeserve.co.uk>
From: Chuck Silvers <chuq@chuq.com>
List: port-cats
Date: 08/29/2004 10:33:36
--EeQfGwPcQSOJBaQU
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

hi,

I'm looking into this issue since the PR for it (26160) is designated
a show-stopper for the 2.0 release.

the actual problem here is that the a.out text segment size is too small,
since it only takes into account the ELF ".text" section and not any of
the link_set_* sections.  the a.out header appears to be constructed by
bfd/aoutx.h:adjust_z_magic(), here's the context:

#0  0x0808f1ca in adjust_z_magic ()
#1  0x0808f6c4 in aout_32_adjust_sizes_and_vmas ()
#2  0x0808f80c in aout_32_set_section_contents ()
#3  0x0806bc8c in bfd_set_section_contents ()
#4  0x0804bc33 in copy_section ()
#5  0x0806bb62 in bfd_map_over_sections ()
#6  0x0804aedc in copy_object ()
#7  0x0804b523 in copy_file ()
#8  0x0804ce7f in copy_main ()
#9  0x0804d04a in main ()

the problem is that this function assumes that the a.out text segment
should only be as big as the .text section that was precreated by objcopy
in setup_section().  I fussed a bit with making BFD do the right thing,
but I didn't make much headway since I don't know much about BFD.

instead I propose the attached script to fix up the aout text segment size
after objcopy does the conversion.  does anyone see any problem with this?

if no one can come up with anything better, I'll check in this script
and change the cats and shark Makefile.*.inc fragments to use it
in a couple days.

-Chuck



On Sun, Feb 29, 2004 at 07:04:33PM +0000, Richard Earnshaw wrote:
> I'm finding evidence of objcopy making a mess of creating kernels for 
> machines that boot a.out.  Here's a dump of the headers:
> 
> /work/rearnsha/netbsd/build/cats/tools/bin/arm--netbsdelf-objdump -h netbsd
> 
> netbsd:     file format elf32-littlearm
> 
> Sections:
> Idx Name          Size      VMA       LMA       File off  Algn
>   0 .text         0023bf43  f0000020  f0000020  00008020  2**5
>                   CONTENTS, ALLOC, LOAD, READONLY, CODE
>   1 link_set_malloc_types 00000120  f023bf64  f023bf64  00243f64  2**2
>                   CONTENTS, ALLOC, LOAD, READONLY, DATA
>   2 link_set_sysctl_funcs 0000008c  f023c084  f023c084  00244084  2**2
>                   CONTENTS, ALLOC, LOAD, READONLY, DATA
>   3 link_set_evcnts 00000004  f023c110  f023c110  00244110  2**2
>                   CONTENTS, ALLOC, LOAD, READONLY, DATA
>   4 .data         000462c0  f023d000  f023d000  00245000  2**2
>                   CONTENTS, ALLOC, LOAD, DATA
>   5 .bss          00030f64  f02832c0  f02832c0  0028b2c0  2**2
>                   ALLOC
>   6 .arm.atpcs    00000000  00000000  00000000  0028b2c0  2**0
>                   CONTENTS, READONLY
>   7 .comment      000056d7  00000000  00000000  0028b2c0  2**0
>                   CONTENTS, READONLY
>   8 .ident        000062cd  00000000  00000000  00290997  2**0
>                   CONTENTS, READONLY
> 
> /work/rearnsha/netbsd/build/cats/tools/bin/arm--netbsdelf-objdump -h 
> netbsd.aout
> 
> netbsd.aout:     file format a.out-arm-netbsd
> 
> Sections:
> Idx Name          Size      VMA       LMA       File off  Algn
>   0 .text         0023bfe0  f0000020  f0000020  00000020  2**2
>                   CONTENTS, ALLOC, LOAD, CODE
>   1 .data         00047000  f023c000  f023c000  0023c000  2**2
>                   CONTENTS, ALLOC, LOAD, DATA
>   2 .bss          00030224  f0283000  f0283000  00000000  2**2
>                   ALLOC
> 
> note that in the file generated by objcopy (netbsd.aout) the bss is marked 
> to start at VA 0xf0283000, which is earlier than the end of the .data 
> section in the original -- the result of this will be that the end of the 
> data section is corrupted when the image loaded (it will be set to zero).
> 
> I'm not sure, but I suspect that the culprit is those link_sections, which 
> aren't being fully accounted in the size of the data section.  The result 
> is that when the size of the .data section is calculated for the a.out 
> file it comes out one page too small (in the a.out file the start of the 
> .bss should be statically zeroed data in the last page of the .data 
> section).
> 
> Ideas anyone?
> 
> R.

--EeQfGwPcQSOJBaQU
Content-Type: application/x-sh
Content-Disposition: attachment; filename="fixtextsize.sh"
Content-Transfer-Encoding: quoted-printable

#!/bin/sh=0A=0A# Fix up the a.out text segment size to compensate for a bin=
utils bug=0A# where objcopy doesn't count link_set ELF sections in the text=
 size.=0A=0AELF=3D$1=0AAOUT=3D$2=0AOUT=3D$3=0A=0Atextvma=3D`$OBJDUMP -h $EL=
F | grep \\.text | awk '{print $4}'`=0Adatavma=3D`$OBJDUMP -h $ELF | grep \=
\.data | awk '{print $4}'`=0A=0Atextsz=3D`(=0A	echo ibase=3D16=0A	echo "$da=
tavma - $textvma + 20" | tr [a-f] [A-F]=0A) | bc`=0A=0Acp -p $AOUT $OUT.tmp=
=0A=0Aecho $textsz | awk \=0A'{=0A	printf "\047\\%o\\%o\\%o\\%o\047",=0A		$=
1 % 256, ($1 / 256) % 256, ($1 / 65536) % 256, 0=0A}' | xargs printf | dd o=
f=3D$OUT.tmp bs=3D4 count=3D1 seek=3D1 conv=3Dnotrunc=0A=0Amv $OUT.tmp $OUT=
=0A
--EeQfGwPcQSOJBaQU--