scc

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

coff32getsec.c (1415B)


      1 #include <stdio.h>
      2 
      3 #include <scc/mach.h>
      4 
      5 #include "../libmach.h"
      6 #include "coff32.h"
      7 
      8 Section *
      9 coff32getsec(Obj *obj, int *idx, Section *sec)
     10 {
     11 	long n = *idx;
     12 	int type;
     13 	unsigned sflags;
     14 	unsigned long flags;
     15 	SCNHDR *scn;
     16 	Coff32 *coff = obj->data;
     17 	FILHDR *hdr = &coff->hdr;
     18 
     19 	if (n >= hdr->f_nscns)
     20 		return NULL;
     21 
     22 	scn = &coff->scns[n];
     23 	flags = scn->s_flags;
     24 
     25 	if (flags & STYP_TEXT) {
     26 		type = 'T';
     27 		sflags = SALLOC | SRELOC | SLOAD | SEXEC | SREAD;
     28 		if (flags & STYP_NOLOAD)
     29 			sflags |= SSHARED;
     30 	} else if (flags & STYP_DATA) {
     31 		type = 'D';
     32 		sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD;
     33 		if (flags & STYP_NOLOAD)
     34 			sflags |= SSHARED;
     35 	} else if (flags & STYP_BSS) {
     36 		type = 'B';
     37 		sflags = SALLOC | SREAD | SWRITE;
     38 	} else if (flags & STYP_INFO) {
     39 		type = 'N';
     40 		sflags = 0;
     41 	} else if (flags & STYP_LIB) {
     42 		type = 'T';
     43 		sflags = SRELOC;
     44 	} else if (flags & STYP_DSECT) {
     45 		type = 'D';
     46 		sflags = SRELOC;
     47 	} else if (flags & STYP_PAD) {
     48 		type = 'D';
     49 		sflags = SLOAD;
     50 	} else {
     51 		type = 'D';  /* We assume that STYP_REG is data */
     52 		sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD;
     53 	}
     54 
     55 	if (flags & STYP_NOLOAD)
     56 		sflags &= ~SLOAD;
     57 
     58 	sec->name = scn->s_name;
     59 	sec->index = n;
     60 	sec->size = scn->s_size;
     61 	sec->base = 0; /* TODO: Check what is the actual value */
     62 	sec->type = type;
     63 	sec->flags = sflags;
     64 	sec->align = 4; /* TODO: Check how align is defined in coff */
     65 
     66 	return sec;
     67 }