scc

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

coff32setsec.c (1499B)


      1 #include <limits.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 #include <scc/mach.h>
      7 
      8 #include "../libmach.h"
      9 #include "coff32.h"
     10 
     11 Section *
     12 coff32setsec(Obj *obj, int *idx, Section *sec)
     13 {
     14 	long flags, n = *idx;
     15 	SCNHDR *scn;
     16 	Coff32 *coff = obj->data;
     17 	FILHDR *hdr = &coff->hdr;
     18 
     19 	/* TODO: what happens with SABS? */
     20 	switch (sec->type) {
     21 	case 'D':
     22 		switch (sec->flags) {
     23 		case SALLOC | SRELOC | SLOAD | SWRITE | SREAD:
     24 			if (strcmp(sec->name ,".data") == 0)
     25 				flags = STYP_DATA;
     26 			else
     27 				flags = STYP_REG;
     28 			break;
     29 		case SALLOC | SREAD | SWRITE:
     30 			flags = STYP_NOLOAD;
     31 			break;
     32 		case SALLOC | SRELOC | SLOAD | SREAD:
     33 			flags = STYP_RDATA;
     34 			break;
     35 		case SEXEC | SALLOC | SRELOC | SLOAD | SREAD:
     36 			flags = STYP_TEXT | STYP_DATA;
     37 			break;
     38 		default:
     39 			goto invalid;
     40 		}
     41 		break;
     42 	case 'T':
     43 		flags = STYP_TEXT;
     44 		break;
     45 	case 'B':
     46 		flags = STYP_BSS;
     47 		break;
     48 	case 'N':
     49 	case '?':
     50 	default:
     51 	invalid:
     52 		/* TODO */
     53 		return NULL;
     54 	}
     55 
     56 	if (strlen(sec->name) >= SCNNMLEN)
     57 		return NULL;
     58 
     59 	if (n >= hdr->f_nscns) {
     60 		if (n > SHRT_MAX - 1)
     61 			return NULL;
     62 		scn = realloc(coff->scns, (n+1) * sizeof(SCNHDR));
     63 		if (!scn)
     64 			return NULL;
     65 		coff->scns = scn;
     66 		hdr->f_nscns = n + 1;
     67 	}
     68 	scn = &coff->scns[n];
     69 
     70 	strncpy(scn->s_name, sec->name, SCNNMLEN);
     71 	scn->s_paddr = 0;
     72 	scn->s_vaddr = sec->base;
     73 	scn->s_size = sec->size;
     74 	scn->s_scnptr = 0;
     75 	scn->s_relptr = 0;
     76 	scn->s_lnnoptr = 0;
     77 	scn->s_nrelloc = 0;
     78 	scn->s_nlnno = 0;
     79 	scn->s_flags = flags; 
     80 
     81 	return sec;
     82 }