Subject: Re: objcopy making a mess of converting kernels to a.out format
To: Chuck Silvers <chuq@chuq.com>
From: Jason Thorpe <thorpej@wasabisystems.com>
List: port-cats
Date: 09/01/2004 12:31:44
--Apple-Mail-6--204212579
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed


On Aug 29, 2004, at 10:33 AM, Chuck Silvers wrote:

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

I've been discussing this problem with Ian Taylor... do you have a ELF 
kernel you can send me that fails to copy correctly?

>
> 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.
> <fixtextsize.sh>
         -- Jason R. Thorpe <thorpej@wasabisystems.com>


--Apple-Mail-6--204212579
content-type: application/pgp-signature; x-mac-type=70674453;
	name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (Darwin)

iD8DBQFBNiOgOpVKkaBm8XkRAmjbAJ9WFmtlzo0sBvfi+mtQDzKeuZtzngCgpxJQ
ijeHWDLyGVPFT41TNiDbW38=
=K4fs
-----END PGP SIGNATURE-----

--Apple-Mail-6--204212579--