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
108 * char getnb() lklex.c
109 * sym * lkpsym() lksym.c
112 * A symbol structure is created and/or modified.
113 * If structure space allocation fails linker will abort.
114 * Several severe errors (these are internal errors
115 * indicating a corrupted .rel file or corrupted
116 * assembler or linker) will terminated the linker.
120 * Find/Create a global symbol entry.
125 * | `----- sp->s_type
126 * `------------ sp->s_id
132 register int c, i, nglob;
139 c = getnb();get();get();
141 tsp->s_type |= S_REF;
143 fprintf(stderr, "Non zero S_REF\n");
149 if (tsp->s_type & S_DEF && tsp->s_addr != i) {
150 fprintf(stderr, "Multiple definition of %8s\n", id);
153 tsp->s_type |= S_DEF;
155 * Set value and area extension link.
160 fprintf(stderr, "Invalid symbol type %c for %8s\n", c, id);
164 * Place pointer in header symbol list
167 fprintf(stderr, "No header defined\n");
172 for (i=0; i < nglob ;++i) {
178 fprintf(stderr, "Header symbol list overflow\n");
183 /*)Function sym * lkpsym(id,f)
185 * char * id symbol name string
186 * int f f == 0, lookup only
187 * f != 0, create if not found
189 * The function lookup() searches the symbol hash tables for
190 * a symbol name match returning a pointer to the sym structure.
191 * If the symbol is not found then a sym structure is created,
192 * initialized, and linked to the appropriate hash table if f != 0.
193 * A pointer to this new sym structure is returned or a NULL
194 * pointer is returned if f == 0.
197 * int h computed hash value
198 * sym * sp pointer to a sym structure
201 * sym * symhash[] array of pointers to NHASH
202 * linked symbol lists
206 * VOID * new() lksym.c
207 * int symeq() lksym.c
210 * If the function new() fails to allocate space
211 * for the new sym structure the linker terminates.
218 register struct sym *sp;
224 if (symeq(id, sp->s_id))
230 sp = (struct sym *) new (sizeof(struct sym));
231 sp->s_sp = symhash[h];
233 sp->s_id = StoreString( id ); /* JLH */
237 /*)Function addr_t symval(tsp)
239 * sym * tsp pointer to a symbol structure
241 * The function symval() returns the value of the
242 * relocated symbol by adding the variable definition
243 * value to the areax base address.
246 * addr_t val relocated address value
260 register struct sym *tsp;
266 val += tsp->s_axp->a_addr;
271 /*)Function VOID symdef(fp)
273 * FILE * fp file handle for output
275 * The function symdef() scans the hashed symbol table
276 * searching for variables referenced but not defined.
277 * Undefined variables are linked to the default
278 * area "_CODE" and reported as referenced by the
279 * appropriate module.
282 * int i hash table index loop variable
283 * sym * sp pointer to linked symbol structure
286 * area *areap The pointer to the first
287 * area structure of a linked list
288 * sym *symhash[NHASH] array of pointers to NHASH
289 * linked symbol lists
295 * Undefined variables have their areas set to "_CODE".
302 register struct sym *sp;
305 for (i=0; i<NHASH; ++i) {
308 if (sp->s_axp == NULL)
309 sp->s_axp = areap->a_axp;
310 if ((sp->s_type & S_DEF) == 0)
317 /*)Function VOID symmod(fp,tsp)
319 * FILE * fp output file handle
320 * sym * tsp pointer to a symbol structure
322 * The function symmod() scans the header structures
323 * searching for a reference to the symbol structure
324 * pointer to by tsp. The function then generates an error
325 * message whichs names the module having referenced the
326 * undefined variable.
330 * sym ** p pointer to a list of pointers
331 * to symbol structures
334 * head *headp The pointer to the first
335 * head structure of a linked list
336 * head *hp Pointer to the current
338 * int lkerr error flag
341 * int fprintf() c_library
344 * Error output generated.
355 if ((hp = headp) != NULL) {
358 for (i=0; i<hp->h_nglob; ++i) {
360 fprintf(fp, "\n?ASlink-Warning-Undefined Global '%s' ", tsp->s_id);
361 fprintf(fp, "referenced by module '%s'\n", hp->m_id);
370 /*)Function int symeq(p1, p2)
372 * char * p1 name string
373 * char * p2 name string
375 * The function symeq() compares the two name strings for a match.
376 * The return value is 1 for a match and 0 for no match.
382 * char ccase[] an array of characters which
383 * perform the case translation function
395 register char *p1, *p2;
398 return (strcmp( p1, p2 ) == 0);
400 return (strcmpi( p1, p2 ) == 0);
404 /*)Function int hash(p)
406 * char * p pointer to string to hash
408 * The function hash() computes a hash code using the sum
409 * of all characters mod table size algorithm.
412 * int h accumulated character sum
416 * char ccase[] an array of characters which
417 * perform the case translation function
446 /*)Function VOID * new(n)
448 * unsigned int n allocation size in bytes
450 * The function new() allocates n bytes of space and returns
451 * a pointer to this memory. If no space is available the
452 * linker is terminated.
455 * char * p a general pointer
456 * char * q a general pointer
462 * int fprintf() c_library
463 * VOID * malloc() c_library
466 * Memory is allocated, if allocation fails
467 * the linker is terminated.
475 register unsigned int i;
477 if ((p = (char *) malloc(n)) == NULL) {
478 fprintf(stderr, "Out of space!\n");
481 for (i=0,q=p; i<n; i++) {