scc

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

pass3.c (1152B)


      1 #include <ctype.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 
      5 #include <scc/mach.h>
      6 
      7 #include "ld.h"
      8 
      9 static void
     10 rebase(Obj *obj)
     11 {
     12 	Symbol *aux;
     13 	Objsym *sym;
     14 
     15 	for (sym = obj->syms; sym; sym = sym->next) {
     16 		switch (toupper(sym->type)) {
     17 		case 'T':
     18 		case 'D':
     19 		case 'B':
     20 			aux = lookup(sym->name, NOINSTALL);
     21 			aux->value += obj->secs[sym->sect].base;
     22 		case 't':
     23 		case 'd':
     24 		case 'b':
     25 			sym->value += obj->secs[sym->sect].base;
     26 		case 'N':
     27 		case 'U':
     28 		case '?':
     29 			break;
     30 		default:
     31 			abort();
     32 		}
     33 	}
     34 }
     35 
     36 /*
     37  * rebase all the sections
     38  */
     39 void
     40 pass3(int argc, char *argv[])
     41 {
     42 	Obj *obj;
     43 	Objlst *lst;
     44 	Objsect *sp;
     45 	unsigned long long *base, text, data, bss;
     46 
     47 	/*
     48 	 * TODO: deal with page aligment
     49 	 */
     50 	textbase = text = 0x100;
     51 	database = data = textsiz;
     52 	bssbase = bss = data+datasiz;
     53 
     54 	for (lst = objhead; lst; lst = lst->next) {
     55 		obj = lst->obj;
     56 		for (sp = obj->secs; sp; sp = sp->next) {
     57 			switch (sp->type) {
     58 			case 'T':
     59 				base = &text;
     60 				break;
     61 			case 'D':
     62 				base = &data;
     63 				break;
     64 			case 'B':
     65 				base = &bss;
     66 				break;
     67 			default:
     68 				abort();
     69 			}
     70 			sp->base = *base;
     71 			*base += sp->size;
     72 		}
     73 		rebase(obj);
     74 	}
     75 }