Constructors, library order, and portability

Programming, for all ages and all languages.
Post Reply
nullplan
Member
Member
Posts: 1769
Joined: Wed Aug 30, 2017 8:24 am

Re: Constructors, library order, and portability

Post by nullplan »

Officially, initializer order is undefined. Unofficially, initializers are run in a depth-first way (dependencies first), at least on glibc and musl. What happens on Windows is determined by the dynamic linker in use, which I don't know.

How about, instead of using __attribute__((constructor)) and DT_INIT or DT_INIT_ARRAY, you generate your own initializer list, establish whatever order on it, and run it first thing from your language runtime. That way, the C library will be fully initialized already, and you will be in full control of the process.
Carpe diem!
User avatar
~
Member
Member
Posts: 1226
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Re: Constructors, library order, and portability

Post by ~ »

The core of libraries should have independent order.

It's the critical system functions you need that rely on each other to make things possible, and even then the cores are stand-alone. If you call stuff from other subsystems, the code must be clean/portable to make for stand-alone pieces with no particular order other than what you will be accessing in a logical, functional way.

-----------------------------------------------------------
For example if you need the disk you need the base system, user input, some data buffers, memory management at least with malloc/free, a sample partition with files, an unfragmented partition with at least 1 file to modify.

You can gradually add functions to those components in order as you see you need to "install" more functions for your system resources to function properly.
YouTube:
http://youtube.com/@AltComp126

My x86 emulator/kernel project and software tools/documentation:
http://master.dl.sourceforge.net/projec ... 7z?viasf=1
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Constructors, library order, and portability

Post by Korona »

In the SysV ABI, initializers (DT_INIT and DT_INIT_ARRAY) in a library run in unspecified order (use the static initializer idiom or pthread_once() to order them). Initializers in different libraries, however, are specified to run in topological order. In other words, a lib is initialized after all of its dependencies are already initialized.

EDIT: Because you asked for a reference: section 5-22 ("Initialization and Termination functions") of the (incomplete) PDF version http://www.sco.com/developers/devspecs/gabi41.pdf of the SysV specification (the HTML version is more verbose in general).
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
User avatar
~
Member
Member
Posts: 1226
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Re: Constructors, library order, and portability

Post by ~ »

Since it's a compiler tool you are making yourself, you can make it manually configurable in ways that match what you need for your own projects.

Test what works for you and for existing systems, and it will surely end up optimum.
YouTube:
http://youtube.com/@AltComp126

My x86 emulator/kernel project and software tools/documentation:
http://master.dl.sourceforge.net/projec ... 7z?viasf=1
Post Reply