scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

symbol.c (2303B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include <scc/mach.h>
      6 #include <scc/scc.h>
      7 
      8 #include "ld.h"
      9 
     10 #define NR_SYMBOL 128
     11 #define NR_SECTIONS 32
     12 
     13 static Symbol *symtab[NR_SYMBOL];
     14 static Section *sectab[NR_SECTIONS];
     15 static Section *seclast;
     16 
     17 Section *sechead;
     18 
     19 Symbol *
     20 lookup(char *name)
     21 {
     22 	unsigned h;
     23 	Symbol *sym;
     24 
     25 	h = genhash(name) % NR_SYMBOL;
     26 	for (sym = symtab[h]; sym; sym = sym->hash) {
     27 		if (!strcmp(name, sym->name))
     28 			return sym;
     29 	}
     30 
     31 	return NULL;
     32 }
     33 
     34 Symbol *
     35 install(char *name)
     36 {
     37 	unsigned h;
     38 	size_t len;
     39 	Symbol *sym;
     40 	char *s;
     41 
     42 	h = genhash(name) % NR_SYMBOL;
     43 
     44 	len = strlen(name) + 1;
     45 	s = malloc(len);
     46 	sym = malloc(sizeof(*sym));
     47 	if (!s || !sym) {
     48 		error("out of memory");
     49 		exit(EXIT_FAILURE);
     50 	}
     51 
     52 	sym->obj = NULL;
     53 	sym->name = memcpy(s, name, len);
     54 	sym->hash = symtab[h];
     55 	symtab[h] = sym;
     56 	sym->value = 0;
     57 	sym->size = 0;
     58 	sym->next = sym->prev = NULL;
     59 
     60 	return sym;
     61 }
     62 
     63 Section *
     64 section(char *name)
     65 {
     66 	unsigned h;
     67 	size_t len;
     68 	char *s;
     69 	Section *sec;
     70 
     71 	h = genhash(name) % NR_SECTIONS;
     72 	for (sec = sectab[h]; sec; sec = sec->hash) {
     73 		if (!strcmp(name, sec->name))
     74 			return sec;
     75 	}
     76 
     77 	len = strlen(name) + 1;
     78 	s = malloc(len);
     79 	sec = malloc(sizeof(*sec));
     80 	if (!s || !sec) {
     81 		error("out of memory");
     82 		exit(EXIT_FAILURE);
     83 	}
     84 
     85 	sec->name = memcpy(s, name, len);
     86 	sec->type = '?';
     87 	sec->base = 0;
     88 	sec->size = 0;
     89 	sec->flags = 0;
     90 	sec->hash = sectab[h];
     91 	sectab[h] = sec;
     92 
     93 	if (!sechead)
     94 		sechead = sec;
     95 	else
     96 		seclast->next = sec;
     97 	sec->next = NULL;
     98 
     99 	return seclast = sec;
    100 }
    101 
    102 #ifndef NDEBUG
    103 void
    104 debugsym(void)
    105 {
    106 	Symbol **symp, *sym;
    107 
    108 	fputs("Symbols:\n", stderr);
    109 	for (symp = symtab; symp < &symtab[NR_SYMBOL]; symp++) {
    110 		for (sym = *symp; sym; sym = sym->hash)
    111 			fprintf(stderr,
    112 			        "sym: %s (%#llx)\n",
    113 			        sym->name,
    114 			        sym->value);
    115 	}
    116 }
    117 
    118 void
    119 debugsec(void)
    120 {
    121 	Section **secp, *sec;
    122 
    123 	fputs("Sections:\n", stderr);
    124 	for (secp = sectab; secp < &sectab[NR_SECTIONS]; secp++) {
    125 		for (sec = *secp; sec; sec = sec->hash)
    126 			fprintf(stderr,
    127 			        "sec: %s - %c (%#llx,%#lx)\n",
    128 			        sec->name,
    129 			        sec->type,
    130 			        sec->base,
    131 			        sec->size);
    132 	}
    133 
    134 	for (sec = sechead; sec; sec = sec->next) {
    135 		fprintf(stderr,
    136 		        "%s %s",
    137 		        sec->name,
    138 			sec->next ? "->" : "\n");
    139 	}
    140 }
    141 #endif