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
62 while (spp < &symhash[NHASH])
66 /*)Function sym * newsym()
68 * The function newsym() is called to evaluate the symbol
69 * definition/reference directive from the .rel file(s).
70 * If the symbol is not found in the symbol table a new
71 * symbol structure is created. Evaluation of the
72 * directive determines if this is a reference or a definition.
73 * Multiple definitions of the same variable will be flagged
74 * as an error if the values are not identical. A symbol
75 * definition places the symbol value and area extension
76 * into the symbols data structure. And finally, a pointer
77 * to the symbol structure is placed into the head structure
78 * symbol list. Refer to the description of the header, symbol,
79 * area, and areax structures in lkdata.c for structure and
83 * int c character from input text
84 * int i evaluation value
85 * char id[] symbol name
86 * int nglob number of symbols in this header
87 * sym * tsp pointer to symbol structure
88 * sym ** s list of pointers to symbol structures
91 * areax *axp Pointer to the current
93 * head *headp The pointer to the first
94 * head structure of a linked list
95 * int lkerr error flag
98 * Addr_T eval() lkeval.c
99 * VOID exit() c_library
100 * int fprintf() c_library
101 * char getSid() lklex.c
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 unsigned i ;
128 register unsigned nglob ;
134 getSid(id); // old: getid(id, -1);
136 c = getnb();get();get();
138 tsp->s_type |= S_REF;
140 fprintf(stderr, "Non zero S_REF\n");
146 if (tsp->s_type & S_DEF && tsp->s_addr != i) {
148 fprintf(stderr, "Multiple definition of %s\n", id);
150 fprintf(stderr, "Multiple definition of %.8s\n", id);
154 tsp->s_type |= S_DEF;
156 * Set value and area extension link.
161 fprintf(stderr, "Invalid symbol type %c for %.8s\n", c, id);
165 * Place pointer in header symbol list
168 fprintf(stderr, "No header defined\n");
173 for (i=0; i < nglob ;++i) {
179 fprintf(stderr, "Header symbol list overflow\n");
186 /*)Function sym * lkpsym(id,f)
188 * char * id symbol name string
189 * int f f == 0, lookup only
190 * f != 0, create if not found
192 * The function lookup() searches the symbol hash tables for
193 * a symbol name match returning a pointer to the sym structure.
194 * If the symbol is not found then a sym structure is created,
195 * initialized, and linked to the appropriate hash table if f != 0.
196 * A pointer to this new sym structure is returned or a NULL
197 * pointer is returned if f == 0.
200 * int h computed hash value
201 * sym * sp pointer to a sym structure
204 * sym * symhash[] array of pointers to NHASH
205 * linked symbol lists
209 * VOID * new() lksym.c
210 * int symeq() lksym.c
213 * If the function new() fails to allocate space
214 * for the new sym structure the linker terminates.
221 register struct sym *sp;
227 if (symeq(id, sp->s_id))
233 sp = (struct sym *) new (sizeof(struct sym));
234 sp->s_sp = symhash[h];
236 sp->s_id = StoreString( id ); /* JLH */
240 /*)Function Addr_T symval(tsp)
242 * sym * tsp pointer to a symbol structure
244 * The function symval() returns the value of the
245 * relocated symbol by adding the variable definition
246 * value to the areax base address.
249 * Addr_T val relocated address value
263 register struct sym *tsp;
269 val += tsp->s_axp->a_addr;
274 /*)Function VOID symdef(fp)
276 * FILE * fp file handle for output
278 * The function symdef() scans the hashed symbol table
279 * searching for variables referenced but not defined.
280 * Undefined variables are linked to the default
281 * area "_CODE" and reported as referenced by the
282 * appropriate module.
285 * int i hash table index loop variable
286 * sym * sp pointer to linked symbol structure
289 * area *areap The pointer to the first
290 * area structure of a linked list
291 * sym *symhash[NHASH] array of pointers to NHASH
292 * linked symbol lists
298 * Undefined variables have their areas set to "_CODE".
305 register struct sym *sp;
308 for (i=0; i<NHASH; ++i) {
311 if (sp->s_axp == NULL)
312 sp->s_axp = areap->a_axp;
313 if ((sp->s_type & S_DEF) == 0)
320 /*)Function VOID symmod(fp,tsp)
322 * FILE * fp output file handle
323 * sym * tsp pointer to a symbol structure
325 * The function symmod() scans the header structures
326 * searching for a reference to the symbol structure
327 * pointer to by tsp. The function then generates an error
328 * message whichs names the module having referenced the
329 * undefined variable.
333 * sym ** p pointer to a list of pointers
334 * to symbol structures
337 * head *headp The pointer to the first
338 * head structure of a linked list
339 * head *hp Pointer to the current
341 * int lkerr error flag
344 * int fprintf() c_library
347 * Error output generated.
358 if ((hp = headp) != NULL) {
361 for (i=0; i<hp->h_nglob; ++i) {
363 fprintf(fp, "\n?ASlink-Warning-Undefined Global '%s' ", tsp->s_id);
364 fprintf(fp, "referenced by module '%s'\n", hp->m_id);
373 /*)Function int symeq(p1, p2)
375 * char * p1 name string
376 * char * p2 name string
378 * The function symeq() compares the two name strings for a match.
379 * The return value is 1 for a match and 0 for no match.
385 * char ccase[] an array of characters which
386 * perform the case translation function
398 register char *p1, *p2;
401 return (strncmp( p1, p2, NCPS ) == 0);
403 return (as_strncmpi( p1, p2, NCPS ) == 0);
407 /*)Function int hash(p)
409 * char * p pointer to string to hash
411 * The function hash() computes a hash code using the sum
412 * of all characters mod table size algorithm.
415 * int h accumulated character sum
419 * char ccase[] an array of characters which
420 * perform the case translation function
443 h += ccase[(unsigned char)(*p++)];
450 /*)Function VOID * new(n)
452 * unsigned int n allocation size in bytes
454 * The function new() allocates n bytes of space and returns
455 * a pointer to this memory. If no space is available the
456 * linker is terminated.
459 * char * p a general pointer
460 * char * q a general pointer
466 * int fprintf() c_library
467 * VOID * malloc() c_library
470 * Memory is allocated, if allocation fails
471 * the linker is terminated.
480 if ((p = (char *) calloc(n, 1)) == NULL) {
481 fprintf(stderr, "Out of space!\n");