summaryrefslogtreecommitdiff
path: root/ld/linksyms.c
blob: b5304a30b830f827a75211acff932f8b93598823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

/* linksyms.c - write binary file for linker */

/* Copyright (C) 1994 Bruce Evans */

#include "syshead.h"
#include "const.h"
#include "obj.h"
#include "type.h"
#undef EXTERN
#include "globvar.h"

FORWARD void linkrefs P((struct modstruct *modptr));
PUBLIC bool_t reloc_output = 0;

/* link all symbols connected to entry symbols */

PUBLIC void linksyms(argreloc_output)
bool_pt argreloc_output;
{
    char needlink;
    struct entrylist *elptr;
    struct modstruct *modptr;
    struct symstruct *symptr;

#ifdef REL_OUTPUT
    reloc_output = argreloc_output;
    if (argreloc_output)
    {
	if (modfirst->modnext != NUL_PTR)
	    fatalerror("relocatable output only works for one input file");
	for (modptr = modfirst; modptr != NUL_PTR; modptr = modptr->modnext)
	    modptr->loadflag = TRUE;
	return;
    }
#endif
    if ((symptr = findsym("_main")) != NUL_PTR)
	entrysym(symptr);
    do
    {
	if ((elptr = entryfirst) == NUL_PTR)
	    fatalerror("no start symbol");
	for (modptr = modfirst; modptr != NUL_PTR; modptr = modptr->modnext)
	    modptr->loadflag = FALSE;
	for (; elptr != NUL_PTR; elptr = elptr->elnext)
	    linkrefs(elptr->elsymptr->modptr);
	if ((symptr = findsym("start")) != NUL_PTR)
	    linkrefs(symptr->modptr);
	needlink = FALSE;
	{
	    struct redlist *prlptr = 0;
	    struct redlist *rlptr;

	    for (rlptr = redfirst; rlptr != NUL_PTR;
		 rlptr = (prlptr = rlptr)->rlnext)
		if (rlptr->rlmodptr->loadflag &&
		    rlptr->rlmodptr->class > rlptr->rlsymptr->modptr->class)
		{
		    rlptr->rlsymptr->modptr = rlptr->rlmodptr;
		    rlptr->rlsymptr->value = rlptr->rlvalue;
		    if (rlptr == redfirst)
			redfirst = rlptr->rlnext;
		    else
			prlptr->rlnext = rlptr->rlnext;
		    needlink = TRUE;
		}
	}
    }
    while (needlink);
}

PRIVATE void linkrefs(modptr)
struct modstruct *modptr;
{
    register struct symstruct **symparray;
    register struct symstruct *symptr;

    modptr->loadflag = TRUE;
    for (symparray = modptr->symparray;
	 (symptr = *symparray) != NUL_PTR; ++symparray)
	if (symptr->modptr->loadflag == FALSE)
	    linkrefs(symptr->modptr);
}