This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Reassign boundaries in Program Headers after offset calculation
AbandonedPublic

Authored by andcarminati on Jun 13 2023, 7:22 AM.

Details

Summary

With this change, FileSiz and MemSiz can be calculated correctly in the case of out-of-order output sections.

Diff Detail

Event Timeline

andcarminati created this revision.Jun 13 2023, 7:22 AM
Herald added a project: Restricted Project. · View Herald Transcript
andcarminati requested review of this revision.EditedJun 13 2023, 7:22 AM

Some background related to this proposal:

The following program:

.section data,"wa",@nobits
.zero 1
.section .second_in_section,"ax",@progbits
.zero 0x20
.section .first_in_section,"ax",@progbits
.zero 1
.globl _start
_start:

When linked with this script:

SECTIONS {
  second_section (0x10000000 +64) : { KEEP (*(.second_in_section)); } > mem
  first_section 0x10000000 : { KEEP (*(.first_in_section)); } > mem
  /DISCARD/ : { KEEP (*(.text, data .comment, .symtab, .strtab)); }
}

MEMORY { mem (r): org = 0x10000000, len = 0x00100000 }

Generates the follwing result (llvm-readelf -l):

Elf file type is EXEC (Executable file)
Entry point 0x10000001
There are 2 program headers, starting at offset 64

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  LOAD           0x001040 0x0000000010000040 0x0000000010000040 0xffffffffffffffc1 0xffffffffffffffc1 R E 0x1000
  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x0

 Section to Segment mapping:
  Segment Sections...
   00     
   01     
   None   second_section first_section .shstrtab

This overflow occurs because the first and last sections are not handled correctly in all phases of the linking.

This comment was removed by MaskRay.
andcarminati abandoned this revision.Jun 19 2023, 12:27 AM

Under further investigation.