2003-08-30 12:47:54 -04:00
|
|
|
/* stab.c: Read and manage symbol tables from object modules.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
|
2009-12-19 07:28:26 -05:00
|
|
|
* Copyright 2002, 2003 Sam Hocevar <sam@hocevar.net>, Paris
|
2003-08-30 12:47:54 -04:00
|
|
|
*
|
|
|
|
* This software was derived from Elk 1.2, which was Copyright 1987, 1988,
|
|
|
|
* 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
|
|
|
|
* by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
|
|
|
|
* between TELES and Nixdorf Microprocessor Engineering, Berlin).
|
|
|
|
*
|
|
|
|
* Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
|
|
|
|
* owners or individual owners of copyright in this software, grant to any
|
|
|
|
* person or company a worldwide, royalty free, license to
|
|
|
|
*
|
|
|
|
* i) copy this software,
|
|
|
|
* ii) prepare derivative works based on this software,
|
|
|
|
* iii) distribute copies of this software or derivative works,
|
|
|
|
* iv) perform this software, or
|
|
|
|
* v) display this software,
|
|
|
|
*
|
|
|
|
* provided that this notice is not removed and that neither Oliver Laumann
|
|
|
|
* nor Teles nor Nixdorf are deemed to have made any representations as to
|
|
|
|
* the suitability of this software for any purpose nor are held responsible
|
|
|
|
* for any defects of this software.
|
|
|
|
*
|
|
|
|
* THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
|
2003-08-19 15:19:38 -04:00
|
|
|
*/
|
|
|
|
|
2003-09-06 07:25:29 -04:00
|
|
|
#include "config.h"
|
2003-08-19 15:19:38 -04:00
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2003-09-06 07:25:29 -04:00
|
|
|
#include "kernel.h"
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
void Free_Symbols (SYMTAB *);
|
|
|
|
|
2003-09-07 05:55:56 -04:00
|
|
|
#if defined(CAN_LOAD_LIB) || defined (INIT_OBJECTS)
|
2003-08-19 15:19:38 -04:00
|
|
|
|
2003-09-07 07:25:04 -04:00
|
|
|
/*#if defined(MACH_O)
|
2003-08-19 15:19:38 -04:00
|
|
|
# include "stab-macho.c"
|
2003-09-06 10:45:29 -04:00
|
|
|
#elif defined(HAVE_LIBELF)
|
2003-08-19 15:19:38 -04:00
|
|
|
# include "stab-elf.c"
|
2003-08-25 11:01:22 -04:00
|
|
|
#elif defined(COFF) || defined(XCOFF)
|
2003-08-19 15:19:38 -04:00
|
|
|
# include "stab-coff.c"
|
2003-08-25 11:01:22 -04:00
|
|
|
#elif defined(ECOFF)
|
2003-08-19 15:19:38 -04:00
|
|
|
# include "stab-ecoff.c"
|
2003-08-25 11:01:22 -04:00
|
|
|
#elif defined(CONVEX_AOUT)
|
2003-08-19 15:19:38 -04:00
|
|
|
# include "stab-convex.c"
|
2003-08-25 11:01:22 -04:00
|
|
|
#elif defined(hp9000s300) || defined(__hp9000s300) || defined(__hp9000s300__)
|
2003-08-19 15:19:38 -04:00
|
|
|
# include "stab-hp9k300.c"
|
2003-08-25 11:01:22 -04:00
|
|
|
#elif defined(hp9000s800) || defined(__hp9000s800) || defined(__hp9000s800__)
|
2003-08-19 15:19:38 -04:00
|
|
|
# include "stab-hp9k800.c"
|
2003-09-07 07:25:04 -04:00
|
|
|
#elif defined(HAVE_A_OUT_H)
|
2003-08-19 15:19:38 -04:00
|
|
|
# include "stab-bsd.c"
|
2003-09-07 07:25:04 -04:00
|
|
|
#else
|
|
|
|
# include "stab-unix.c"
|
|
|
|
#endif*/
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
static SYMPREFIX Ignore_Prefixes[] = {
|
|
|
|
/* Currently none */
|
2003-08-19 15:24:23 -04:00
|
|
|
{ 0, 0 }
|
2003-08-19 15:19:38 -04:00
|
|
|
};
|
|
|
|
static SYMPREFIX Init_Prefixes[] = {
|
2003-08-19 15:24:23 -04:00
|
|
|
{ INIT_PREFIX, PR_EXTENSION },
|
|
|
|
{ "_GLOBAL_.I.", PR_CONSTRUCTOR }, /* SVR4.2/g++ */
|
|
|
|
{ "__sti__", PR_CONSTRUCTOR },
|
|
|
|
{ "_STI", PR_CONSTRUCTOR },
|
|
|
|
{ "_GLOBAL_$I$", PR_CONSTRUCTOR },
|
|
|
|
{ 0, 0 }
|
2003-08-19 15:19:38 -04:00
|
|
|
};
|
|
|
|
static SYMPREFIX Finit_Prefixes[] = {
|
2003-08-19 15:24:23 -04:00
|
|
|
{ FINIT_PREFIX, PR_EXTENSION },
|
|
|
|
{ "_GLOBAL_.D.", PR_CONSTRUCTOR },
|
|
|
|
{ "__std__", PR_CONSTRUCTOR },
|
|
|
|
{ "_STD", PR_CONSTRUCTOR },
|
|
|
|
{ "_GLOBAL_$D$", PR_CONSTRUCTOR },
|
|
|
|
{ 0, 0 }
|
2003-08-19 15:19:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static FUNCT *Finalizers;
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
static void Call (unsigned long int l) {
|
2003-08-19 15:19:38 -04:00
|
|
|
#ifdef XCOFF
|
2003-08-19 15:24:23 -04:00
|
|
|
unsigned long int vec[3];
|
2003-08-19 15:19:38 -04:00
|
|
|
extern main();
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
memcpy (vec, main, sizeof vec);
|
2003-08-19 15:19:38 -04:00
|
|
|
vec[0] = (l & ~0xF0000000) + (vec[0] & 0xF0000000);
|
|
|
|
((void (*)())vec)();
|
|
|
|
#else
|
|
|
|
((void (*)())l)();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
void Call_Initializers (SYMTAB *tab, char *addr, int which) {
|
2003-08-19 15:19:38 -04:00
|
|
|
SYM *sp;
|
|
|
|
char *p;
|
|
|
|
SYMPREFIX *pp;
|
|
|
|
FUNCT *fp, **fpp;
|
|
|
|
|
|
|
|
/* Set pointer to end of list of finalizers; extension finalization
|
|
|
|
* functions and C++ static destructors will be appended to this list:
|
|
|
|
*/
|
|
|
|
for (fpp = &Finalizers; *fpp; fpp = &(*fpp)->next)
|
2003-09-02 04:12:11 -04:00
|
|
|
;
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
for (sp = tab->first; sp; sp = sp->next) {
|
2003-09-07 09:12:27 -04:00
|
|
|
if (!sp->value || (char *)sp->value < addr)
|
2003-09-02 04:12:11 -04:00
|
|
|
continue;
|
|
|
|
p = sp->name;
|
2003-08-19 15:19:38 -04:00
|
|
|
#ifdef SYMS_BEGIN_WITH
|
2003-09-02 04:12:11 -04:00
|
|
|
if (*p == SYMS_BEGIN_WITH)
|
|
|
|
p++;
|
|
|
|
else
|
|
|
|
continue;
|
2003-08-19 15:19:38 -04:00
|
|
|
#endif
|
2003-09-02 04:12:11 -04:00
|
|
|
for (pp = Ignore_Prefixes; pp->name; pp++)
|
|
|
|
if (strncmp (p, pp->name, strlen (pp->name)) == 0)
|
|
|
|
goto next;
|
|
|
|
for (pp = Init_Prefixes; pp->name; pp++) {
|
|
|
|
if (pp->type == which
|
|
|
|
&& strncmp (p, pp->name, strlen (pp->name)) == 0) {
|
|
|
|
if (Verb_Init)
|
|
|
|
printf ("[calling %s]\n", p);
|
|
|
|
Call (sp->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Append to list of finalizers (to be invoked on exit):
|
|
|
|
*/
|
|
|
|
for (pp = Finit_Prefixes; pp->name; pp++) {
|
|
|
|
if (pp->type == which
|
|
|
|
&& strncmp (p, pp->name, strlen (pp->name)) == 0) {
|
|
|
|
fp = (FUNCT *)Safe_Malloc (sizeof (FUNCT));
|
|
|
|
fp->func = (void (*)())sp->value;
|
|
|
|
fp->name = Safe_Malloc (strlen (p) + 1);
|
|
|
|
strcpy (fp->name, p);
|
|
|
|
fp->next = 0;
|
|
|
|
*fpp = fp;
|
|
|
|
fpp = &fp->next;
|
|
|
|
}
|
|
|
|
}
|
2003-08-19 15:19:38 -04:00
|
|
|
next: ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the finialization functions and C++ static destructors. Make sure
|
|
|
|
* that calling exit() from a function doesn't cause endless recursion.
|
|
|
|
*/
|
2003-08-19 15:24:23 -04:00
|
|
|
void Call_Finalizers () {
|
2003-08-19 15:19:38 -04:00
|
|
|
while (Finalizers) {
|
2003-09-02 04:12:11 -04:00
|
|
|
FUNCT *fp = Finalizers;
|
|
|
|
Finalizers = fp->next;
|
|
|
|
if (Verb_Init)
|
|
|
|
printf ("[calling %s]\n", fp->name);
|
|
|
|
Call ((unsigned long int)fp->func);
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
void Free_Symbols (SYMTAB *tab) {
|
2003-08-19 15:19:38 -04:00
|
|
|
register SYM *sp, *nextp;
|
|
|
|
|
|
|
|
for (sp = tab->first; sp; sp = nextp) {
|
2003-09-02 04:12:11 -04:00
|
|
|
nextp = sp->next;
|
2003-08-19 15:19:38 -04:00
|
|
|
#if defined(COFF) || defined(ECOFF)
|
2003-09-02 04:12:11 -04:00
|
|
|
free (sp->name);
|
2003-08-19 15:19:38 -04:00
|
|
|
#endif
|
2003-09-02 04:12:11 -04:00
|
|
|
free ((char *)sp);
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
if (tab->strings)
|
2003-09-02 04:12:11 -04:00
|
|
|
free (tab->strings);
|
2003-08-19 15:19:38 -04:00
|
|
|
free ((char *)tab);
|
|
|
|
}
|
2003-09-07 07:25:04 -04:00
|
|
|
#endif /* CAN_LOAD_LIB || INIT_OBJECTS */
|