4 * (C) Copyright 1989-1995
12 * - lkpsym: Use StoreString for sym construction
13 * - change symeq() to do length-independent string compare
14 * - change hash() to do length-independent hash calculation
24 * The module lksym.c contains the functions that operate
25 * on the symbol structures.
27 * lksym.c contains the following functions:
38 * lksym.c contains no local/static variables.
41 /*)Function VOID syminit()
43 * The function syminit() is called to clear the hashtable.
46 * int h computed hash value
47 * sym ** spp pointer to an array of
48 * sym structure pointers
51 * sym * symhash[] array of pointers to NHASH
58 * (1) The symbol hash tables are cleared
68 while (spp < &symhash[NHASH])
72 /*)Function sym * newsym()
74 * The function newsym() is called to evaluate the symbol
75 * definition/reference directive from the .rel file(s).
76 * If the symbol is not found in the symbol table a new
77 * symbol structure is created. Evaluation of the
78 * directive determines if this is a reference or a definition.
79 * Multiple definitions of the same variable will be flagged
80 * as an error if the values are not identical. A symbol
81 * definition places the symbol value and area extension
82 * into the symbols data structure. And finally, a pointer
83 * to the symbol structure is placed into the head structure
84 * symbol list. Refer to the description of the header, symbol,
85 * area, and areax structures in lkdata.c for structure and
89 * int c character from input text
90 * int i evaluation value
91 * char id[] symbol name
92 * int nglob number of symbols in this header
93 * sym * tsp pointer to symbol structure
94 * sym ** s list of pointers to symbol structures
97 * areax *axp Pointer to the current
99 * head *headp The pointer to the first
100 * head structure of a linked list
101 * int lkerr error flag
104 * Addr_T eval() lkeval.c
105 * VOID exit() c_library
106 * int fprintf() c_library
107 * char getSid() lklex.c
109 * char getnb() lklex.c
110 * sym * lkpsym() lksym.c
113 * A symbol structure is created and/or modified.
114 * If structure space allocation fails linker will abort.
115 * Several severe errors (these are internal errors
116 * indicating a corrupted .rel file or corrupted
117 * assembler or linker) will terminated the linker.
121 * Find/Create a global symbol entry.
126 * | `----- sp->s_type
127 * `------------ sp->s_id
133 register unsigned i ;
134 register unsigned nglob ;
140 getSid(id); // old: getid(id, -1);
142 c = getnb();get();get();
144 tsp->s_type |= S_REF;
146 fprintf(stderr, "Non zero S_REF\n");
152 if (tsp->s_type & S_DEF && tsp->s_addr != i) {
153 fprintf(stderr, "Multiple definition of %8s\n", id);
156 tsp->s_type |= S_DEF;
158 * Set value and area extension link.
163 fprintf(stderr, "Invalid symbol type %c for %8s\n", c, id);
167 * Place pointer in header symbol list
170 fprintf(stderr, "No header defined\n");
175 for (i=0; i < nglob ;++i) {
181 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 (strcmp( p1, p2 ) == 0);
403 return (as_strcmpi( p1, p2 ) == 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
449 /*)Function VOID * new(n)
451 * unsigned int n allocation size in bytes
453 * The function new() allocates n bytes of space and returns
454 * a pointer to this memory. If no space is available the
455 * linker is terminated.
458 * char * p a general pointer
459 * char * q a general pointer
465 * int fprintf() c_library
466 * VOID * malloc() c_library
469 * Memory is allocated, if allocation fails
470 * the linker is terminated.
479 if ((p = (char *) calloc(n, 1)) == NULL) {
480 fprintf(stderr, "Out of space!\n");