Subject: ELF format questions
To: None <port-mac68k@netbsd.org>
From: None <nigel@ind.tansu.com.au>
List: port-mac68k
Date: 03/01/2001 13:30:19
	This is more of a question for the kernel list,
but I had to unsubscribe to that (too many mailing lists).
Sorry for the slightly off-topic nature.


	I am trying to add ELF support to the Booter.
Now, I have added most of the guts of load_elf() from
libsa's loadfile.c, but there are a few aspects of an
ELF program that I don't understand.


	Chuck Silvers kindly provided me with an ELF kernel.
Here is a dump of its contents:

% elfdump -ecps .Macintosh/netbsd.elf | head -n90

ELF Header
  ei_magic:   { 0x7f, E, L, F }
  ei_class:   ELFCLASS32          ei_data:      ELFDATA2MSB
  e_machine:  EM_68K              e_version:    EV_CURRENT
  e_type:     ET_EXEC
  e_flags:                     0
  e_entry:                0x2e00  e_ehsize:     52  e_shstrndx:    6
  e_shoff:              0x1d6918  e_shentsize:  40  e_shnum:       9
  e_phoff:                  0x34  e_phentsize:  32  e_phnum:       2

Program Header[0]:
    p_vaddr:      0x0             p_flags:    [ PF_X  PF_R ]
    p_paddr:      0x0             p_type:     [ PT_LOAD ]
    p_filesz:     0x1c31a7        p_memsz:    0x1c31a7
    p_offset:     0x78            p_align:    0x8

Program Header[1]:
    p_vaddr:      0x1c51a8        p_flags:    [ PF_W  PF_R ]
    p_paddr:      0x1c51a8        p_type:     [ PT_LOAD ]
    p_filesz:     0x134fc         p_memsz:    0x4f7fc
    p_offset:     0x1c3220        p_align:    0x10

Section Header[1]:  sh_name: .text
    sh_addr:      0x0             sh_flags:   [ SHF_ALLOC  SHF_EXECINSTR ]
    sh_size:      0x1973e8        sh_type:    [ SHT_PROGBITS ]
    sh_offset:    0x78            sh_entsize: 0x0
    sh_link:      0               sh_info:    0
    sh_addralign: 0x8           

Section Header[2]:  sh_name: .rodata
    sh_addr:      0x1973e8        sh_flags:   [ SHF_ALLOC ]
    sh_size:      0x2bdbf         sh_type:    [ SHT_PROGBITS ]
    sh_offset:    0x197460        sh_entsize: 0
    sh_link:      0               sh_info:    0
    sh_addralign: 0x4           

Section Header[3]:  sh_name: .data
    sh_addr:      0x1c51a8        sh_flags:   [ SHF_WRITE  SHF_ALLOC ]
    sh_size:      0x134fc         sh_type:    [ SHT_PROGBITS ]
    sh_offset:    0x1c3220        sh_entsize: 0
    sh_link:      0               sh_info:    0
    sh_addralign: 0x8           

Section Header[4]:  sh_name: .bss
    sh_addr:      0x1d86b0        sh_flags:   [ SHF_WRITE  SHF_ALLOC ]
    sh_size:      0x3c2f4         sh_type:    [ SHT_NOBITS ]
    sh_offset:    0x1d6728        sh_entsize: 0
    sh_link:      0               sh_info:    0
    sh_addralign: 0x10          

Section Header[5]:  sh_name: .ident
    sh_addr:      0x2149a4        sh_flags:   0
    sh_size:      0x1b4           sh_type:    [ SHT_PROGBITS ]
    sh_offset:    0x1d6728        sh_entsize: 0
    sh_link:      0               sh_info:    0
    sh_addralign: 0x1           

Section Header[6]:  sh_name: .shstrtab
    sh_addr:      0               sh_flags:   0
    sh_size:      0x3b            sh_type:    [ SHT_STRTAB ]
    sh_offset:    0x1d68dc        sh_entsize: 0
    sh_link:      0               sh_info:    0
    sh_addralign: 0x1           

Section Header[7]:  sh_name: .symtab
    sh_addr:      0               sh_flags:   0
    sh_size:      0x44b70         sh_type:    [ SHT_SYMTAB ]
    sh_offset:    0x1d6a80        sh_entsize: 0x10
    sh_link:      8               sh_info:    10787
    sh_addralign: 0x4           

Section Header[8]:  sh_name: .strtab
    sh_addr:      0x0             sh_flags:   0
    sh_size:      0x1e865         sh_type:    [ SHT_STRTAB ]
    sh_offset:    0x21b5f0        sh_entsize: 0x0
    sh_link:      0               sh_info:    0
    sh_addralign: 0x1           

Symbol Table:  .symtab
     index    value       size     type bind oth shndx       name
       [0]  0x00000000 0x00000000  NOTY LOCL 0   UNDEF       
       [1]  0x00000000 0x00000000  SECT LOCL 0   .text       
       [2]  0x001973e8 0x00000000  SECT LOCL 0   .rodata     
       [3]  0x001c51a8 0x00000000  SECT LOCL 0   .data       
       [4]  0x001d86b0 0x00000000  SECT LOCL 0   .bss        
       [5]  0x002149a4 0x00000000  SECT LOCL 0   .ident      
       [6]  0x00000000 0x00000000  SECT LOCL 0   .shstrtab   
       [7]  0x00000000 0x00000000  SECT LOCL 0   .symtab     
       [8]  0x00000000 0x00000000  SECT LOCL 0   .strtab     



	My questions:


1) ELF executables have two ways of describing executable stuff:
   Program Headers and Section Headers. loadfile.c parses all of
   the Program Headers, and some of the Section Headers (for SYMs).

   Can anyone think of a reason why it would not simply parse all
   of the Section Headers, and only process the ones that were
   relevant to loading an executable?

   (It would make the code a _lot_ simpler)

2) Is there any clearer ELF grokking code in NetBSD land?

3) It seems that there are disparities between the sizes in the
   Program Headers and the Section Headers.

   e.g. Section Header 3 (.data)  ->  sh_size = 79100
	Section Header 4 (.bss)   ->  sh_size = 246516
						------
				        Total = 325616
						======

        Program Header 2 	  ->  p_memsz = 325628


   Looking at the sh_addr fields, the .bss section actually
   starts 12 bytes after it needs to. It is not on an 8K page
   alignment, though.

   Does anyone know why the BSS section would need to be aligned
   with 12 bytes of padding?



	Thanks in advance for any help you may be able to offer.


-- 
| Nigel Pearson, nigel@ind.tansu.com.au | "Reality is that which,   |
|   Telstra NW-D, Sydney, Australia.    |  when you stop believing  |
| Office: 9206 3468    Fax:  9212 6329  |  in it, doesn't go away." |
| Mobile: 0408 664435  Home: 9792 6998  | Philip K. Dick - 'Valis.' |