OSDev.org
https://forum.osdev.org/

Constructors, library order, and portability
https://forum.osdev.org/viewtopic.php?f=13&t=33745
Page 1 of 1

Author:  nullplan [ Thu Jun 27, 2019 11:41 am ]
Post subject:  Re: Constructors, library order, and portability

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.

Author:  ~ [ Thu Jun 27, 2019 11:50 am ]
Post subject:  Re: Constructors, library order, and portability

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.

Author:  Korona [ Thu Jun 27, 2019 3:46 pm ]
Post subject:  Re: Constructors, library order, and portability

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).

Author:  ~ [ Sat Jun 29, 2019 9:26 am ]
Post subject:  Re: Constructors, library order, and portability

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.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/