Opened 5 years ago

Closed 2 years ago

#15743 closed bug (fixed)

implement dl_iterate_phdr

Reported by: X512 Owned by: jessicah
Priority: normal Milestone: R1/beta4
Component: System/POSIX Version: R1/Development
Keywords: Cc:
Blocked By: Blocking:
Platform: All

Description (last modified by X512)

This is hrev53903.

dl_iterate_phdr is required for Mesa graphics drivers.

Required declarations are already present at https://git.haiku-os.org/haiku/tree/headers/compatibility/bsd/sys/link_elf.h.

Change History (15)

comment:1 by return_0e, 5 years ago

But dl_iterate_phdr isn't part of the POSIX standard. If we do end up implementing this, We probably would need to place the implementation under src/libs/bsd rather than in libroot.

comment:2 by waddlesplash, 5 years ago

The dl_iterate_phdr() function allows an application to inquire at run time to find out which shared objects it has loaded.

I think you can rather easily implement this using Haiku's get_next_image_info. Indeed, it sounds like a good candidate to put into libbsd.

Musl's implementation, for reference: https://github.com/rofl0r/musl/blob/master/src/ldso/dl_iterate_phdr.c

comment:3 by X512, 4 years ago

Also needed for LLVM libunwind.

Version 0, edited 4 years ago by X512 (next)

comment:4 by X512, 4 years ago

I think you can rather easily implement this using Haiku's get_next_image_info.

No, it can't. dl_iterate_phdr allows go get image base and ELF program header table.

comment:5 by X512, 4 years ago

runtime_loader currently don't save ELF header and program header table in image_t. image_t should be changed or dl_iterate_phdr implementation should read ELF file on each call.

Also image_t have no meaningful image base. It use regions that can be loaded at arbitrary addresses, possibly in different order than described in program header table. So formula addr == info->dlpi_addr + info->dlpi_phdr[x].p_vaddr; described in documentation may not work.

Last edited 4 years ago by X512 (previous) (diff)

comment:6 by X512, 4 years ago

Description: modified (diff)

comment:7 by nielx, 4 years ago

Why does Mesa need it?

(I thought I needed it for Rust's backtrace-rs as well, but for the actual use I could use the image_info)

comment:8 by jessicah, 3 years ago

Owner: changed from nobody to jessicah
Status: newassigned

I'm going to give a go at implementing this, as libunwind is a needed dependency for my .Net porting effort, and trying to make libunwind work without it is proving troublesome.

comment:9 by hoanga, 3 years ago

When this implementation is in a ready enough state, I think it would help simplify some parts of the port of Zig to Haiku.

in reply to:  2 comment:10 by trungnt2910, 2 years ago

Replying to waddlesplash:

The dl_iterate_phdr() function allows an application to inquire at run time to find out which shared objects it has loaded.

I think you can rather easily implement this using Haiku's get_next_image_info. Indeed, it sounds like a good candidate to put into libbsd.

Musl's implementation, for reference: https://github.com/rofl0r/musl/blob/master/src/ldso/dl_iterate_phdr.c

It is quite easy to implement using Haiku's get_next_image_info, if you ignore all the TLS-related fields (these fields are not available in, for example, early versions of Linux). However, because Haiku's struct dl_phdr_info, declared here: https://git.haiku-os.org/haiku/tree/headers/compatibility/bsd/sys/link_elf.h#n85 declares all the fields, a complete dl_iterate_phdr implementation would need to fill the last 4 fields as well.

In order to do this, we'll need some help from runtime_loader. Is adding something to __gRuntimeLoader acceptable?

comment:11 by waddlesplash, 2 years ago

Yes, that's private API and we can change it as we want to.

Alternatively, how important are the fields? We can rewrite that header to be a Haiku-native one instead of being imported from BSD.

in reply to:  11 comment:12 by trungnt2910, 2 years ago

Replying to waddlesplash:

Yes, that's private API and we can change it as we want to.

Alternatively, how important are the fields? We can rewrite that header to be a Haiku-native one instead of being imported from BSD.

The last 4 fields? Not too important for my personal use case (libunwind just need the ELF headers), but for others that might be consuming the API? I don't know.

comment:13 by waddlesplash, 2 years ago

Well, perhaps we should just implement the "lesser" API and deal with the "greater" one later if we really need it.

comment:14 by trungnt2910, 2 years ago

comment:15 by waddlesplash, 2 years ago

Milestone: UnscheduledR1/beta4
Resolution: fixed
Status: assignedclosed

Done in hrev56227.

Note: See TracTickets for help on using tickets.