5959#define EHDR_SIZE 64
6060#define PHDR_SIZE 56
6161#define SHDR_SIZE 64
62+ #define PAGE_SIZE 0x1000
6263
6364/* ── Plan9 etype values (APExp cc.h with TBOOL=3 inserted) ────────────── */
6465#define P9_TVOID 0
@@ -401,9 +402,22 @@ int main(int argc, char *argv[])
401402 data_vaddr = (text_vaddr + textsize + AOUT_INITRND - 1 )
402403 & ~(AOUT_INITRND - 1 );
403404
404- /* ── ELF file offset layout ── */
405- text_foff = EHDR_SIZE + 2 * PHDR_SIZE ; /* 176 */
406- data_foff = text_foff + (long )textsize ;
405+ /* ── ELF file offset layout ──
406+ * ELF requires p_offset ≡ p_vaddr (mod p_align).
407+ * Pad after the ELF headers so text_foff ≡ text_vaddr (mod PAGE_SIZE),
408+ * then pad between text and data for the same constraint on data. */
409+ {
410+ long base = EHDR_SIZE + 2 * PHDR_SIZE ; /* 176 */
411+ long tmod = (long )(text_vaddr % PAGE_SIZE );
412+ long cmod = base % PAGE_SIZE ;
413+ text_foff = base + ((tmod - cmod + PAGE_SIZE ) % PAGE_SIZE );
414+ }
415+ {
416+ long base = text_foff + (long )textsize ;
417+ long dmod = (long )(data_vaddr % PAGE_SIZE );
418+ long cmod = base % PAGE_SIZE ;
419+ data_foff = base + ((dmod - cmod + PAGE_SIZE ) % PAGE_SIZE );
420+ }
407421
408422 /* ── DWARF generation ── */
409423 /* sections 0=NULL 1=.text 2=.data 3=.bss → DWARF starts at 4 */
@@ -416,12 +430,15 @@ int main(int argc, char *argv[])
416430 int res ;
417431
418432 res = dwarf_producer_init (
419- DW_DLC_POINTER64 | DW_DLC_TARGET_LITTLEENDIAN ,
433+ DW_DLC_POINTER64 | DW_DLC_TARGET_LITTLEENDIAN |
434+ DW_DLC_SYMBOLIC_RELOCATIONS ,
420435 dw_section_callback , NULL , NULL , NULL ,
421436 "x86_64" , "V4" , "" ,
422437 & dbg , & derr );
423438
424- if (res == DW_DLV_OK ) {
439+ if (res != DW_DLV_OK ) {
440+ fprintf (stderr , "dw2elf: dwarf_producer_init failed\n" );
441+ } else {
425442 for (i = 0 ; i < nsidecars ; i ++ ) {
426443 size_t sclen ;
427444 uint8_t * scbuf = loadfile (sidecars [i ], & sclen );
@@ -500,14 +517,19 @@ int main(int argc, char *argv[])
500517 /* ── program headers ── */
501518 write_phdr (ofd , PT_LOAD , PF_R |PF_X ,
502519 (uint64_t )text_foff , text_vaddr ,
503- textsize , textsize , AOUT_INITRND );
520+ textsize , textsize , PAGE_SIZE );
504521 write_phdr (ofd , PT_LOAD , PF_R |PF_W ,
505522 (uint64_t )data_foff , data_vaddr ,
506- datsize , (uint64_t )datsize + bsssize , AOUT_INITRND );
523+ datsize , (uint64_t )datsize + bsssize , PAGE_SIZE );
507524
508- /* ── text and data ── */
525+ /* ── padding + text + padding + data ── */
526+ cur = (long )(EHDR_SIZE + 2 * PHDR_SIZE );
527+ while (cur < text_foff ) { xwrite (ofd , & zero , 1 ); cur ++ ; }
509528 xwrite (ofd , aout_buf + AOUT_HEADR , textsize );
529+ cur += (long )textsize ;
530+ while (cur < data_foff ) { xwrite (ofd , & zero , 1 ); cur ++ ; }
510531 xwrite (ofd , aout_buf + AOUT_HEADR + textsize , datsize );
532+ cur += (long )datsize ;
511533
512534 /* ── DWARF sections ── */
513535 for (i = 0 ; i < n_dw_sects ; i ++ ) {
0 commit comments