rems-project / linksem

Semantic model for aspects of ELF static linking and DWARF debug information
Other
43 stars 7 forks source link

Elf_section_header_table: fix section ordering #1

Closed emersion closed 6 years ago

emersion commented 6 years ago

I believe the order of ELF sections in elf_memory_image_of_elf64_file is reversed. Here is a readelf dump of the object file:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .text             PROGBITS         0000000000000000  00000040
       0000000000000012  0000000000000000  AX       0     0     1
  [ 2] .data             PROGBITS         0000000000000000  00000052
       0000000000000000  0000000000000000  WA       0     0     1
  [ 3] .bss              NOBITS           0000000000000000  00000052
       0000000000000000  0000000000000000  WA       0     0     1
  [ 4] .comment          PROGBITS         0000000000000000  00000052
       000000000000001b  0000000000000001  MS       0     0     1
  [ 5] .note.GNU-stack   PROGBITS         0000000000000000  0000006d
       0000000000000000  0000000000000000           0     0     1
  [ 6] .eh_frame         PROGBITS         0000000000000000  00000070
       0000000000000038  0000000000000000   A       0     0     8
  [ 7] .rela.eh_frame    RELA             0000000000000000  00000190
       0000000000000018  0000000000000018   I       8     6     8
  [ 8] .symtab           SYMTAB           0000000000000000  000000a8
       00000000000000d8  0000000000000018           9     8     8
  [ 9] .strtab           STRTAB           0000000000000000  00000180
       000000000000000e  0000000000000000           0     0     1
  [10] .shstrtab         STRTAB           0000000000000000  000001a8
       0000000000000054  0000000000000000           0     0     1

And here is what elf_memory_image_of_elf64_file gets (i is the index of the section, its name is at the end of the line):

i: 0 elf64_section_name: 17 elf64_section_type: 3 elf64_section_flags: 0 elf64_section_addr: 0 elf64_section_offset: 249 elf64_section_size: 72 elf64_section_link: 0 elf64_section_info: 0 elf64_section_align: 1 elf64_section_entsize: 0 elf64_section_name_as_string: .shstrtab
i: 1 elf64_section_name: 9 elf64_section_type: 3 elf64_section_flags: 0 elf64_section_addr: 0 elf64_section_offset: 248 elf64_section_size: 1 elf64_section_link: 0 elf64_section_info: 0 elf64_section_align: 1 elf64_section_entsize: 0 elf64_section_name_as_string: .strtab
i: 2 elf64_section_name: 1 elf64_section_type: 2 elf64_section_flags: 0 elf64_section_addr: 0 elf64_section_offset: 80 elf64_section_size: 168 elf64_section_link: 8 elf64_section_info: 7 elf64_section_align: 8 elf64_section_entsize: 24 elf64_section_name_as_string: .symtab
i: 3 elf64_section_name: 56 elf64_section_type: 1 elf64_section_flags: 0 elf64_section_addr: 0 elf64_section_offset: 74 elf64_section_size: 0 elf64_section_link: 0 elf64_section_info: 0 elf64_section_align: 1 elf64_section_entsize: 0 elf64_section_name_as_string: .note.GNU-stack
i: 4 elf64_section_name: 50 elf64_section_type: 1 elf64_section_flags: 6 elf64_section_addr: 0 elf64_section_offset: 69 elf64_section_size: 5 elf64_section_link: 0 elf64_section_info: 0 elf64_section_align: 1 elf64_section_entsize: 0 elf64_section_name_as_string: .fini
i: 5 elf64_section_name: 44 elf64_section_type: 1 elf64_section_flags: 6 elf64_section_addr: 0 elf64_section_offset: 64 elf64_section_size: 5 elf64_section_link: 0 elf64_section_info: 0 elf64_section_align: 1 elf64_section_entsize: 0 elf64_section_name_as_string: .init
i: 6 elf64_section_name: 39 elf64_section_type: 8 elf64_section_flags: 3 elf64_section_addr: 0 elf64_section_offset: 64 elf64_section_size: 0 elf64_section_link: 0 elf64_section_info: 0 elf64_section_align: 1 elf64_section_entsize: 0 elf64_section_name_as_string: .bss
i: 7 elf64_section_name: 33 elf64_section_type: 1 elf64_section_flags: 3 elf64_section_addr: 0 elf64_section_offset: 64 elf64_section_size: 0 elf64_section_link: 0 elf64_section_info: 0 elf64_section_align: 1 elf64_section_entsize: 0 elf64_section_name_as_string: .data
i: 8 elf64_section_name: 27 elf64_section_type: 1 elf64_section_flags: 6 elf64_section_addr: 0 elf64_section_offset: 64 elf64_section_size: 0 elf64_section_link: 0 elf64_section_info: 0 elf64_section_align: 1 elf64_section_entsize: 0 elf64_section_name_as_string: .text
i: 9 elf64_section_name: 0 elf64_section_type: 0 elf64_section_flags: 0 elf64_section_addr: 0 elf64_section_offset: 0 elf64_section_size: 0 elf64_section_link: 0 elf64_section_info: 0 elf64_section_align: 0 elf64_section_entsize: 0 elf64_section_name_as_string:

Since the first section is expected to be null, the function fails. Here, the last section is the null one.

I tried not to use append but I failed to define an auxiliary, private, recursive helper function that builds the reversed list. Defining a recursive function inside another function doesn't seem to work and defining a recursive helper function above seems to require the helper function to be public. Let me know if there's a better way to do this.

Once again, there's some noise due to trailing whitespace.