scc

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

coff32getsect.c (1489B)


      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 	scn = coff->scns;
     23 
     24 	secs = malloc(sizeof(Objsect) * hdr->f_nscns);
     25 	if (!secs)
     26 		return -1;
     27 
     28 	for (i = 0; i < hdr->f_nscns; i++) {
     29 		sp = &secs[i];
     30 		sp->next = (i < hdr->f_nscns-1) ? &secs[i+1] : NULL;
     31 
     32 		if (flags & STYP_TEXT) {
     33 			type = 'T';
     34 			sflags = SALLOC | SRELOC | SLOAD | SEXEC | SREAD;
     35 			if (flags & STYP_NOLOAD)
     36 				sflags |= SSHARED;
     37 		} else if (flags & STYP_DATA) {
     38 			type = 'D';
     39 			sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD;
     40 			if (flags & STYP_NOLOAD)
     41 				sflags |= SSHARED;
     42 		} else if (flags & STYP_BSS) {
     43 			type = 'B';
     44 			sflags = SALLOC | SREAD | SWRITE;
     45 		} else if (flags & STYP_INFO) {
     46 			type = 'N';
     47 			sflags = 0;
     48 		} else if (flags & STYP_LIB) {
     49 			type = 'T';
     50 			sflags = SRELOC;
     51 		} else if (flags & STYP_DSECT) {
     52 			type = 'D';
     53 			sflags = SRELOC;
     54 		} else if (flags & STYP_PAD) {
     55 			type = 'D';
     56 			sflags = SLOAD;
     57 		} else {
     58 			type = 'D';  /* We assume that STYP_REG is data */
     59 			sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD;
     60 		}
     61 
     62 		if (flags & STYP_NOLOAD)
     63 			sflags &= ~SLOAD;
     64 
     65 		sp->name = scn->s_name;
     66 		sp->offset = scn->s_scnptr;
     67 		sp->size = scn->s_size;
     68 		sp->type = type;
     69 	}
     70 	obj->secs = secs;
     71 	obj->nsecs = i;
     72 
     73 	return 1;
     74 }