scc

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

coff32getsect.c (1616B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #include <scc/mach.h>
      5 
      6 #include "../libmach.h"
      7 #include "coff32.h"
      8 
      9 int
     10 coff32getsect(Obj *obj)
     11 {
     12 	int i;
     13 	unsigned sflags, type;
     14 	unsigned long flags;
     15 	FILHDR *hdr;
     16 	struct coff32 *coff;
     17 	SCNHDR *scn;
     18 	Objsect *secs, *sp;
     19 
     20 	coff  = obj->data;
     21 	hdr = &coff->hdr;
     22 
     23 	secs = malloc(sizeof(Objsect) * hdr->f_nscns);
     24 	if (!secs)
     25 		return -1;
     26 
     27 	for (i = 0; i < hdr->f_nscns; i++) {
     28 		sp = &secs[i];
     29 		sp->next = (i < hdr->f_nscns-1) ? &secs[i+1] : NULL;
     30 		scn = &coff->scns[i];
     31 		flags = scn->s_flags;
     32 
     33 		if (flags & STYP_TEXT) {
     34 			type = 'T';
     35 			sflags = SALLOC | SRELOC | SLOAD | SEXEC | SREAD;
     36 			if (flags & STYP_NOLOAD)
     37 				sflags |= SSHARED;
     38 		} else if (flags & STYP_DATA) {
     39 			type = 'D';
     40 			sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD;
     41 			if (flags & STYP_NOLOAD)
     42 				sflags |= SSHARED;
     43 		} else if (flags & STYP_BSS) {
     44 			type = 'B';
     45 			sflags = SALLOC | SREAD | SWRITE;
     46 		} else if (flags & STYP_INFO) {
     47 			type = 'N';
     48 			sflags = 0;
     49 		} else if (flags & STYP_LIB) {
     50 			type = 'T';
     51 			sflags = SRELOC;
     52 		} else if (flags & STYP_DSECT) {
     53 			type = 'D';
     54 			sflags = SRELOC;
     55 		} else if (flags & STYP_PAD) {
     56 			type = 'D';
     57 			sflags = SLOAD;
     58 		} else {
     59 			type = 'D';  /* We assume that STYP_REG is data */
     60 			sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD;
     61 		}
     62 
     63 		if (flags & STYP_NOLOAD)
     64 			sflags &= ~SLOAD;
     65 
     66 		sp->name = scn->s_name;
     67 		sp->id = i;
     68 		sp->seek = scn->s_scnptr;
     69 		sp->size = scn->s_size;
     70 		sp->type = type;
     71 		sp->flags = sflags;
     72 		sp->align = 4; /* TODO: Check how align is defined in coff */
     73 	}
     74 	obj->secs = secs;
     75 	obj->nsecs = i;
     76 
     77 	return 1;
     78 }