Author Topic: Strange behaviour with a.out format  (Read 3597 times)

Offline antoineL

  • Jr. Member
  • *
  • Posts: 2
Strange behaviour with a.out format
« on: March 09, 2010, 01:05:02 PM »
Hi folks,

I am having a strange behaviour with NASM with the "aout" object format.
I reduced it to the following testcase
Code: [Select]
  BITS 32
  GLOBAL fwd_bwd

  SECTION .text
    fwd_bwd:
  nop
  jmp forward
    backward:   ret

  SECTION .data

  dd 1,2,3
    forward:
  jmp backward
  nop
The resulting bytes are disassembled as follow (using objdump, edited locally):
Code: [Select]
00000000 <fwd_bwd>:
   0: 90                    nop   
   1: e9 0e 00 00 00        jmp    14 <forward>
2: DISP32 .data+0xfffffff8

00000006 <backward>:
   6: c3                    ret   
   7: 90                    nop   

Disassembly of section .data:

00000008 <forward-0xc>:
   8: 01 00                add    %eax,(%eax)
   a: 00 00                add    %al,(%eax)
   c: 02 00                add    (%eax),%al
   e: 00 00                add    %al,(%eax)
  10: 03 00                add    (%eax),%eax
...

00000014 <forward>:
  14: e9 f5 ff ff ff        jmp    6 <backward+0x8>
15: DISP32 .text
  19: 90                    nop     

However, comparing with another assembler, I believe the result should rather be
Code: [Select]
00000000 <fwd_bwd>:
   0: 90                    nop   
   1: e9 0e 00 00 00        jmp    14 <forward>
2: DISP32 .data+0xfffffff8

00000006 <backward>:
   6: c3                    ret   
   7: 90                    nop   

Disassembly of section .data:

00000008 <forward-0xc>:
   8: 01 00                add    %eax,(%eax)
   a: 00 00                add    %al,(%eax)
   c: 02 00                add    (%eax),%al
   e: 00 00                add    %al,(%eax)
  10: 03 00                add    (%eax),%eax
...

00000014 <forward>:
  14: e9 ed ff ff ff        jmp    6 <backward>
15: DISP32 .text
  19: 90                    nop     


The problem is with the way the "jmp" instruction in the data section is encoded: it looks like Nasm forgets to adjust the emitted bytes in the .data section referenceing the .text section, failing (erroneously) to substracting to them the size of .text section, here 8. I got this impression from the quote below (from output/outaout.c)
Quote
/*
 * a.out files have the curious property that all references to
 * things in the data or bss sections are done by addresses which
 * are actually relative to the start of the _text_ section, in the
 * _file_. (No relation to what happens after linking. No idea why
 * this should be so. It's very strange.) So we have to go through
 * the relocation table, _after_ the final size of each section is
 * known, and fix up the relocations pointed to.
 */
static void aout_fixup_relocs(struct Section *sect)

What should be done here?

Antoine

Offline H. Peter Anvin

  • NASM Developer
  • Jr. Member
  • *****
  • Posts: 18
Re: Strange behaviour with a.out format
« Reply #1 on: April 06, 2010, 09:06:02 PM »
What platform are you seeing this on?  a.out isn't widely used anymore...

Offline antoineL

  • Jr. Member
  • *
  • Posts: 2
Re: Strange behaviour with a.out format
« Reply #2 on: April 07, 2010, 08:05:51 AM »
Thanks for your attention.

What platform are you seeing this on?  a.out isn't widely used anymore...
Here are the gory details, since you asked  ;)

I am working on Minix 3, which is using an hybrid of the classical PC/IX a.out used for years, and the classical 4BSD a.out; and as you know, Minix is still alive and kicking, along with that unbearable perfume of nostalgia :).

But the reported problem is not with this breed of a.out (since Minix does not allow execution from .data, the test case is rather pointless, at the very least convoluted.)
Rather that, while testing my mods I created a more involved test case for any kind of relocation I could imagine, and found a difference here (with respect to binutils and 4BSD a.out); and furthermore my attention was drawn by the (quoted) comment, which I take as a hint that Nasm code could be improved. At the very least, the comment could be changed to really explain how a.out works.

Is there interest for that in the mainline of Nasm?