Home
       coff32getsym.c - scc - simple c99 compiler
  HTML git clone git://git.simple-cc.org/scc
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
   DIR README
   DIR LICENSE
       ---
       coff32getsym.c (1337B)
       ---
            1 #include <ctype.h>
            2 #include <stdio.h>
            3 #include <string.h>
            4 
            5 #include <scc/mach.h>
            6 
            7 #include "../libmach.h"
            8 #include "coff32.h"
            9 
           10 static int
           11 typeof(Coff32 *coff, SYMENT *ent)
           12 {
           13         int c;
           14         SCNHDR *scn;
           15 
           16         switch (ent->n_scnum) {
           17         case N_DEBUG:
           18                 c = 'N';
           19                 break;
           20         case N_ABS:
           21                 c = 'a';
           22                 break;
           23         case N_UNDEF:
           24                 c = (ent->n_value != 0) ? 'C' : 'U';
           25                 break;
           26         default:
           27                 scn = &coff->scns[ent->n_scnum-1];
           28 
           29                 switch (scn->s_flags) {
           30                 case STYP_TEXT:
           31                         c = 't';
           32                         break;
           33                 case STYP_DATA:
           34                         c = 'd';
           35                         break;
           36                 case STYP_BSS:
           37                         c = 'b';
           38                         break;
           39                 case STYP_INFO:
           40                         c = 'N';
           41                         break;
           42                 case STYP_LIT:
           43                         c = 'r';
           44                         break;
           45                 default:
           46                         c = '?';
           47                         break;
           48                 }
           49         }
           50 
           51         if (ent->n_sclass == C_EXT)
           52                 c = toupper(c);
           53 
           54         return c;
           55 }
           56 
           57 static char *
           58 symname(Coff32 *coff, SYMENT *ent)
           59 {
           60         if (ent->n_zeroes != 0)
           61                 return ent->n_name;
           62 
           63         return &coff->strtbl[ent->n_offset];
           64 }
           65 
           66 Symbol *
           67 coff32getsym(Obj *obj, int *idx, Symbol *sym)
           68 {
           69         int n = *idx;
           70         SYMENT *ent;
           71         Coff32 *coff = obj->data;
           72         FILHDR *hdr = &coff->hdr;
           73 
           74         if ((hdr->f_flags & F_LSYMS) != 0 || n >= coff->hdr.f_nsyms)
           75                 return NULL;
           76 
           77         ent = &coff->ents[n];
           78         sym->name = symname(coff, ent);
           79         sym->type = typeof(coff, ent);
           80         sym->stype = SYMOBJECT;
           81         sym->value = ent->n_value;
           82         sym->size = (sym->type == 'C') ? ent->n_value : 0;
           83         sym->index = n;
           84         *idx += ent->n_numaux;
           85 
           86         return sym;
           87 }