4 * (C) Copyright 1989-1995
19 * The module lksym.c contains the functions that operate
20 * on the symbol structures.
22 * lksym.c contains the following functions:
33 * lksym.c contains no local/static variables.
36 /*)Function VOID syminit()
38 * The function syminit() is called to clear the hashtable.
41 * int h computed hash value
42 * sym ** spp pointer to an array of
43 * sym structure pointers
46 * sym * symhash[] array of pointers to NHASH
53 * (1) The symbol hash tables are cleared
63 while (spp < &symhash[NHASH])
67 /*)Function sym * newsym()
69 * The function newsym() is called to evaluate the symbol
70 * definition/reference directive from the .rel file(s).
71 * If the symbol is not found in the symbol table a new
72 * symbol structure is created. Evaluation of the
73 * directive determines if this is a reference or a definition.
74 * Multiple definitions of the same variable will be flagged
75 * as an error if the values are not identical. A symbol
76 * definition places the symbol value and area extension
77 * into the symbols data structure. And finally, a pointer
78 * to the symbol structure is placed into the head structure
79 * symbol list. Refer to the description of the header, symbol,
80 * area, and areax structures in lkdata.c for structure and
84 * int c character from input text
85 * int i evaluation value
86 * char id[] symbol name
87 * int nglob number of symbols in this header
88 * sym * tsp pointer to symbol structure
89 * sym ** s list of pointers to symbol structures
92 * areax *axp Pointer to the current
94 * head *headp The pointer to the first
95 * head structure of a linked list
96 * int lkerr error flag
99 * Addr_T eval() lkeval.c
100 * VOID exit() c_library
101 * int fprintf() c_library
103 * char getnb() lklex.c
104 * sym * lkpsym() lksym.c
107 * A symbol structure is created and/or modified.
108 * If structure space allocation fails linker will abort.
109 * Several severe errors (these are internal errors
110 * indicating a corrupted .rel file or corrupted
111 * assembler or linker) will terminated the linker.
115 * Find/Create a global symbol entry.
120 * | `----- sp->s_type
121 * `------------ sp->s_id
127 register int c, i, nglob;
134 c = getnb();get();get();
136 tsp->s_type |= S_REF;
138 fprintf(stderr, "Non zero S_REF\n");
144 if (tsp->s_type & S_DEF && tsp->s_addr != i) {
146 fprintf(stderr, "Multiple definition of %s\n", id);
148 fprintf(stderr, "Multiple definition of %.8s\n", id);
152 tsp->s_type |= S_DEF;
154 * Set value and area extension link.
159 fprintf(stderr, "Invalid symbol type %c for %.8s\n", c, id);
163 * Place pointer in header symbol list
166 fprintf(stderr, "No header defined\n");
171 for (i=0; i < nglob ;++i) {
177 fprintf(stderr, "Header symbol list overflow\n");
182 /*)Function sym * lkpsym(id,f)
184 * char * id symbol name string
185 * int f f == 0, lookup only
186 * f != 0, create if not found
188 * The function lookup() searches the symbol hash tables for
189 * a symbol name match returning a pointer to the sym structure.
190 * If the symbol is not found then a sym structure is created,
191 * initialized, and linked to the appropriate hash table if f != 0.
192 * A pointer to this new sym structure is returned or a NULL
193 * pointer is returned if f == 0.
196 * int h computed hash value
197 * sym * sp pointer to a sym structure
200 * sym * symhash[] array of pointers to NHASH
201 * linked symbol lists
205 * VOID * new() lksym.c
206 * int symeq() lksym.c
209 * If the function new() fails to allocate space
210 * for the new sym structure the linker terminates.
217 register struct sym *sp;
223 if (symeq(id, sp->s_id))
229 sp = (struct sym *) new (sizeof(struct sym));
230 sp->s_sp = symhash[h];
232 strncpy(sp->s_id, id, NCPS);
236 /*)Function Addr_T symval(tsp)
238 * sym * tsp pointer to a symbol structure
240 * The function symval() returns the value of the
241 * relocated symbol by adding the variable definition
242 * value to the areax base address.
245 * Addr_T val relocated address value
259 register struct sym *tsp;
265 val += tsp->s_axp->a_addr;
270 /*)Function VOID symdef(fp)
272 * FILE * fp file handle for output
274 * The function symdef() scans the hashed symbol table
275 * searching for variables referenced but not defined.
276 * Undefined variables are linked to the default
277 * area "_CODE" and reported as referenced by the
278 * appropriate module.
281 * int i hash table index loop variable
282 * sym * sp pointer to linked symbol structure
285 * area *areap The pointer to the first
286 * area structure of a linked list
287 * sym *symhash[NHASH] array of pointers to NHASH
288 * linked symbol lists
294 * Undefined variables have their areas set to "_CODE".
301 register struct sym *sp;
304 for (i=0; i<NHASH; ++i) {
307 if (sp->s_axp == NULL)
308 sp->s_axp = areap->a_axp;
309 if ((sp->s_type & S_DEF) == 0)
316 /*)Function VOID symmod(fp,tsp)
318 * FILE * fp output file handle
319 * sym * tsp pointer to a symbol structure
321 * The function symmod() scans the header structures
322 * searching for a reference to the symbol structure
323 * pointer to by tsp. The function then generates an error
324 * message whichs names the module having referenced the
325 * undefined variable.
329 * sym ** p pointer to a list of pointers
330 * to symbol structures
333 * head *headp The pointer to the first
334 * head structure of a linked list
335 * head *hp Pointer to the current
337 * int lkerr error flag
340 * int fprintf() c_library
343 * Error output generated.
354 if ((hp = headp) != NULL) {
357 for (i=0; i<hp->h_nglob; ++i) {
359 fprintf(fp, "\n?ASlink-Warning-Undefined Global %s ", tsp->s_id);
360 fprintf(fp, "referenced by module %s\n", hp->m_id);
369 /*)Function int symeq(p1, p2)
371 * char * p1 name string
372 * char * p2 name string
374 * The function symeq() compares the two name strings for a match.
375 * The return value is 1 for a match and 0 for no match.
381 * char ccase[] an array of characters which
382 * perform the case translation function
394 register char *p1, *p2;
405 if (ccase[*p1++] != ccase[*p2++])
413 /*)Function int hash(p)
415 * char * p pointer to string to hash
417 * The function hash() computes a hash code using the sum
418 * of all characters mod table size algorithm.
421 * int h accumulated character sum
425 * char ccase[] an array of characters which
426 * perform the case translation function
456 /*)Function VOID * new(n)
458 * unsigned int n allocation size in bytes
460 * The function new() allocates n bytes of space and returns
461 * a pointer to this memory. If no space is available the
462 * linker is terminated.
465 * char * p a general pointer
466 * char * q a general pointer
472 * int fprintf() c_library
473 * VOID * malloc() c_library
476 * Memory is allocated, if allocation fails
477 * the linker is terminated.
485 register unsigned int i;
487 if ((p = (char *) malloc(n)) == NULL) {
488 fprintf(stderr, "Out of space!\n");
491 for (i=0,q=p; i<n; i++) {