1#include <ctype.h>2#include <stdio.h>3#include <stdlib.h>45#include <scc/mach.h>67#include "ld.h"89/* TODO: This function must go in pass2 */10static void11rebase(Obj *obj)12{13 Symbol *aux;14 Objsym *sym;1516 for (sym = obj->syms; sym; sym = sym->next) {17 switch (sym->type) {18 case 'T':19 case 'D':20 case 'B':21 /*22 * this lookup must succeed, otherwise23 * we have an error in our code.24 */25 aux = lookup(sym->name);26 aux->value += obj->secs[sym->index].base;27 case 't':28 case 'd':29 case 'b':30 sym->value += obj->secs[sym->index].base;31 case 'U':32 case 'N':33 case '?':34 break;35 default:36 abort();37 }38 }39}4041/*42 * rebase all the sections43 */44void45pass3(int argc, char *argv[])46{47 int i;48 Obj *obj;49 Section *sec;50 Segment *seg;5152 /*53 * TODO: deal with page aligment54 */55 text.base = 0x100;56 rodata.base = text.base + text.size;57 data.base = rodata.base + rodata.size;58 bss.base = data.base + data.size;5960 for (obj = objhead; obj; obj = obj->next) {61 for (i = 0; getsec(obj, &i, &sec); i++) {62 /* TODO: deal with symbol aligment */63 switch (sec->type) {64 case 'T':65 seg = &text;66 break;67 /* TODO: what happens with rodata? */68 case 'D':69 seg = &data;70 break;71 case 'B':72 seg = &bss;73 break;74 default:75 abort();76 }7778 rebase(obj, i, seg->size);79 seg->size += sec.size;80 }81 }82}