I think the problem here is you are using a linker script.

You are creating a new class of binary, with different layouts and issues,
and since you are doing it on your own, you'll never know what you are
missing until later.

The linker script stuff is fragile, poorly undocumented stuff which changes
constantly between releases, has big variance between clang and gnu toolchains,
and if you don't have to do it you shouldn't.

We only use it for kernels, bootblocks, and ld.so.  And in all those cases,
we need a linker script *PER ARCHITECTURE*.

I think you'll never get it perfect for 1 architecture, and certainly not
for 4 architectures.


Lorenz (xha) <m...@xha.li> wrote:

> On Mon, Oct 09, 2023 at 01:29:52PM -0700, Philip Guenther wrote:
> > On Mon, Oct 9, 2023 at 11:21 AM Lorenz (xha) <m...@xha.li> wrote:
> > 
> > > hi misc@,
> > >
> > > i'm currently porting the hare programming language to openbsd and i am
> > > having quite a few problems trying to use a linker script. i am always
> > > getting a "/bin/ksh: .bin/hare: Invalid argument" error.
> > >
> > > so far i tried a lot of stuff like comparing a working version without a
> > > linker script, looking if any of the programm headers are missing, etc.
> > >
> > 
> > So you have a working binary (w/o linker script) and a not-working binary
> > (w/linker script) and you've even done the comparison of the program
> > headers of the two...and you're not going to show those but rather ask
> > what, in general, could be wrong?  Oooookay.
> 
> i am pretty sure that it woudln't have been helpful since i don't know
> what i am doing (yet). nothing's working
> 
> > Read /usr/src/sys/kern/*exec* and review the logic around the 10
> > occurrences of EINVAL in that code.  Presumably the differences you
> > identified will point to one or more of them
> 
> found it: PT_PHDRS is missing. i didn't identify that difference at
> first tho. it's needeed for PIE if i understand correctly.
> 
> why is ld not adding a PT_PHDR programm header? as far as i undestand,
> PT_PHDR are the programm headers themselfs?
> 
> this is my linker script (kind of mess right now because of debugging).
> it is supposed to link with libc. i am moving the init functions in a
> different section so that the hare runtime can execute them and not
> libc. that should have nothing to do with the problems i am having,
> however. am i missing something?
> 
> ```
> ENTRY(__start)
> SECTIONS {
>       .text : {
>               KEEP (*(.text))
>               *(.text.*)
>       }
> 
>       .data : {
>               KEEP (*(.data))
>               *(.data.*)
>       }
> 
>       .init_array : {
>               PROVIDE_HIDDEN (__init_array_start = .);
>               KEEP (*(.init_array))
>               PROVIDE_HIDDEN (__init_array_end = .);
>       }
> 
>       .fini_array : {
>               PROVIDE_HIDDEN (__fini_array_start = .);
>               KEEP (*(.fini_array))
>               PROVIDE_HIDDEN (__fini_array_end = .);
>       } :data
> 
>       .test_array : {
>               PROVIDE_HIDDEN (__test_array_start = .);
>               KEEP (*(.test_array))
>               PROVIDE_HIDDEN (__test_array_end = .);
>       } :data
> 
>       .note.openbsd.ident : {
>               KEEP (*(.note.openbsd.ident))
>               *(.note.openbsd.*)
>       }
> 
>       .bss : {
>               KEEP (*(.bss))
>               *(.bss.*)
>       }
> }
> ```
> 
> this is the readelf --headers of the programm produced with the linker
> script:
> 
> ```
> Program Headers:
>   Type           Offset             VirtAddr           PhysAddr
>                  FileSiz            MemSiz              Flags  Align
>   INTERP         0x0000000000001000 0x0000000000000000 0x0000000000000000
>                  0x0000000000000013 0x0000000000000013  R      1
>       [Requesting program interpreter: /usr/libexec/ld.so]
>   LOAD           0x0000000000001000 0x0000000000000000 0x0000000000000000
>                  0x000000000000b47c 0x000000000000b47c  R      1000
>   LOAD           0x000000000000c480 0x000000000000b480 0x000000000000b480
>                  0x00000000001356d0 0x00000000001356d0    E    1000
>   LOAD           0x0000000000141b50 0x0000000000140b50 0x0000000000140b50
>                  0x00000000000211e8 0x00000000000211e8  RW     1000
>   LOAD           0x0000000000162d38 0x0000000000161d38 0x0000000000161d38
>                  0x0000000000000018 0x0000000000000018  R      1000
>   LOAD           0x0000000000162d50 0x0000000000161d50 0x0000000000161d50
>                  0x0000000000000000 0x00000000000009b0  RW     1000
>   DYNAMIC        0x0000000000162ab0 0x0000000000161ab0 0x0000000000161ab0
>                  0x0000000000000160 0x0000000000000160  RW     8
>   GNU_RELRO      0x0000000000162a40 0x0000000000161a40 0x0000000000161a40
>                  0x00000000000002f8 0x00000000000005c0  R      1
>   GNU_EH_FRAME   0x000000000000c390 0x000000000000b390 0x000000000000b390
>                  0x000000000000002c 0x000000000000002c  R      4
>   OPENBSD_RANDOM 0x0000000000162aa8 0x0000000000161aa8 0x0000000000161aa8
>                  0x0000000000000008 0x0000000000000008  RW     8
>   GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
>                  0x0000000000000000 0x0000000000000000  RW     0
>   OPENBSD_NOBTCF 0x0000000000000000 0x0000000000000000 0x0000000000000000
>                  0x0000000000000000 0x0000000000000000    E    0
>   NOTE           0x0000000000162d38 0x0000000000161d38 0x0000000000161d38
>                  0x0000000000000018 0x0000000000000018  R      4
> 
>  Section to Segment mapping:
>   Segment Sections...
>    00     .interp
>    01     .interp .dynsym .gnu.hash .hash .dynstr .rela.dyn .rela.plt .rodata 
> .eh_frame_hdr .eh_frame
>    02     .text .init .fini .plt
>    03     .data .openbsd.randomdata.retguard.1205 
> .openbsd.randomdata.retguard.2473 .openbsd.randomdata.retguard.1471 
> .openbsd.randomdata.retguard.1773 .init_array .fini_array .jcr .ctors .dtors 
> .openbsd.randomdata .dynamic .got .got.plt
>    04     .note.openbsd.ident
>    05     .bss
>    06     .dynamic
>    07     .init_array .fini_array .jcr .ctors .dtors .openbsd.randomdata 
> .dynamic .got .got.plt .note.openbsd.ident
>    08     .eh_frame_hdr
>    09     .openbsd.randomdata
>    10
>    11
>    12     .note.openbsd.ident
> ```
> 
> and without the linker script, but this is segfaulting because libc is
> running the @init functions:
> 
> ```
> Program Headers:
>   Type           Offset             VirtAddr           PhysAddr
>                  FileSiz            MemSiz              Flags  Align
>   PHDR           0x0000000000000040 0x0000000000000040 0x0000000000000040
>                  0x00000000000002d8 0x00000000000002d8  R      8
>   INTERP         0x0000000000000318 0x0000000000000318 0x0000000000000318
>                  0x0000000000000013 0x0000000000000013  R      1
>       [Requesting program interpreter: /usr/libexec/ld.so]
>   LOAD           0x0000000000000000 0x0000000000000000 0x0000000000000000
>                  0x000000000000b7dc 0x000000000000b7dc  R      1000
>   LOAD           0x000000000000b7e0 0x000000000000c7e0 0x000000000000c7e0
>                  0x00000000001356e0 0x00000000001356e0    E    1000
>   LOAD           0x0000000000140ec0 0x0000000000142ec0 0x0000000000142ec0
>                  0x0000000000000318 0x0000000000000318  RW     1000
>   LOAD           0x00000000001411d8 0x00000000001441d8 0x00000000001441d8
>                  0x0000000000020ed0 0x0000000000021880  RW     1000
>   DYNAMIC        0x0000000000140f50 0x0000000000142f50 0x0000000000142f50
>                  0x0000000000000160 0x0000000000000160  RW     8
>   GNU_RELRO      0x0000000000140ec0 0x0000000000142ec0 0x0000000000142ec0
>                  0x0000000000000318 0x0000000000001140  R      1
>   GNU_EH_FRAME   0x000000000000b6f0 0x000000000000b6f0 0x000000000000b6f0
>                  0x000000000000002c 0x000000000000002c  R      4
>   OPENBSD_RANDOM 0x0000000000140ec0 0x0000000000142ec0 0x0000000000142ec0
>                  0x0000000000000028 0x0000000000000028  RW     8
>   GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
>                  0x0000000000000000 0x0000000000000000  RW     0
>   OPENBSD_NOBTCF 0x0000000000000000 0x0000000000000000 0x0000000000000000
>                  0x0000000000000000 0x0000000000000000    E    0
>   NOTE           0x000000000000032c 0x000000000000032c 0x000000000000032c
>                  0x0000000000000018 0x0000000000000018  R      4
> 
>  Section to Segment mapping:
>   Segment Sections...
>    00
>    01     .interp
>    02     .interp .note.openbsd.ident .dynsym .gnu.hash .hash .dynstr 
> .rela.dyn .rela.plt .rodata .eh_frame_hdr .eh_frame
>    03     .text .init .fini .plt
>    04     .openbsd.randomdata .jcr .ctors .dtors .fini_array .init_array 
> .dynamic .got .got.plt
>    05     .data .bss
>    06     .dynamic
>    07     .openbsd.randomdata .jcr .ctors .dtors .fini_array .init_array 
> .dynamic .got .got.plt
>    08     .eh_frame_hdr
>    09     .openbsd.randomdata
>    10
>    11
>    12     .note.openbsd.ident
> ```
> 

Reply via email to