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 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");
181 /*)Function sym * lkpsym(id,f)
183 * char * id symbol name string
184 * int f f == 0, lookup only
185 * f != 0, create if not found
187 * The function lookup() searches the symbol hash tables for
188 * a symbol name match returning a pointer to the sym structure.
189 * If the symbol is not found then a sym structure is created,
190 * initialized, and linked to the appropriate hash table if f != 0.
191 * A pointer to this new sym structure is returned or a NULL
192 * pointer is returned if f == 0.
195 * int h computed hash value
196 * sym * sp pointer to a sym structure
199 * sym * symhash[] array of pointers to NHASH
200 * linked symbol lists
204 * VOID * new() lksym.c
205 * int symeq() lksym.c
208 * If the function new() fails to allocate space
209 * for the new sym structure the linker terminates.
216 register struct sym *sp;
222 if (symeq(id, sp->s_id))
228 sp = (struct sym *) new (sizeof(struct sym));
229 sp->s_sp = symhash[h];
231 strncpy(sp->s_id, id, NCPS);
235 /*)Function addr_t symval(tsp)
237 * sym * tsp pointer to a symbol structure
239 * The function symval() returns the value of the
240 * relocated symbol by adding the variable definition
241 * value to the areax base address.
244 * addr_t val relocated address value
258 register struct sym *tsp;
264 val += tsp->s_axp->a_addr;
269 /*)Function VOID symdef(fp)
271 * FILE * fp file handle for output
273 * The function symdef() scans the hashed symbol table
274 * searching for variables referenced but not defined.
275 * Undefined variables are linked to the default
276 * area "_CODE" and reported as referenced by the
277 * appropriate module.
280 * int i hash table index loop variable
281 * sym * sp pointer to linked symbol structure
284 * area *areap The pointer to the first
285 * area structure of a linked list
286 * sym *symhash[NHASH] array of pointers to NHASH
287 * linked symbol lists
293 * Undefined variables have their areas set to "_CODE".
300 register struct sym *sp;
303 for (i=0; i<NHASH; ++i) {
306 if (sp->s_axp == NULL)
307 sp->s_axp = areap->a_axp;
308 if ((sp->s_type & S_DEF) == 0)
315 /*)Function VOID symmod(fp,tsp)
317 * FILE * fp output file handle
318 * sym * tsp pointer to a symbol structure
320 * The function symmod() scans the header structures
321 * searching for a reference to the symbol structure
322 * pointer to by tsp. The function then generates an error
323 * message whichs names the module having referenced the
324 * undefined variable.
328 * sym ** p pointer to a list of pointers
329 * to symbol structures
332 * head *headp The pointer to the first
333 * head structure of a linked list
334 * head *hp Pointer to the current
336 * int lkerr error flag
339 * int fprintf() c_library
342 * Error output generated.
353 if ((hp = headp) != NULL) {
356 for (i=0; i<hp->h_nglob; ++i) {
358 fprintf(fp, "\n?ASlink-Warning-Undefined Global %s ", tsp->s_id);
359 fprintf(fp, "referenced by module %s\n", hp->m_id);
368 /*)Function int symeq(p1, p2)
370 * char * p1 name string
371 * char * p2 name string
373 * The function symeq() compares the two name strings for a match.
374 * The return value is 1 for a match and 0 for no match.
380 * char ccase[] an array of characters which
381 * perform the case translation function
393 register char *p1, *p2;
404 if (ccase[*p1++] != ccase[*p2++])
412 /*)Function int hash(p)
414 * char * p pointer to string to hash
416 * The function hash() computes a hash code using the sum
417 * of all characters mod table size algorithm.
420 * int h accumulated character sum
424 * char ccase[] an array of characters which
425 * perform the case translation function
455 /*)Function VOID * new(n)
457 * unsigned int n allocation size in bytes
459 * The function new() allocates n bytes of space and returns
460 * a pointer to this memory. If no space is available the
461 * linker is terminated.
464 * char * p a general pointer
465 * char * q a general pointer
471 * int fprintf() c_library
472 * VOID * malloc() c_library
475 * Memory is allocated, if allocation fails
476 * the linker is terminated.
484 register unsigned int i;
486 if ((p = (char *) malloc(n)) == NULL) {
487 fprintf(stderr, "Out of space!\n");
490 for (i=0,q=p; i<n; i++) {