What is the structure of a.out executable?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
antoni
Member
Member
Posts: 61
Joined: Sun May 24, 2020 9:11 am
Location: /dev/null

What is the structure of a.out executable?

Post by antoni »

I want to implement some executable format because:

A) Flat binaries are horrible.
B) I want to write programs in C.

I chose a.out. The only reasonable source I found was this:

https://www.freebsd.org/cgi/man.cgi?query=a.out&apropos=0&sektion=0&manpath=NetBSD+1.4&format=html

There are very detailed descriptions of structures used in a.out and information that the file starts with the exec structure in this text. Unfortunately, there is no information where the rest of them is in the file. Moreover, the sizes of these structures read from the exec header do not add up to the total file size. The file size is 80 bytes. The text segment is 20 bytes long. The size of the symbol table is 12 and the size of the text relocation table is 8. Additionally, exec structure has 32 bytes, so the file is 8 bytes longer than it should be. I obtained this file by assembling this code with NASM:

Code: Select all

BITS 32

mov edi, msg
mov eax, 0x125716
call eax
ret

msg:
db 'TEST', 10, 0
by this command:

Code: Select all

nasm -f aout aout_test.asm -o aout_test
Here is hexdump:

Code: Select all

0000000 0107 0064 0014 0000 0000 0000 0000 0000
0000010 000c 0000 0000 0000 0008 0000 0000 0000
0000020 0dbf 0000 b800 5716 0012 d0ff 54c3 5345
0000030 0a54 9000 0001 0000 0004 0400 0004 0000
0000040 0004 0000 000d 0000 0008 0000 736d 0067
0000050
exec header of this file:

Code: Select all

{a_midmag = 6553863, a_text = 20, a_data = 0, a_bss = 0, a_syms = 12, a_entry = 0, a_trsize = 8, a_drsize = 0}
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: What is the structure of a.out executable?

Post by PeterX »

I have the book "Linkers & Loaders". From that the a.out file header is:

Code: Select all

a_midmag = 6553863 -> "magic" to recognize a.out
a_text = 20 -> sizeof code ("text") section
a_data = 0 -> size of data (Maybe you should specify "section .data" before the string def?)
a_bss = 0 -> size of bss
a_syms = 12 -> size of the symbol table
a_entry = 0 -> offset to start point of the code (I'm a bit irritated that this is zero. Maybe you must specify it? Or maybe it is because "section .text" is missing? Or is it relative to text-start?)
a_trsize = 8 -> text relocation size
a_drsize = 0 -> data relocation size
[All values are 32bit.]
The sections containing the actual "stuff" are simply concatenated, right after the file header:

Code: Select all

code
data
bss
symbol table
text reloc info
data reloc info
EDIT: I overlooked your comment on the additional 8 bytes. Sorry. I don'tknow how they are "solved".

EDIT2: I read the book and there is possibly one more header entry? Maybe string table size?

Greetings
Peter
Last edited by PeterX on Fri Sep 25, 2020 10:08 am, edited 1 time in total.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: What is the structure of a.out executable?

Post by bzt »

antoni wrote:I chose a.out. The only reasonable source I found was this:
What about our wiki? It would be great if you could provide more details on that page as you progress with your development.

Cheers,
bzt
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: What is the structure of a.out executable?

Post by PeterX »

bzt wrote:
antoni wrote:I chose a.out. The only reasonable source I found was this:
What about our wiki? It would be great if you could provide more details on that page as you progress with your development.

Cheers,
bzt
Great info! Unfortunately a.out doesn't show up in the list, so I (and probably others, too) didn't know that this page exists.

Greetings
Peter
reapersms
Member
Member
Posts: 48
Joined: Fri Oct 04, 2019 10:10 am

Re: What is the structure of a.out executable?

Post by reapersms »

The excess is the string table the symbol table references.
The string table consists of an unsigned long length followed by null-
terminated symbol strings. The length represents the size of the entire
table in bytes, so its minimum value (or the offset of the first string)
is always 4 on 32-bit machines.
So, 4 bytes for a total size of the symbol table, length included (00000008), and 4 bytes for the null-terminated "msg"
antoni
Member
Member
Posts: 61
Joined: Sun May 24, 2020 9:11 am
Location: /dev/null

Re: What is the structure of a.out executable?

Post by antoni »

According to wikipedia page about executable formats a.out have extension for 64-bit executables. Unfortunately, the only source about a.out in the bibliography is the page from the BSD manual I mentioned. I wish I could run 64-bit codes. Do you know any assembler/compiler with support od this format, or open source os that implements it? NASM supports only 32-bit a.out.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: What is the structure of a.out executable?

Post by bzt »

antoni wrote:According to wikipedia page about executable formats a.out have extension for 64-bit executables. Unfortunately, the only source about a.out in the bibliography is the page from the BSD manual I mentioned. I wish I could run 64-bit codes. Do you know any assembler/compiler with support od this format, or open source os that implements it? NASM supports only 32-bit a.out.
According to the gcc docs, gcc, GNU as and GNU ld supports 64 bit a.out.

But as long as assemblers concerned, you can always output in binary format and add the a.out header yourself. You'll need some tricky macros, but absolutely doable. For example MenuetOS, which has it's own, very a.out like header for executables does this.

As for NASM, it looks to me it's rather easy to add 64 bit a.out, see outaout.c. Just replace "fwriteint32_t" with "fwriteint64_t" and use proper magic values. Or use "-fbin" and create header and sections with macros.

Cheers,
bzt
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: What is the structure of a.out executable?

Post by Schol-R-LEA »

Just curious, but is there a reason you want to use a.out, specifically? While there are plenty of other Executable Formats, and while a.out is one of the simplest, so I can see why it may be appealing, but are you certain that it supports everything you need? For C programming, ELF and PE are both more commonly used today.

I would strongly recommend the John Levine book, Linkers and Loaders, if you can afford it (US $58 new on Amazon, last I checked). The beta version of the book and the accompanying source code is free on Levine's web page, but it isn't complete and has some known errors. While it is almost 25 years old now, it is still accurate to most of the standards currently used (though not on the 64-bit extensions).
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
antoni
Member
Member
Posts: 61
Joined: Sun May 24, 2020 9:11 am
Location: /dev/null

Re: What is the structure of a.out executable?

Post by antoni »

ELF is difficult to implement. Certainly much more difficult than a.out. I haven't read much about PE, but it's Microsoft's format, so even if it were easy to implement, I wouldn't have a way to generate executables (I don't have Windows).

To load a.out executable, you just need to copy text and data sections to memory and apply relocations.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: What is the structure of a.out executable?

Post by bzt »

antoni wrote:ELF is difficult to implement.
No, it's not.
antoni wrote:To load a.out executable, you just need to copy text and data sections to memory and apply relocations.
No different to ELF. See here, a simple C code that copies text and data segments to memory, less than 10 SLoC.

The difficulty comes in when you start to implement relocations, but that's no different in a.out. You have to traverse relocation records and patch memory for both.

Cheers,
bzt
Post Reply