| 1 |
/* do not edit automatically generated by mc from M2Dependent. */ |
| 2 |
/* M2Dependent.mod implements the run time module dependencies. |
| 3 |
|
| 4 |
Copyright (C) 2022-2025 Free Software Foundation, Inc. |
| 5 |
Contributed by Gaius Mulley <[email protected]>. |
| 6 |
|
| 7 |
This file is part of GNU Modula-2. |
| 8 |
|
| 9 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 3, or (at your option) |
| 12 |
any later version. |
| 13 |
|
| 14 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
General Public License for more details. |
| 18 |
|
| 19 |
Under Section 7 of GPL version 3, you are granted additional |
| 20 |
permissions described in the GCC Runtime Library Exception, version |
| 21 |
3.1, as published by the Free Software Foundation. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License and |
| 24 |
a copy of the GCC Runtime Library Exception along with this program; |
| 25 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 26 |
<http://www.gnu.org/licenses/>. */ |
| 27 |
|
| 28 |
#include <stdbool.h> |
| 29 |
# if !defined (PROC_D) |
| 30 |
# define PROC_D |
| 31 |
typedef void (*PROC_t) (void); |
| 32 |
typedef struct { PROC_t proc; } PROC; |
| 33 |
# endif |
| 34 |
|
| 35 |
# if !defined (TRUE) |
| 36 |
# define TRUE (1==1) |
| 37 |
# endif |
| 38 |
|
| 39 |
# if !defined (FALSE) |
| 40 |
# define FALSE (1==0) |
| 41 |
# endif |
| 42 |
|
| 43 |
#include <stddef.h> |
| 44 |
#include <string.h> |
| 45 |
#include <limits.h> |
| 46 |
#include <stdlib.h> |
| 47 |
# include "GStorage.h" |
| 48 |
#include <unistd.h> |
| 49 |
#if defined(__cplusplus) |
| 50 |
# undef NULL |
| 51 |
# define NULL 0 |
| 52 |
#endif |
| 53 |
#define _M2Dependent_C |
| 54 |
|
| 55 |
#include "GM2Dependent.h" |
| 56 |
# include "Glibc.h" |
| 57 |
# include "GASCII.h" |
| 58 |
# include "GSYSTEM.h" |
| 59 |
# include "GStorage.h" |
| 60 |
# include "GStrLib.h" |
| 61 |
|
| 62 |
typedef struct M2Dependent_ArgCVEnvP_p M2Dependent_ArgCVEnvP; |
| 63 |
|
| 64 |
typedef struct M2Dependent_DependencyList_r M2Dependent_DependencyList; |
| 65 |
|
| 66 |
typedef char *M2Dependent_PtrToChar; |
| 67 |
|
| 68 |
typedef struct M2Dependent_ProcedureList_r M2Dependent_ProcedureList; |
| 69 |
|
| 70 |
typedef struct M2Dependent__T2_r M2Dependent__T2; |
| 71 |
|
| 72 |
typedef M2Dependent__T2 *M2Dependent_ModuleChain; |
| 73 |
|
| 74 |
typedef struct M2Dependent__T3_r M2Dependent__T3; |
| 75 |
|
| 76 |
typedef M2Dependent__T3 *M2Dependent_ProcedureChain; |
| 77 |
|
| 78 |
typedef struct M2Dependent__T4_a M2Dependent__T4; |
| 79 |
|
| 80 |
typedef enum {M2Dependent_unregistered, M2Dependent_unordered, M2Dependent_started, M2Dependent_ordered, M2Dependent_user} M2Dependent_DependencyState; |
| 81 |
|
| 82 |
struct M2Dependent_DependencyList_r { |
| 83 |
PROC proc; |
| 84 |
bool forced; |
| 85 |
bool forc; |
| 86 |
bool appl; |
| 87 |
M2Dependent_DependencyState state; |
| 88 |
}; |
| 89 |
|
| 90 |
struct M2Dependent_ProcedureList_r { |
| 91 |
M2Dependent_ProcedureChain head; |
| 92 |
M2Dependent_ProcedureChain tail; |
| 93 |
}; |
| 94 |
|
| 95 |
struct M2Dependent__T3_r { |
| 96 |
PROC p; |
| 97 |
M2Dependent_ProcedureChain prev; |
| 98 |
M2Dependent_ProcedureChain next; |
| 99 |
}; |
| 100 |
|
| 101 |
struct M2Dependent__T4_a { M2Dependent_ModuleChain array[M2Dependent_user-M2Dependent_unregistered+1]; }; |
| 102 |
struct M2Dependent__T2_r { |
| 103 |
void *name; |
| 104 |
void *libname; |
| 105 |
M2Dependent_ArgCVEnvP init; |
| 106 |
M2Dependent_ArgCVEnvP fini; |
| 107 |
M2Dependent_DependencyList dependency; |
| 108 |
M2Dependent_ModuleChain prev; |
| 109 |
M2Dependent_ModuleChain next; |
| 110 |
}; |
| 111 |
|
| 112 |
static M2Dependent__T4 Modules; |
| 113 |
static bool DynamicInitialization; |
| 114 |
static bool Initialized; |
| 115 |
static bool WarningTrace; |
| 116 |
static bool ModuleTrace; |
| 117 |
static bool HexTrace; |
| 118 |
static bool DependencyTrace; |
| 119 |
static bool PreTrace; |
| 120 |
static bool PostTrace; |
| 121 |
static bool ForceTrace; |
| 122 |
static M2Dependent_ProcedureList InitialProc; |
| 123 |
static M2Dependent_ProcedureList TerminateProc; |
| 124 |
|
| 125 |
/* |
| 126 |
ConstructModules - resolve dependencies and then call each |
| 127 |
module constructor in turn. |
| 128 |
*/ |
| 129 |
|
| 130 |
extern "C" void M2Dependent_ConstructModules (void * applicationmodule, void * libname, void * overrideliborder, int argc, void * argv, void * envp); |
| 131 |
|
| 132 |
/* |
| 133 |
DeconstructModules - resolve dependencies and then call each |
| 134 |
module constructor in turn. |
| 135 |
*/ |
| 136 |
|
| 137 |
extern "C" void M2Dependent_DeconstructModules (void * applicationmodule, void * libname, int argc, void * argv, void * envp); |
| 138 |
|
| 139 |
/* |
| 140 |
RegisterModule - adds module name to the list of outstanding |
| 141 |
modules which need to have their dependencies |
| 142 |
explored to determine initialization order. |
| 143 |
*/ |
| 144 |
|
| 145 |
extern "C" void M2Dependent_RegisterModule (void * modulename, void * libname, M2Dependent_ArgCVEnvP init, M2Dependent_ArgCVEnvP fini, PROC dependencies); |
| 146 |
|
| 147 |
/* |
| 148 |
RequestDependant - used to specify that modulename is dependant upon |
| 149 |
module dependantmodule. It only takes effect |
| 150 |
if we are using DynamicInitialization. |
| 151 |
*/ |
| 152 |
|
| 153 |
extern "C" void M2Dependent_RequestDependant (void * modulename, void * libname, void * dependantmodule, void * dependantlibname); |
| 154 |
|
| 155 |
/* |
| 156 |
InstallTerminationProcedure - installs a procedure, p, which will |
| 157 |
be called when the procedure |
| 158 |
ExecuteTerminationProcedures |
| 159 |
is invoked. It returns TRUE if the |
| 160 |
procedure is installed. |
| 161 |
*/ |
| 162 |
|
| 163 |
extern "C" bool M2Dependent_InstallTerminationProcedure (PROC p); |
| 164 |
|
| 165 |
/* |
| 166 |
ExecuteInitialProcedures - executes the initial procedures installed by |
| 167 |
InstallInitialProcedure. |
| 168 |
*/ |
| 169 |
|
| 170 |
extern "C" void M2Dependent_ExecuteInitialProcedures (void); |
| 171 |
|
| 172 |
/* |
| 173 |
InstallInitialProcedure - installs a procedure to be executed just |
| 174 |
before the BEGIN code section of the |
| 175 |
main program module. |
| 176 |
*/ |
| 177 |
|
| 178 |
extern "C" bool M2Dependent_InstallInitialProcedure (PROC p); |
| 179 |
|
| 180 |
/* |
| 181 |
ExecuteTerminationProcedures - calls each installed termination procedure |
| 182 |
in reverse order. |
| 183 |
*/ |
| 184 |
|
| 185 |
extern "C" void M2Dependent_ExecuteTerminationProcedures (void); |
| 186 |
|
| 187 |
/* |
| 188 |
InitDependencyList - initialize all fields of DependencyList. |
| 189 |
*/ |
| 190 |
|
| 191 |
static void InitDependencyList (M2Dependent_DependencyList *depList, PROC proc, M2Dependent_DependencyState state); |
| 192 |
|
| 193 |
/* |
| 194 |
CreateModule - creates a new module entry and returns the |
| 195 |
ModuleChain. |
| 196 |
*/ |
| 197 |
|
| 198 |
static M2Dependent_ModuleChain CreateModule (void * name, void * libname, M2Dependent_ArgCVEnvP init, M2Dependent_ArgCVEnvP fini, PROC dependencies); |
| 199 |
|
| 200 |
/* |
| 201 |
AppendModule - append chain to end of the list. |
| 202 |
*/ |
| 203 |
|
| 204 |
static void AppendModule (M2Dependent_ModuleChain *head, M2Dependent_ModuleChain chain); |
| 205 |
|
| 206 |
/* |
| 207 |
RemoveModule - remove chain from double linked list head. |
| 208 |
*/ |
| 209 |
|
| 210 |
static void RemoveModule (M2Dependent_ModuleChain *head, M2Dependent_ModuleChain chain); |
| 211 |
|
| 212 |
/* |
| 213 |
onChain - returns TRUE if mptr is on the Modules[state] list. |
| 214 |
*/ |
| 215 |
|
| 216 |
static bool onChain (M2Dependent_DependencyState state, M2Dependent_ModuleChain mptr); |
| 217 |
|
| 218 |
/* |
| 219 |
max - |
| 220 |
*/ |
| 221 |
|
| 222 |
static unsigned int max (unsigned int a, unsigned int b); |
| 223 |
|
| 224 |
/* |
| 225 |
min - |
| 226 |
*/ |
| 227 |
|
| 228 |
static unsigned int min (unsigned int a, unsigned int b); |
| 229 |
|
| 230 |
/* |
| 231 |
LookupModuleN - lookup module from the state list. |
| 232 |
The strings lengths are known. |
| 233 |
*/ |
| 234 |
|
| 235 |
static M2Dependent_ModuleChain LookupModuleN (M2Dependent_DependencyState state, void * name, unsigned int namelen, void * libname, unsigned int libnamelen); |
| 236 |
|
| 237 |
/* |
| 238 |
LookupModule - lookup and return the ModuleChain pointer containing |
| 239 |
module name from a particular list. |
| 240 |
*/ |
| 241 |
|
| 242 |
static M2Dependent_ModuleChain LookupModule (M2Dependent_DependencyState state, void * name, void * libname); |
| 243 |
|
| 244 |
/* |
| 245 |
toCString - replace any character sequence |
| 246 |
into a newline. |
| 247 |
*/ |
| 248 |
|
| 249 |
static void toCString (char *str, unsigned int _str_high); |
| 250 |
|
| 251 |
/* |
| 252 |
strcmp - return 0 if both strings are equal. |
| 253 |
We cannot use Builtins.def during bootstrap. |
| 254 |
*/ |
| 255 |
|
| 256 |
static int strcmp (M2Dependent_PtrToChar a, M2Dependent_PtrToChar b); |
| 257 |
|
| 258 |
/* |
| 259 |
strncmp - return 0 if both strings are equal. |
| 260 |
We cannot use Builtins.def during bootstrap. |
| 261 |
*/ |
| 262 |
|
| 263 |
static int strncmp (M2Dependent_PtrToChar a, M2Dependent_PtrToChar b, unsigned int n); |
| 264 |
|
| 265 |
/* |
| 266 |
strlen - returns the length of string. |
| 267 |
*/ |
| 268 |
|
| 269 |
static int strlen_ (M2Dependent_PtrToChar string); |
| 270 |
|
| 271 |
/* |
| 272 |
traceprintf - wrap printf with a boolean flag. |
| 273 |
*/ |
| 274 |
|
| 275 |
static void traceprintf (bool flag, const char *str_, unsigned int _str_high); |
| 276 |
|
| 277 |
/* |
| 278 |
traceprintf2 - wrap printf with a boolean flag. |
| 279 |
*/ |
| 280 |
|
| 281 |
static void traceprintf2 (bool flag, const char *str_, unsigned int _str_high, void * arg); |
| 282 |
|
| 283 |
/* |
| 284 |
traceprintf3 - wrap printf with a boolean flag. |
| 285 |
*/ |
| 286 |
|
| 287 |
static void traceprintf3 (bool flag, const char *str_, unsigned int _str_high, void * arg1, void * arg2); |
| 288 |
|
| 289 |
/* |
| 290 |
moveTo - moves mptr to the new list determined by newstate. |
| 291 |
It updates the mptr state appropriately. |
| 292 |
*/ |
| 293 |
|
| 294 |
static void moveTo (M2Dependent_DependencyState newstate, M2Dependent_ModuleChain mptr); |
| 295 |
|
| 296 |
/* |
| 297 |
ResolveDependant - |
| 298 |
*/ |
| 299 |
|
| 300 |
static void ResolveDependant (M2Dependent_ModuleChain mptr, void * currentmodule, void * libname); |
| 301 |
|
| 302 |
/* |
| 303 |
PerformRequestDependant - the current modulename has a dependancy upon |
| 304 |
dependantmodule. If dependantmodule is NIL then |
| 305 |
modulename has no further dependants and it can be |
| 306 |
resolved. |
| 307 |
*/ |
| 308 |
|
| 309 |
static void PerformRequestDependant (void * modulename, void * libname, void * dependantmodule, void * dependantlibname); |
| 310 |
|
| 311 |
/* |
| 312 |
ResolveDependencies - resolve dependencies for currentmodule, libname. |
| 313 |
*/ |
| 314 |
|
| 315 |
static void ResolveDependencies (void * currentmodule, void * libname); |
| 316 |
|
| 317 |
/* |
| 318 |
DisplayModuleInfo - displays all module in the state. |
| 319 |
*/ |
| 320 |
|
| 321 |
static void DisplayModuleInfo (M2Dependent_DependencyState state, const char *desc_, unsigned int _desc_high); |
| 322 |
|
| 323 |
/* |
| 324 |
DumpModuleData - |
| 325 |
*/ |
| 326 |
|
| 327 |
static void DumpModuleData (bool flag); |
| 328 |
|
| 329 |
/* |
| 330 |
combine - dest := src + dest. Places src at the front of list dest. |
| 331 |
Pre condition: src, dest are lists. |
| 332 |
Post condition : dest := src + dest |
| 333 |
src := NIL. |
| 334 |
*/ |
| 335 |
|
| 336 |
static void combine (M2Dependent_DependencyState src, M2Dependent_DependencyState dest); |
| 337 |
|
| 338 |
/* |
| 339 |
tracemodule - |
| 340 |
*/ |
| 341 |
|
| 342 |
static void tracemodule (bool flag, void * modname, unsigned int modlen, void * libname, unsigned int liblen); |
| 343 |
|
| 344 |
/* |
| 345 |
ForceModule - |
| 346 |
*/ |
| 347 |
|
| 348 |
static void ForceModule (void * modname, unsigned int modlen, void * libname, unsigned int liblen); |
| 349 |
|
| 350 |
/* |
| 351 |
ForceDependencies - if the user has specified a forced order then we override |
| 352 |
the dynamic ordering with the preference. |
| 353 |
*/ |
| 354 |
|
| 355 |
static void ForceDependencies (void * overrideliborder); |
| 356 |
|
| 357 |
/* |
| 358 |
CheckApplication - check to see that the application is the last entry in the list. |
| 359 |
This might happen if the application only imports FOR C modules. |
| 360 |
*/ |
| 361 |
|
| 362 |
static void CheckApplication (void); |
| 363 |
|
| 364 |
/* |
| 365 |
warning3 - write format arg1 arg2 to stderr. |
| 366 |
*/ |
| 367 |
|
| 368 |
static void warning3 (const char *format_, unsigned int _format_high, void * arg1, void * arg2); |
| 369 |
|
| 370 |
/* |
| 371 |
equal - return TRUE if C string cstr is equal to str. |
| 372 |
*/ |
| 373 |
|
| 374 |
static bool equal (void * cstr, const char *str_, unsigned int _str_high); |
| 375 |
|
| 376 |
/* |
| 377 |
SetupDebugFlags - By default assigns ModuleTrace, DependencyTrace, |
| 378 |
DumpPostInit to FALSE. It checks the environment |
| 379 |
GCC_M2LINK_RTFLAG which can contain |
| 380 |
"all,module,hex,pre,post,dep,force". all turns them all on. |
| 381 |
The flag meanings are as follows and flags the are in |
| 382 |
execution order. |
| 383 |
|
| 384 |
module generate trace info as the modules are registered. |
| 385 |
hex dump the modules ctor functions address in hex. |
| 386 |
pre generate a list of all modules seen prior to having |
| 387 |
their dependancies resolved. |
| 388 |
dep display a trace as the modules are resolved. |
| 389 |
post generate a list of all modules seen after having |
| 390 |
their dependancies resolved dynamically. |
| 391 |
force generate a list of all modules seen after having |
| 392 |
their dependancies resolved and forced. |
| 393 |
*/ |
| 394 |
|
| 395 |
static void SetupDebugFlags (void); |
| 396 |
|
| 397 |
/* |
| 398 |
Init - initialize the debug flags and set all lists to NIL. |
| 399 |
*/ |
| 400 |
|
| 401 |
static void Init (void); |
| 402 |
|
| 403 |
/* |
| 404 |
CheckInitialized - checks to see if this module has been initialized |
| 405 |
and if it has not it calls Init. We need this |
| 406 |
approach as this module is called by module ctors |
| 407 |
before we reach main. |
| 408 |
*/ |
| 409 |
|
| 410 |
static void CheckInitialized (void); |
| 411 |
|
| 412 |
/* |
| 413 |
ExecuteReverse - execute the procedure associated with procptr |
| 414 |
and then proceed to try and execute all previous |
| 415 |
procedures in the chain. |
| 416 |
*/ |
| 417 |
|
| 418 |
static void ExecuteReverse (M2Dependent_ProcedureChain procptr); |
| 419 |
|
| 420 |
/* |
| 421 |
AppendProc - append proc to the end of the procedure list |
| 422 |
defined by proclist. |
| 423 |
*/ |
| 424 |
|
| 425 |
static bool AppendProc (M2Dependent_ProcedureList *proclist, PROC proc); |
| 426 |
|
| 427 |
/* |
| 428 |
InitProcList - initialize the head and tail pointers to NIL. |
| 429 |
*/ |
| 430 |
|
| 431 |
static void InitProcList (M2Dependent_ProcedureList *p); |
| 432 |
|
| 433 |
|
| 434 |
/* |
| 435 |
InitDependencyList - initialize all fields of DependencyList. |
| 436 |
*/ |
| 437 |
|
| 438 |
static void InitDependencyList (M2Dependent_DependencyList *depList, PROC proc, M2Dependent_DependencyState state) |
| 439 |
{ |
| 440 |
(*depList).proc = proc; |
| 441 |
(*depList).forced = false; |
| 442 |
(*depList).forc = false; |
| 443 |
(*depList).appl = false; |
| 444 |
(*depList).state = state; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
/* |
| 449 |
CreateModule - creates a new module entry and returns the |
| 450 |
ModuleChain. |
| 451 |
*/ |
| 452 |
|
| 453 |
static M2Dependent_ModuleChain CreateModule (void * name, void * libname, M2Dependent_ArgCVEnvP init, M2Dependent_ArgCVEnvP fini, PROC dependencies) |
| 454 |
{ |
| 455 |
M2Dependent_ModuleChain mptr; |
| 456 |
|
| 457 |
Storage_ALLOCATE ((void **) &mptr, sizeof (M2Dependent__T2)); |
| 458 |
mptr->name = name; |
| 459 |
mptr->libname = libname; |
| 460 |
mptr->init = init; |
| 461 |
mptr->fini = fini; |
| 462 |
InitDependencyList (&mptr->dependency, dependencies, M2Dependent_unregistered); |
| 463 |
mptr->prev = NULL; |
| 464 |
mptr->next = NULL; |
| 465 |
if (HexTrace) |
| 466 |
{ |
| 467 |
libc_printf ((const char *) " (init: %p fini: %p", 22, init, fini); |
| 468 |
libc_printf ((const char *) " dep: %p)", 10, dependencies); |
| 469 |
} |
| 470 |
return mptr; |
| 471 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 472 |
__builtin_unreachable (); |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
/* |
| 477 |
AppendModule - append chain to end of the list. |
| 478 |
*/ |
| 479 |
|
| 480 |
static void AppendModule (M2Dependent_ModuleChain *head, M2Dependent_ModuleChain chain) |
| 481 |
{ |
| 482 |
if ((*head) == NULL) |
| 483 |
{ |
| 484 |
(*head) = chain; |
| 485 |
chain->prev = chain; |
| 486 |
chain->next = chain; |
| 487 |
} |
| 488 |
else |
| 489 |
{ |
| 490 |
chain->next = (*head); /* Add Item to the end of list. */ |
| 491 |
chain->prev = (*head)->prev; /* Add Item to the end of list. */ |
| 492 |
(*head)->prev->next = chain; |
| 493 |
(*head)->prev = chain; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
/* |
| 499 |
RemoveModule - remove chain from double linked list head. |
| 500 |
*/ |
| 501 |
|
| 502 |
static void RemoveModule (M2Dependent_ModuleChain *head, M2Dependent_ModuleChain chain) |
| 503 |
{ |
| 504 |
if ((chain->next == (*head)) && (chain == (*head))) |
| 505 |
{ |
| 506 |
(*head) = NULL; |
| 507 |
} |
| 508 |
else |
| 509 |
{ |
| 510 |
if ((*head) == chain) |
| 511 |
{ |
| 512 |
(*head) = (*head)->next; |
| 513 |
} |
| 514 |
chain->prev->next = chain->next; |
| 515 |
chain->next->prev = chain->prev; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
/* |
| 521 |
onChain - returns TRUE if mptr is on the Modules[state] list. |
| 522 |
*/ |
| 523 |
|
| 524 |
static bool onChain (M2Dependent_DependencyState state, M2Dependent_ModuleChain mptr) |
| 525 |
{ |
| 526 |
M2Dependent_ModuleChain ptr; |
| 527 |
|
| 528 |
if (Modules.array[state-M2Dependent_unregistered] != NULL) |
| 529 |
{ |
| 530 |
ptr = Modules.array[state-M2Dependent_unregistered]; |
| 531 |
do { |
| 532 |
if (ptr == mptr) |
| 533 |
{ |
| 534 |
return true; |
| 535 |
} |
| 536 |
ptr = ptr->next; |
| 537 |
} while (! (ptr == Modules.array[state-M2Dependent_unregistered])); |
| 538 |
} |
| 539 |
return false; |
| 540 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 541 |
__builtin_unreachable (); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
/* |
| 546 |
max - |
| 547 |
*/ |
| 548 |
|
| 549 |
static unsigned int max (unsigned int a, unsigned int b) |
| 550 |
{ |
| 551 |
if (a > b) |
| 552 |
{ |
| 553 |
return a; |
| 554 |
} |
| 555 |
else |
| 556 |
{ |
| 557 |
return b; |
| 558 |
} |
| 559 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 560 |
__builtin_unreachable (); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
/* |
| 565 |
min - |
| 566 |
*/ |
| 567 |
|
| 568 |
static unsigned int min (unsigned int a, unsigned int b) |
| 569 |
{ |
| 570 |
if (a < b) |
| 571 |
{ |
| 572 |
return a; |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
return b; |
| 577 |
} |
| 578 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 579 |
__builtin_unreachable (); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
/* |
| 584 |
LookupModuleN - lookup module from the state list. |
| 585 |
The strings lengths are known. |
| 586 |
*/ |
| 587 |
|
| 588 |
static M2Dependent_ModuleChain LookupModuleN (M2Dependent_DependencyState state, void * name, unsigned int namelen, void * libname, unsigned int libnamelen) |
| 589 |
{ |
| 590 |
M2Dependent_ModuleChain ptr; |
| 591 |
|
| 592 |
if (Modules.array[state-M2Dependent_unregistered] != NULL) |
| 593 |
{ |
| 594 |
ptr = Modules.array[state-M2Dependent_unregistered]; |
| 595 |
do { |
| 596 |
if (((strncmp (reinterpret_cast<M2Dependent_PtrToChar> (ptr->name), reinterpret_cast<M2Dependent_PtrToChar> (name), max (namelen, static_cast<unsigned int> (strlen_ (reinterpret_cast<M2Dependent_PtrToChar> (ptr->name)))))) == 0) && ((strncmp (reinterpret_cast<M2Dependent_PtrToChar> (ptr->libname), reinterpret_cast<M2Dependent_PtrToChar> (libname), max (libnamelen, static_cast<unsigned int> (strlen_ (reinterpret_cast<M2Dependent_PtrToChar> (ptr->libname)))))) == 0)) |
| 597 |
{ |
| 598 |
return ptr; |
| 599 |
} |
| 600 |
ptr = ptr->next; |
| 601 |
} while (! (ptr == Modules.array[state-M2Dependent_unregistered])); |
| 602 |
} |
| 603 |
return NULL; |
| 604 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 605 |
__builtin_unreachable (); |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
/* |
| 610 |
LookupModule - lookup and return the ModuleChain pointer containing |
| 611 |
module name from a particular list. |
| 612 |
*/ |
| 613 |
|
| 614 |
static M2Dependent_ModuleChain LookupModule (M2Dependent_DependencyState state, void * name, void * libname) |
| 615 |
{ |
| 616 |
return LookupModuleN (state, name, static_cast<unsigned int> (strlen_ (reinterpret_cast<M2Dependent_PtrToChar> (name))), libname, static_cast<unsigned int> (strlen_ (reinterpret_cast<M2Dependent_PtrToChar> (libname)))); |
| 617 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 618 |
__builtin_unreachable (); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
/* |
| 623 |
toCString - replace any character sequence |
| 624 |
into a newline. |
| 625 |
*/ |
| 626 |
|
| 627 |
static void toCString (char *str, unsigned int _str_high) |
| 628 |
{ |
| 629 |
unsigned int high; |
| 630 |
unsigned int i; |
| 631 |
unsigned int j; |
| 632 |
|
| 633 |
i = 0; |
| 634 |
high = _str_high; |
| 635 |
while (i < high) |
| 636 |
{ |
| 637 |
if ((i < high) && (str[i] == '\\')) |
| 638 |
{ |
| 639 |
if (str[i+1] == 'n') |
| 640 |
{ |
| 641 |
const_cast<char *>(str)[i] = ASCII_nl; |
| 642 |
j = i+1; |
| 643 |
while (j < high) |
| 644 |
{ |
| 645 |
const_cast<char *>(str)[j] = str[j+1]; |
| 646 |
j += 1; |
| 647 |
} |
| 648 |
} |
| 649 |
} |
| 650 |
i += 1; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
/* |
| 656 |
strcmp - return 0 if both strings are equal. |
| 657 |
We cannot use Builtins.def during bootstrap. |
| 658 |
*/ |
| 659 |
|
| 660 |
static int strcmp (M2Dependent_PtrToChar a, M2Dependent_PtrToChar b) |
| 661 |
{ |
| 662 |
if ((a != NULL) && (b != NULL)) |
| 663 |
{ |
| 664 |
/* avoid gcc warning by using compound statement even if not strictly necessary. */ |
| 665 |
if (a == b) |
| 666 |
{ |
| 667 |
return 0; |
| 668 |
} |
| 669 |
else |
| 670 |
{ |
| 671 |
while ((*a) == (*b)) |
| 672 |
{ |
| 673 |
if ((*a) == ASCII_nul) |
| 674 |
{ |
| 675 |
return 0; |
| 676 |
} |
| 677 |
a += 1; |
| 678 |
b += 1; |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
return 1; |
| 683 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 684 |
__builtin_unreachable (); |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
/* |
| 689 |
strncmp - return 0 if both strings are equal. |
| 690 |
We cannot use Builtins.def during bootstrap. |
| 691 |
*/ |
| 692 |
|
| 693 |
static int strncmp (M2Dependent_PtrToChar a, M2Dependent_PtrToChar b, unsigned int n) |
| 694 |
{ |
| 695 |
if (n == 0) |
| 696 |
{ |
| 697 |
return 0; |
| 698 |
} |
| 699 |
else if ((a != NULL) && (b != NULL)) |
| 700 |
{ |
| 701 |
/* avoid dangling else. */ |
| 702 |
if (a == b) |
| 703 |
{ |
| 704 |
return 0; |
| 705 |
} |
| 706 |
else |
| 707 |
{ |
| 708 |
while (((*a) == (*b)) && (n > 0)) |
| 709 |
{ |
| 710 |
if (((*a) == ASCII_nul) || (n == 1)) |
| 711 |
{ |
| 712 |
return 0; |
| 713 |
} |
| 714 |
a += 1; |
| 715 |
b += 1; |
| 716 |
n -= 1; |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
return 1; |
| 721 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 722 |
__builtin_unreachable (); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
/* |
| 727 |
strlen - returns the length of string. |
| 728 |
*/ |
| 729 |
|
| 730 |
static int strlen_ (M2Dependent_PtrToChar string) |
| 731 |
{ |
| 732 |
int count; |
| 733 |
|
| 734 |
if (string == NULL) |
| 735 |
{ |
| 736 |
return 0; |
| 737 |
} |
| 738 |
else |
| 739 |
{ |
| 740 |
count = 0; |
| 741 |
while ((*string) != ASCII_nul) |
| 742 |
{ |
| 743 |
string += 1; |
| 744 |
count += 1; |
| 745 |
} |
| 746 |
return count; |
| 747 |
} |
| 748 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 749 |
__builtin_unreachable (); |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
/* |
| 754 |
traceprintf - wrap printf with a boolean flag. |
| 755 |
*/ |
| 756 |
|
| 757 |
static void traceprintf (bool flag, const char *str_, unsigned int _str_high) |
| 758 |
{ |
| 759 |
char str[_str_high+1]; |
| 760 |
|
| 761 |
/* make a local copy of each unbounded array. */ |
| 762 |
memcpy (str, str_, _str_high+1); |
| 763 |
|
| 764 |
if (flag) |
| 765 |
{ |
| 766 |
toCString ((char *) str, _str_high); |
| 767 |
libc_printf ((const char *) str, _str_high); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
/* |
| 773 |
traceprintf2 - wrap printf with a boolean flag. |
| 774 |
*/ |
| 775 |
|
| 776 |
static void traceprintf2 (bool flag, const char *str_, unsigned int _str_high, void * arg) |
| 777 |
{ |
| 778 |
char ch; |
| 779 |
char str[_str_high+1]; |
| 780 |
|
| 781 |
/* make a local copy of each unbounded array. */ |
| 782 |
memcpy (str, str_, _str_high+1); |
| 783 |
|
| 784 |
if (flag) |
| 785 |
{ |
| 786 |
toCString ((char *) str, _str_high); |
| 787 |
if (arg == NULL) |
| 788 |
{ |
| 789 |
ch = (char) 0; |
| 790 |
arg = &ch; |
| 791 |
} |
| 792 |
libc_printf ((const char *) str, _str_high, arg); |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
/* |
| 798 |
traceprintf3 - wrap printf with a boolean flag. |
| 799 |
*/ |
| 800 |
|
| 801 |
static void traceprintf3 (bool flag, const char *str_, unsigned int _str_high, void * arg1, void * arg2) |
| 802 |
{ |
| 803 |
char ch; |
| 804 |
char str[_str_high+1]; |
| 805 |
|
| 806 |
/* make a local copy of each unbounded array. */ |
| 807 |
memcpy (str, str_, _str_high+1); |
| 808 |
|
| 809 |
if (flag) |
| 810 |
{ |
| 811 |
toCString ((char *) str, _str_high); |
| 812 |
if (arg1 == NULL) |
| 813 |
{ |
| 814 |
ch = (char) 0; |
| 815 |
arg1 = &ch; |
| 816 |
} |
| 817 |
if (arg2 == NULL) |
| 818 |
{ |
| 819 |
ch = (char) 0; |
| 820 |
arg2 = &ch; |
| 821 |
} |
| 822 |
libc_printf ((const char *) str, _str_high, arg1, arg2); |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
/* |
| 828 |
moveTo - moves mptr to the new list determined by newstate. |
| 829 |
It updates the mptr state appropriately. |
| 830 |
*/ |
| 831 |
|
| 832 |
static void moveTo (M2Dependent_DependencyState newstate, M2Dependent_ModuleChain mptr) |
| 833 |
{ |
| 834 |
if (onChain (mptr->dependency.state, mptr)) |
| 835 |
{ |
| 836 |
RemoveModule (&Modules.array[mptr->dependency.state-M2Dependent_unregistered], mptr); |
| 837 |
} |
| 838 |
mptr->dependency.state = newstate; |
| 839 |
AppendModule (&Modules.array[mptr->dependency.state-M2Dependent_unregistered], mptr); |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
/* |
| 844 |
ResolveDependant - |
| 845 |
*/ |
| 846 |
|
| 847 |
static void ResolveDependant (M2Dependent_ModuleChain mptr, void * currentmodule, void * libname) |
| 848 |
{ |
| 849 |
if (mptr == NULL) |
| 850 |
{ |
| 851 |
traceprintf3 (DependencyTrace, (const char *) " module %s [%s] has not been registered via a global constructor\\n", 68, currentmodule, libname); |
| 852 |
} |
| 853 |
else |
| 854 |
{ |
| 855 |
if (onChain (M2Dependent_started, mptr)) |
| 856 |
{ |
| 857 |
traceprintf (DependencyTrace, (const char *) " processing...\\n", 18); |
| 858 |
} |
| 859 |
else |
| 860 |
{ |
| 861 |
moveTo (M2Dependent_started, mptr); |
| 862 |
traceprintf3 (DependencyTrace, (const char *) " starting: %s [%s]\\n", 22, currentmodule, libname); |
| 863 |
(*mptr->dependency.proc.proc) (); /* Invoke and process the dependency graph. */ |
| 864 |
traceprintf3 (DependencyTrace, (const char *) " finished: %s [%s]\\n", 22, currentmodule, libname); /* Invoke and process the dependency graph. */ |
| 865 |
moveTo (M2Dependent_ordered, mptr); |
| 866 |
} |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
/* |
| 872 |
PerformRequestDependant - the current modulename has a dependancy upon |
| 873 |
dependantmodule. If dependantmodule is NIL then |
| 874 |
modulename has no further dependants and it can be |
| 875 |
resolved. |
| 876 |
*/ |
| 877 |
|
| 878 |
static void PerformRequestDependant (void * modulename, void * libname, void * dependantmodule, void * dependantlibname) |
| 879 |
{ |
| 880 |
M2Dependent_ModuleChain mptr; |
| 881 |
|
| 882 |
traceprintf3 (DependencyTrace, (const char *) " module %s [%s]", 16, modulename, libname); |
| 883 |
if (dependantmodule == NULL) |
| 884 |
{ |
| 885 |
/* avoid dangling else. */ |
| 886 |
traceprintf (DependencyTrace, (const char *) " has finished its import graph\\n", 32); |
| 887 |
mptr = LookupModule (M2Dependent_unordered, modulename, libname); |
| 888 |
if (mptr != NULL) |
| 889 |
{ |
| 890 |
traceprintf3 (DependencyTrace, (const char *) " module %s [%s] is now ordered\\n", 33, modulename, libname); |
| 891 |
moveTo (M2Dependent_ordered, mptr); |
| 892 |
} |
| 893 |
} |
| 894 |
else |
| 895 |
{ |
| 896 |
traceprintf3 (DependencyTrace, (const char *) " imports from %s [%s]\\n", 23, dependantmodule, dependantlibname); |
| 897 |
mptr = LookupModule (M2Dependent_ordered, dependantmodule, dependantlibname); |
| 898 |
if (mptr == NULL) |
| 899 |
{ |
| 900 |
traceprintf3 (DependencyTrace, (const char *) " module %s [%s] is not ordered\\n", 33, dependantmodule, dependantlibname); |
| 901 |
mptr = LookupModule (M2Dependent_unordered, dependantmodule, dependantlibname); |
| 902 |
if (mptr == NULL) |
| 903 |
{ |
| 904 |
traceprintf3 (DependencyTrace, (const char *) " module %s [%s] is not unordered\\n", 35, dependantmodule, dependantlibname); |
| 905 |
mptr = LookupModule (M2Dependent_started, dependantmodule, dependantlibname); |
| 906 |
if (mptr == NULL) |
| 907 |
{ |
| 908 |
traceprintf3 (DependencyTrace, (const char *) " module %s [%s] has not started\\n", 34, dependantmodule, dependantlibname); |
| 909 |
traceprintf3 (DependencyTrace, (const char *) " module %s [%s] attempting to import from", 42, modulename, libname); |
| 910 |
traceprintf3 (DependencyTrace, (const char *) " %s [%s] which has not registered itself via a constructor\\n", 60, dependantmodule, dependantlibname); |
| 911 |
} |
| 912 |
else |
| 913 |
{ |
| 914 |
traceprintf3 (DependencyTrace, (const char *) " module %s [%s] has registered itself and has started\\n", 56, dependantmodule, dependantlibname); |
| 915 |
} |
| 916 |
} |
| 917 |
else |
| 918 |
{ |
| 919 |
traceprintf3 (DependencyTrace, (const char *) " module %s [%s] resolving\\n", 28, dependantmodule, dependantlibname); |
| 920 |
ResolveDependant (mptr, dependantmodule, dependantlibname); |
| 921 |
} |
| 922 |
} |
| 923 |
else |
| 924 |
{ |
| 925 |
traceprintf3 (DependencyTrace, (const char *) " module %s [%s]", 16, modulename, libname); |
| 926 |
traceprintf3 (DependencyTrace, (const char *) " dependant %s [%s] is ordered\\n", 31, dependantmodule, dependantlibname); |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
/* |
| 933 |
ResolveDependencies - resolve dependencies for currentmodule, libname. |
| 934 |
*/ |
| 935 |
|
| 936 |
static void ResolveDependencies (void * currentmodule, void * libname) |
| 937 |
{ |
| 938 |
M2Dependent_ModuleChain mptr; |
| 939 |
|
| 940 |
mptr = LookupModule (M2Dependent_unordered, currentmodule, libname); |
| 941 |
while (mptr != NULL) |
| 942 |
{ |
| 943 |
traceprintf3 (DependencyTrace, (const char *) " attempting to resolve the dependants for %s [%s]\\n", 53, currentmodule, libname); |
| 944 |
ResolveDependant (mptr, currentmodule, libname); |
| 945 |
mptr = Modules.array[M2Dependent_unordered-M2Dependent_unregistered]; |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
/* |
| 951 |
DisplayModuleInfo - displays all module in the state. |
| 952 |
*/ |
| 953 |
|
| 954 |
static void DisplayModuleInfo (M2Dependent_DependencyState state, const char *desc_, unsigned int _desc_high) |
| 955 |
{ |
| 956 |
M2Dependent_ModuleChain mptr; |
| 957 |
unsigned int count; |
| 958 |
char desc[_desc_high+1]; |
| 959 |
|
| 960 |
/* make a local copy of each unbounded array. */ |
| 961 |
memcpy (desc, desc_, _desc_high+1); |
| 962 |
|
| 963 |
if (Modules.array[state-M2Dependent_unregistered] != NULL) |
| 964 |
{ |
| 965 |
libc_printf ((const char *) "%s modules\\n", 12, const_cast<void*> (static_cast<const void*>(desc))); |
| 966 |
mptr = Modules.array[state-M2Dependent_unregistered]; |
| 967 |
count = 0; |
| 968 |
do { |
| 969 |
if (mptr->name == NULL) |
| 970 |
{ |
| 971 |
libc_printf ((const char *) " %d %s []", 11, count, mptr->name); |
| 972 |
} |
| 973 |
else |
| 974 |
{ |
| 975 |
libc_printf ((const char *) " %d %s [%s]", 13, count, mptr->name, mptr->libname); |
| 976 |
} |
| 977 |
count += 1; |
| 978 |
if (mptr->dependency.appl) |
| 979 |
{ |
| 980 |
libc_printf ((const char *) " application", 12); |
| 981 |
} |
| 982 |
if (mptr->dependency.forc) |
| 983 |
{ |
| 984 |
libc_printf ((const char *) " for C", 6); |
| 985 |
} |
| 986 |
if (mptr->dependency.forced) |
| 987 |
{ |
| 988 |
libc_printf ((const char *) " forced ordering", 16); |
| 989 |
} |
| 990 |
libc_printf ((const char *) "\\n", 2); |
| 991 |
mptr = mptr->next; |
| 992 |
} while (! (mptr == Modules.array[state-M2Dependent_unregistered])); |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
|
| 997 |
/* |
| 998 |
DumpModuleData - |
| 999 |
*/ |
| 1000 |
|
| 1001 |
static void DumpModuleData (bool flag) |
| 1002 |
{ |
| 1003 |
if (flag) |
| 1004 |
{ |
| 1005 |
DisplayModuleInfo (M2Dependent_unregistered, (const char *) "unregistered", 12); |
| 1006 |
DisplayModuleInfo (M2Dependent_unordered, (const char *) "unordered", 9); |
| 1007 |
DisplayModuleInfo (M2Dependent_started, (const char *) "started", 7); |
| 1008 |
DisplayModuleInfo (M2Dependent_ordered, (const char *) "ordered", 7); |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
|
| 1013 |
/* |
| 1014 |
combine - dest := src + dest. Places src at the front of list dest. |
| 1015 |
Pre condition: src, dest are lists. |
| 1016 |
Post condition : dest := src + dest |
| 1017 |
src := NIL. |
| 1018 |
*/ |
| 1019 |
|
| 1020 |
static void combine (M2Dependent_DependencyState src, M2Dependent_DependencyState dest) |
| 1021 |
{ |
| 1022 |
M2Dependent_ModuleChain last; |
| 1023 |
|
| 1024 |
while (Modules.array[src-M2Dependent_unregistered] != NULL) |
| 1025 |
{ |
| 1026 |
last = Modules.array[src-M2Dependent_unregistered]->prev; |
| 1027 |
moveTo (M2Dependent_ordered, last); |
| 1028 |
Modules.array[dest-M2Dependent_unregistered] = last; /* New item is at the head. */ |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
/* |
| 1034 |
tracemodule - |
| 1035 |
*/ |
| 1036 |
|
| 1037 |
static void tracemodule (bool flag, void * modname, unsigned int modlen, void * libname, unsigned int liblen) |
| 1038 |
{ |
| 1039 |
typedef struct tracemodule__T5_a tracemodule__T5; |
| 1040 |
|
| 1041 |
struct tracemodule__T5_a { char array[100+1]; }; |
| 1042 |
tracemodule__T5 buffer; |
| 1043 |
unsigned int len; |
| 1044 |
|
| 1045 |
if (flag) |
| 1046 |
{ |
| 1047 |
len = min (modlen, sizeof (buffer)-1); |
| 1048 |
libc_strncpy (&buffer, modname, len); |
| 1049 |
buffer.array[len] = (char) 0; |
| 1050 |
libc_printf ((const char *) "%s ", 3, &buffer); |
| 1051 |
len = min (liblen, sizeof (buffer)-1); |
| 1052 |
libc_strncpy (&buffer, libname, len); |
| 1053 |
buffer.array[len] = (char) 0; |
| 1054 |
libc_printf ((const char *) " [%s]", 5, &buffer); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
/* |
| 1060 |
ForceModule - |
| 1061 |
*/ |
| 1062 |
|
| 1063 |
static void ForceModule (void * modname, unsigned int modlen, void * libname, unsigned int liblen) |
| 1064 |
{ |
| 1065 |
M2Dependent_ModuleChain mptr; |
| 1066 |
|
| 1067 |
traceprintf (ForceTrace, (const char *) "forcing module: ", 16); |
| 1068 |
tracemodule (ForceTrace, modname, modlen, libname, liblen); |
| 1069 |
traceprintf (ForceTrace, (const char *) "\\n", 2); |
| 1070 |
mptr = LookupModuleN (M2Dependent_ordered, modname, modlen, libname, liblen); |
| 1071 |
if (mptr != NULL) |
| 1072 |
{ |
| 1073 |
mptr->dependency.forced = true; |
| 1074 |
moveTo (M2Dependent_user, mptr); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
/* |
| 1080 |
ForceDependencies - if the user has specified a forced order then we override |
| 1081 |
the dynamic ordering with the preference. |
| 1082 |
*/ |
| 1083 |
|
| 1084 |
static void ForceDependencies (void * overrideliborder) |
| 1085 |
{ |
| 1086 |
unsigned int len; |
| 1087 |
unsigned int modlen; |
| 1088 |
unsigned int liblen; |
| 1089 |
M2Dependent_PtrToChar modname; |
| 1090 |
M2Dependent_PtrToChar libname; |
| 1091 |
M2Dependent_PtrToChar pc; |
| 1092 |
M2Dependent_PtrToChar start; |
| 1093 |
|
| 1094 |
if (overrideliborder != NULL) |
| 1095 |
{ |
| 1096 |
traceprintf2 (ForceTrace, (const char *) "user forcing order: %s\\n", 24, overrideliborder); |
| 1097 |
pc = static_cast<M2Dependent_PtrToChar> (overrideliborder); |
| 1098 |
start = pc; |
| 1099 |
len = 0; |
| 1100 |
modname = NULL; |
| 1101 |
modlen = 0; |
| 1102 |
libname = NULL; |
| 1103 |
liblen = 0; |
| 1104 |
while ((*pc) != ASCII_nul) |
| 1105 |
{ |
| 1106 |
switch ((*pc)) |
| 1107 |
{ |
| 1108 |
case ':': |
| 1109 |
libname = start; |
| 1110 |
liblen = len; |
| 1111 |
len = 0; |
| 1112 |
pc += 1; |
| 1113 |
start = pc; |
| 1114 |
break; |
| 1115 |
|
| 1116 |
case ',': |
| 1117 |
modname = start; |
| 1118 |
modlen = len; |
| 1119 |
ForceModule (reinterpret_cast<void *> (modname), modlen, reinterpret_cast<void *> (libname), liblen); |
| 1120 |
libname = NULL; |
| 1121 |
liblen = 0; |
| 1122 |
modlen = 0; |
| 1123 |
len = 0; |
| 1124 |
pc += 1; |
| 1125 |
start = pc; |
| 1126 |
break; |
| 1127 |
|
| 1128 |
|
| 1129 |
default: |
| 1130 |
pc += 1; |
| 1131 |
len += 1; |
| 1132 |
break; |
| 1133 |
} |
| 1134 |
} |
| 1135 |
if (start != pc) |
| 1136 |
{ |
| 1137 |
ForceModule (reinterpret_cast<void *> (start), len, reinterpret_cast<void *> (libname), liblen); |
| 1138 |
} |
| 1139 |
combine (M2Dependent_user, M2Dependent_ordered); |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
|
| 1144 |
/* |
| 1145 |
CheckApplication - check to see that the application is the last entry in the list. |
| 1146 |
This might happen if the application only imports FOR C modules. |
| 1147 |
*/ |
| 1148 |
|
| 1149 |
static void CheckApplication (void) |
| 1150 |
{ |
| 1151 |
M2Dependent_ModuleChain mptr; |
| 1152 |
M2Dependent_ModuleChain appl; |
| 1153 |
|
| 1154 |
mptr = Modules.array[M2Dependent_ordered-M2Dependent_unregistered]; |
| 1155 |
if (mptr != NULL) |
| 1156 |
{ |
| 1157 |
appl = NULL; |
| 1158 |
do { |
| 1159 |
if (mptr->dependency.appl) |
| 1160 |
{ |
| 1161 |
appl = mptr; |
| 1162 |
} |
| 1163 |
else |
| 1164 |
{ |
| 1165 |
mptr = mptr->next; |
| 1166 |
} |
| 1167 |
} while (! ((appl != NULL) || (mptr == Modules.array[M2Dependent_ordered-M2Dependent_unregistered]))); |
| 1168 |
if (appl != NULL) |
| 1169 |
{ |
| 1170 |
RemoveModule (&Modules.array[M2Dependent_ordered-M2Dependent_unregistered], appl); |
| 1171 |
AppendModule (&Modules.array[M2Dependent_ordered-M2Dependent_unregistered], appl); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
|
| 1177 |
/* |
| 1178 |
warning3 - write format arg1 arg2 to stderr. |
| 1179 |
*/ |
| 1180 |
|
| 1181 |
static void warning3 (const char *format_, unsigned int _format_high, void * arg1, void * arg2) |
| 1182 |
{ |
| 1183 |
typedef struct warning3__T6_a warning3__T6; |
| 1184 |
|
| 1185 |
struct warning3__T6_a { char array[4096+1]; }; |
| 1186 |
warning3__T6 buffer; |
| 1187 |
int len; |
| 1188 |
char format[_format_high+1]; |
| 1189 |
|
| 1190 |
/* make a local copy of each unbounded array. */ |
| 1191 |
memcpy (format, format_, _format_high+1); |
| 1192 |
|
| 1193 |
if (WarningTrace) |
| 1194 |
{ |
| 1195 |
len = libc_snprintf (&buffer, static_cast<size_t> (sizeof (buffer)), (const char *) "warning: ", 9); |
| 1196 |
libc_write (2, &buffer, static_cast<size_t> (len)); |
| 1197 |
len = libc_snprintf (&buffer, static_cast<size_t> (sizeof (buffer)), (const char *) format, _format_high, arg1, arg2); |
| 1198 |
libc_write (2, &buffer, static_cast<size_t> (len)); |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
|
| 1203 |
/* |
| 1204 |
equal - return TRUE if C string cstr is equal to str. |
| 1205 |
*/ |
| 1206 |
|
| 1207 |
static bool equal (void * cstr, const char *str_, unsigned int _str_high) |
| 1208 |
{ |
| 1209 |
char str[_str_high+1]; |
| 1210 |
|
| 1211 |
/* make a local copy of each unbounded array. */ |
| 1212 |
memcpy (str, str_, _str_high+1); |
| 1213 |
|
| 1214 |
return (strncmp (reinterpret_cast <M2Dependent_PtrToChar> (cstr), reinterpret_cast <M2Dependent_PtrToChar> (const_cast<void*> (static_cast<const void*>(str))), StrLib_StrLen ((const char *) str, _str_high))) == 0; |
| 1215 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1216 |
__builtin_unreachable (); |
| 1217 |
} |
| 1218 |
|
| 1219 |
|
| 1220 |
/* |
| 1221 |
SetupDebugFlags - By default assigns ModuleTrace, DependencyTrace, |
| 1222 |
DumpPostInit to FALSE. It checks the environment |
| 1223 |
GCC_M2LINK_RTFLAG which can contain |
| 1224 |
"all,module,hex,pre,post,dep,force". all turns them all on. |
| 1225 |
The flag meanings are as follows and flags the are in |
| 1226 |
execution order. |
| 1227 |
|
| 1228 |
module generate trace info as the modules are registered. |
| 1229 |
hex dump the modules ctor functions address in hex. |
| 1230 |
pre generate a list of all modules seen prior to having |
| 1231 |
their dependancies resolved. |
| 1232 |
dep display a trace as the modules are resolved. |
| 1233 |
post generate a list of all modules seen after having |
| 1234 |
their dependancies resolved dynamically. |
| 1235 |
force generate a list of all modules seen after having |
| 1236 |
their dependancies resolved and forced. |
| 1237 |
*/ |
| 1238 |
|
| 1239 |
static void SetupDebugFlags (void) |
| 1240 |
{ |
| 1241 |
typedef char *SetupDebugFlags__T1; |
| 1242 |
|
| 1243 |
SetupDebugFlags__T1 pc; |
| 1244 |
|
| 1245 |
ModuleTrace = false; |
| 1246 |
DependencyTrace = false; |
| 1247 |
PostTrace = false; |
| 1248 |
PreTrace = false; |
| 1249 |
ForceTrace = false; |
| 1250 |
HexTrace = false; |
| 1251 |
WarningTrace = false; |
| 1252 |
pc = static_cast<SetupDebugFlags__T1> (libc_getenv (const_cast<void*> (static_cast<const void*>("GCC_M2LINK_RTFLAG")))); |
| 1253 |
while ((pc != NULL) && ((*pc) != ASCII_nul)) |
| 1254 |
{ |
| 1255 |
if (equal (reinterpret_cast<void *> (pc), (const char *) "all", 3)) |
| 1256 |
{ |
| 1257 |
ModuleTrace = true; |
| 1258 |
DependencyTrace = true; |
| 1259 |
PreTrace = true; |
| 1260 |
PostTrace = true; |
| 1261 |
ForceTrace = true; |
| 1262 |
HexTrace = true; |
| 1263 |
WarningTrace = true; |
| 1264 |
pc += 3; |
| 1265 |
} |
| 1266 |
else if (equal (reinterpret_cast<void *> (pc), (const char *) "module", 6)) |
| 1267 |
{ |
| 1268 |
/* avoid dangling else. */ |
| 1269 |
ModuleTrace = true; |
| 1270 |
pc += 6; |
| 1271 |
} |
| 1272 |
else if (equal (reinterpret_cast<void *> (pc), (const char *) "warning", 7)) |
| 1273 |
{ |
| 1274 |
/* avoid dangling else. */ |
| 1275 |
WarningTrace = true; |
| 1276 |
pc += 7; |
| 1277 |
} |
| 1278 |
else if (equal (reinterpret_cast<void *> (pc), (const char *) "hex", 3)) |
| 1279 |
{ |
| 1280 |
/* avoid dangling else. */ |
| 1281 |
HexTrace = true; |
| 1282 |
pc += 3; |
| 1283 |
} |
| 1284 |
else if (equal (reinterpret_cast<void *> (pc), (const char *) "dep", 3)) |
| 1285 |
{ |
| 1286 |
/* avoid dangling else. */ |
| 1287 |
DependencyTrace = true; |
| 1288 |
pc += 3; |
| 1289 |
} |
| 1290 |
else if (equal (reinterpret_cast<void *> (pc), (const char *) "pre", 3)) |
| 1291 |
{ |
| 1292 |
/* avoid dangling else. */ |
| 1293 |
PreTrace = true; |
| 1294 |
pc += 3; |
| 1295 |
} |
| 1296 |
else if (equal (reinterpret_cast<void *> (pc), (const char *) "post", 4)) |
| 1297 |
{ |
| 1298 |
/* avoid dangling else. */ |
| 1299 |
PostTrace = true; |
| 1300 |
pc += 4; |
| 1301 |
} |
| 1302 |
else if (equal (reinterpret_cast<void *> (pc), (const char *) "force", 5)) |
| 1303 |
{ |
| 1304 |
/* avoid dangling else. */ |
| 1305 |
ForceTrace = true; |
| 1306 |
pc += 5; |
| 1307 |
} |
| 1308 |
else |
| 1309 |
{ |
| 1310 |
/* avoid dangling else. */ |
| 1311 |
pc += 1; |
| 1312 |
} |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
|
| 1317 |
/* |
| 1318 |
Init - initialize the debug flags and set all lists to NIL. |
| 1319 |
*/ |
| 1320 |
|
| 1321 |
static void Init (void) |
| 1322 |
{ |
| 1323 |
M2Dependent_DependencyState state; |
| 1324 |
|
| 1325 |
InitProcList (&InitialProc); |
| 1326 |
InitProcList (&TerminateProc); |
| 1327 |
SetupDebugFlags (); |
| 1328 |
for (state=M2Dependent_unregistered; state<=M2Dependent_user; state= static_cast<M2Dependent_DependencyState>(static_cast<int>(state+1))) |
| 1329 |
{ |
| 1330 |
Modules.array[state-M2Dependent_unregistered] = NULL; |
| 1331 |
} |
| 1332 |
DynamicInitialization = false; |
| 1333 |
} |
| 1334 |
|
| 1335 |
|
| 1336 |
/* |
| 1337 |
CheckInitialized - checks to see if this module has been initialized |
| 1338 |
and if it has not it calls Init. We need this |
| 1339 |
approach as this module is called by module ctors |
| 1340 |
before we reach main. |
| 1341 |
*/ |
| 1342 |
|
| 1343 |
static void CheckInitialized (void) |
| 1344 |
{ |
| 1345 |
if (! Initialized) |
| 1346 |
{ |
| 1347 |
Initialized = true; |
| 1348 |
Init (); |
| 1349 |
} |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
/* |
| 1354 |
ExecuteReverse - execute the procedure associated with procptr |
| 1355 |
and then proceed to try and execute all previous |
| 1356 |
procedures in the chain. |
| 1357 |
*/ |
| 1358 |
|
| 1359 |
static void ExecuteReverse (M2Dependent_ProcedureChain procptr) |
| 1360 |
{ |
| 1361 |
while (procptr != NULL) |
| 1362 |
{ |
| 1363 |
(*procptr->p.proc) (); /* Invoke the procedure. */ |
| 1364 |
procptr = procptr->prev; /* Invoke the procedure. */ |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
|
| 1369 |
/* |
| 1370 |
AppendProc - append proc to the end of the procedure list |
| 1371 |
defined by proclist. |
| 1372 |
*/ |
| 1373 |
|
| 1374 |
static bool AppendProc (M2Dependent_ProcedureList *proclist, PROC proc) |
| 1375 |
{ |
| 1376 |
M2Dependent_ProcedureChain pdes; |
| 1377 |
|
| 1378 |
Storage_ALLOCATE ((void **) &pdes, sizeof (M2Dependent__T3)); |
| 1379 |
pdes->p = proc; |
| 1380 |
pdes->prev = (*proclist).tail; |
| 1381 |
pdes->next = NULL; |
| 1382 |
if ((*proclist).head == NULL) |
| 1383 |
{ |
| 1384 |
(*proclist).head = pdes; |
| 1385 |
} |
| 1386 |
(*proclist).tail = pdes; |
| 1387 |
return true; |
| 1388 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1389 |
__builtin_unreachable (); |
| 1390 |
} |
| 1391 |
|
| 1392 |
|
| 1393 |
/* |
| 1394 |
InitProcList - initialize the head and tail pointers to NIL. |
| 1395 |
*/ |
| 1396 |
|
| 1397 |
static void InitProcList (M2Dependent_ProcedureList *p) |
| 1398 |
{ |
| 1399 |
(*p).head = NULL; |
| 1400 |
(*p).tail = NULL; |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
/* |
| 1405 |
ConstructModules - resolve dependencies and then call each |
| 1406 |
module constructor in turn. |
| 1407 |
*/ |
| 1408 |
|
| 1409 |
extern "C" void M2Dependent_ConstructModules (void * applicationmodule, void * libname, void * overrideliborder, int argc, void * argv, void * envp) |
| 1410 |
{ |
| 1411 |
M2Dependent_ModuleChain mptr; |
| 1412 |
|
| 1413 |
CheckInitialized (); |
| 1414 |
DynamicInitialization = true; /* This procedure is only called if we desire dynamic initialization. */ |
| 1415 |
traceprintf3 (ModuleTrace, (const char *) "application module: %s [%s]\\n", 29, applicationmodule, libname); /* This procedure is only called if we desire dynamic initialization. */ |
| 1416 |
mptr = LookupModule (M2Dependent_unordered, applicationmodule, libname); |
| 1417 |
if (mptr != NULL) |
| 1418 |
{ |
| 1419 |
mptr->dependency.appl = true; |
| 1420 |
} |
| 1421 |
traceprintf (PreTrace, (const char *) "Pre resolving dependents\\n", 26); |
| 1422 |
DumpModuleData (PreTrace); |
| 1423 |
ResolveDependencies (applicationmodule, libname); |
| 1424 |
traceprintf (PreTrace, (const char *) "Post resolving dependents\\n", 27); |
| 1425 |
DumpModuleData (PostTrace); |
| 1426 |
ForceDependencies (overrideliborder); |
| 1427 |
traceprintf (ForceTrace, (const char *) "After user forcing ordering\\n", 29); |
| 1428 |
DumpModuleData (ForceTrace); |
| 1429 |
CheckApplication (); |
| 1430 |
traceprintf (ForceTrace, (const char *) "After runtime forces application to the end\\n", 45); |
| 1431 |
DumpModuleData (ForceTrace); |
| 1432 |
if (Modules.array[M2Dependent_ordered-M2Dependent_unregistered] == NULL) |
| 1433 |
{ |
| 1434 |
traceprintf3 (ModuleTrace, (const char *) " module: %s [%s] has not registered itself using a global constructor\\n", 72, applicationmodule, libname); |
| 1435 |
traceprintf2 (ModuleTrace, (const char *) " hint try compile and linking using: gm2 %s.mod\\n", 50, applicationmodule); |
| 1436 |
traceprintf2 (ModuleTrace, (const char *) " or try using: gm2 -fscaffold-static %s.mod\\n", 46, applicationmodule); |
| 1437 |
} |
| 1438 |
else |
| 1439 |
{ |
| 1440 |
mptr = Modules.array[M2Dependent_ordered-M2Dependent_unregistered]; |
| 1441 |
do { |
| 1442 |
if (mptr->dependency.forc) |
| 1443 |
{ |
| 1444 |
traceprintf3 (ModuleTrace, (const char *) "initializing module: %s [%s] for C\\n", 36, mptr->name, mptr->libname); |
| 1445 |
} |
| 1446 |
else |
| 1447 |
{ |
| 1448 |
traceprintf3 (ModuleTrace, (const char *) "initializing module: %s [%s]\\n", 30, mptr->name, mptr->libname); |
| 1449 |
} |
| 1450 |
if (mptr->dependency.appl) |
| 1451 |
{ |
| 1452 |
traceprintf3 (ModuleTrace, (const char *) "application module: %s [%s]\\n", 29, mptr->name, mptr->libname); |
| 1453 |
traceprintf (ModuleTrace, (const char *) " calling ExecuteInitialProcedures\\n", 36); |
| 1454 |
M2Dependent_ExecuteInitialProcedures (); |
| 1455 |
traceprintf (ModuleTrace, (const char *) " calling application module\\n", 30); |
| 1456 |
} |
| 1457 |
(*mptr->init.proc) (argc, argv, envp); |
| 1458 |
mptr = mptr->next; |
| 1459 |
} while (! (mptr == Modules.array[M2Dependent_ordered-M2Dependent_unregistered])); |
| 1460 |
} |
| 1461 |
} |
| 1462 |
|
| 1463 |
|
| 1464 |
/* |
| 1465 |
DeconstructModules - resolve dependencies and then call each |
| 1466 |
module constructor in turn. |
| 1467 |
*/ |
| 1468 |
|
| 1469 |
extern "C" void M2Dependent_DeconstructModules (void * applicationmodule, void * libname, int argc, void * argv, void * envp) |
| 1470 |
{ |
| 1471 |
M2Dependent_ModuleChain mptr; |
| 1472 |
|
| 1473 |
traceprintf3 (ModuleTrace, (const char *) "application module finishing: %s [%s]\\n", 39, applicationmodule, libname); |
| 1474 |
if (Modules.array[M2Dependent_ordered-M2Dependent_unregistered] == NULL) |
| 1475 |
{ |
| 1476 |
traceprintf (ModuleTrace, (const char *) " no ordered modules found during finishing\\n", 45); |
| 1477 |
} |
| 1478 |
else |
| 1479 |
{ |
| 1480 |
traceprintf (ModuleTrace, (const char *) "ExecuteTerminationProcedures\\n", 30); |
| 1481 |
M2Dependent_ExecuteTerminationProcedures (); |
| 1482 |
traceprintf (ModuleTrace, (const char *) "terminating modules in sequence\\n", 33); |
| 1483 |
mptr = Modules.array[M2Dependent_ordered-M2Dependent_unregistered]->prev; |
| 1484 |
do { |
| 1485 |
if (mptr->dependency.forc) |
| 1486 |
{ |
| 1487 |
traceprintf3 (ModuleTrace, (const char *) "finalizing module: %s [%s] for C\\n", 34, mptr->name, mptr->libname); |
| 1488 |
} |
| 1489 |
else |
| 1490 |
{ |
| 1491 |
traceprintf3 (ModuleTrace, (const char *) "finalizing module: %s [%s]\\n", 28, mptr->name, mptr->libname); |
| 1492 |
} |
| 1493 |
(*mptr->fini.proc) (argc, argv, envp); |
| 1494 |
mptr = mptr->prev; |
| 1495 |
} while (! (mptr == Modules.array[M2Dependent_ordered-M2Dependent_unregistered]->prev)); |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
|
| 1500 |
/* |
| 1501 |
RegisterModule - adds module name to the list of outstanding |
| 1502 |
modules which need to have their dependencies |
| 1503 |
explored to determine initialization order. |
| 1504 |
*/ |
| 1505 |
|
| 1506 |
extern "C" void M2Dependent_RegisterModule (void * modulename, void * libname, M2Dependent_ArgCVEnvP init, M2Dependent_ArgCVEnvP fini, PROC dependencies) |
| 1507 |
{ |
| 1508 |
M2Dependent_ModuleChain mptr; |
| 1509 |
|
| 1510 |
CheckInitialized (); |
| 1511 |
mptr = LookupModule (M2Dependent_unordered, modulename, libname); |
| 1512 |
if (mptr == NULL) |
| 1513 |
{ |
| 1514 |
traceprintf3 (ModuleTrace, (const char *) "module: %s [%s] registering", 27, modulename, libname); |
| 1515 |
moveTo (M2Dependent_unordered, CreateModule (modulename, libname, init, fini, dependencies)); |
| 1516 |
traceprintf (ModuleTrace, (const char *) "\\n", 2); |
| 1517 |
} |
| 1518 |
else |
| 1519 |
{ |
| 1520 |
warning3 ((const char *) "module: %s [%s] (ignoring duplicate registration)\\n", 51, modulename, libname); |
| 1521 |
} |
| 1522 |
} |
| 1523 |
|
| 1524 |
|
| 1525 |
/* |
| 1526 |
RequestDependant - used to specify that modulename is dependant upon |
| 1527 |
module dependantmodule. It only takes effect |
| 1528 |
if we are using DynamicInitialization. |
| 1529 |
*/ |
| 1530 |
|
| 1531 |
extern "C" void M2Dependent_RequestDependant (void * modulename, void * libname, void * dependantmodule, void * dependantlibname) |
| 1532 |
{ |
| 1533 |
CheckInitialized (); |
| 1534 |
PerformRequestDependant (modulename, libname, dependantmodule, dependantlibname); |
| 1535 |
} |
| 1536 |
|
| 1537 |
|
| 1538 |
/* |
| 1539 |
InstallTerminationProcedure - installs a procedure, p, which will |
| 1540 |
be called when the procedure |
| 1541 |
ExecuteTerminationProcedures |
| 1542 |
is invoked. It returns TRUE if the |
| 1543 |
procedure is installed. |
| 1544 |
*/ |
| 1545 |
|
| 1546 |
extern "C" bool M2Dependent_InstallTerminationProcedure (PROC p) |
| 1547 |
{ |
| 1548 |
return AppendProc (&TerminateProc, p); |
| 1549 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1550 |
__builtin_unreachable (); |
| 1551 |
} |
| 1552 |
|
| 1553 |
|
| 1554 |
/* |
| 1555 |
ExecuteInitialProcedures - executes the initial procedures installed by |
| 1556 |
InstallInitialProcedure. |
| 1557 |
*/ |
| 1558 |
|
| 1559 |
extern "C" void M2Dependent_ExecuteInitialProcedures (void) |
| 1560 |
{ |
| 1561 |
ExecuteReverse (InitialProc.tail); |
| 1562 |
} |
| 1563 |
|
| 1564 |
|
| 1565 |
/* |
| 1566 |
InstallInitialProcedure - installs a procedure to be executed just |
| 1567 |
before the BEGIN code section of the |
| 1568 |
main program module. |
| 1569 |
*/ |
| 1570 |
|
| 1571 |
extern "C" bool M2Dependent_InstallInitialProcedure (PROC p) |
| 1572 |
{ |
| 1573 |
return AppendProc (&InitialProc, p); |
| 1574 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 1575 |
__builtin_unreachable (); |
| 1576 |
} |
| 1577 |
|
| 1578 |
|
| 1579 |
/* |
| 1580 |
ExecuteTerminationProcedures - calls each installed termination procedure |
| 1581 |
in reverse order. |
| 1582 |
*/ |
| 1583 |
|
| 1584 |
extern "C" void M2Dependent_ExecuteTerminationProcedures (void) |
| 1585 |
{ |
| 1586 |
ExecuteReverse (TerminateProc.tail); |
| 1587 |
} |
| 1588 |
|
| 1589 |
extern "C" void _M2_M2Dependent_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[]) |
| 1590 |
{ |
| 1591 |
CheckInitialized (); |
| 1592 |
} |
| 1593 |
|
| 1594 |
extern "C" void _M2_M2Dependent_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[]) |
| 1595 |
{ |
| 1596 |
} |