| 1 | 48 | robfinch | // ============================================================================
 | 
      
         | 2 |  |  | //        __
 | 
      
         | 3 |  |  | //   \\__/ o\    (C) 2014-2017  Robert Finch, Waterloo
 | 
      
         | 4 |  |  | //    \  __ /    All rights reserved.
 | 
      
         | 5 |  |  | //     \/_//     robfinch<remove>@finitron.ca
 | 
      
         | 6 |  |  | //       ||
 | 
      
         | 7 |  |  | //
 | 
      
         | 8 |  |  | // A64 - Assembler
 | 
      
         | 9 |  |  | //  - 64 bit CPU
 | 
      
         | 10 |  |  | //
 | 
      
         | 11 |  |  | // This source file is free software: you can redistribute it and/or modify 
 | 
      
         | 12 |  |  | // it under the terms of the GNU Lesser General Public License as published 
 | 
      
         | 13 |  |  | // by the Free Software Foundation, either version 3 of the License, or     
 | 
      
         | 14 |  |  | // (at your option) any later version.                                      
 | 
      
         | 15 |  |  | //                                                                          
 | 
      
         | 16 |  |  | // This source file is distributed in the hope that it will be useful,      
 | 
      
         | 17 |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of           
 | 
      
         | 18 |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
 | 
      
         | 19 |  |  | // GNU General Public License for more details.                             
 | 
      
         | 20 |  |  | //                                                                          
 | 
      
         | 21 |  |  | // You should have received a copy of the GNU General Public License        
 | 
      
         | 22 |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.    
 | 
      
         | 23 |  |  | //                                                                          
 | 
      
         | 24 |  |  | // ============================================================================
 | 
      
         | 25 |  |  | //
 | 
      
         | 26 |  |  | #include "stdafx.h"
 | 
      
         | 27 |  |  |  
 | 
      
         | 28 |  |  | extern FILE *ofp;
 | 
      
         | 29 |  |  | extern int pass;
 | 
      
         | 30 |  |  | int numsym = 0;
 | 
      
         | 31 |  |  |  
 | 
      
         | 32 |  |  | void ShellSort(void *, int, int, int (*)());   // Does a shellsort - like bsort()
 | 
      
         | 33 |  |  | SHashVal HashFnc(void *def);
 | 
      
         | 34 |  |  | int icmp (const void *n1, const void *n2);
 | 
      
         | 35 |  |  | int ncmp (char *n1, const void *n2);
 | 
      
         | 36 |  |  | SHashTbl HashInfo = { HashFnc, icmp, ncmp, 0, sizeof(SYM), NULL };
 | 
      
         | 37 |  |  |  
 | 
      
         | 38 |  |  | SHashVal HashFnc(void *d)
 | 
      
         | 39 |  |  | {
 | 
      
         | 40 |  |  |    SYM *def = (SYM *)d;
 | 
      
         | 41 |  |  |    return htSymHash(&HashInfo, nmTable.GetName(def->name));
 | 
      
         | 42 |  |  | }
 | 
      
         | 43 |  |  |  
 | 
      
         | 44 |  |  | int icmp (const void *m1, const void *m2)
 | 
      
         | 45 |  |  | {
 | 
      
         | 46 |  |  |     SYM *n1; SYM *n2;
 | 
      
         | 47 |  |  |     n1 = (SYM *)m1;
 | 
      
         | 48 |  |  |     n2 = (SYM *)m2;
 | 
      
         | 49 |  |  |         if (n1->name==NULL) return 1;
 | 
      
         | 50 |  |  |         if (n2->name==NULL) return -1;
 | 
      
         | 51 |  |  |   return (strcmp(nmTable.GetName(n1->name), nmTable.GetName(n2->name)));
 | 
      
         | 52 |  |  | }
 | 
      
         | 53 |  |  |  
 | 
      
         | 54 |  |  | int ncmp (char *m1, const void *m2)
 | 
      
         | 55 |  |  | {
 | 
      
         | 56 |  |  |     SYM *n2;
 | 
      
         | 57 |  |  |     n2 = (SYM *)m2;
 | 
      
         | 58 |  |  |         if (m1==NULL) return 1;
 | 
      
         | 59 |  |  |         if (n2->name==NULL) return -1;
 | 
      
         | 60 |  |  |   return (strcmp(m1, nmTable.GetName(n2->name)));
 | 
      
         | 61 |  |  | }
 | 
      
         | 62 |  |  |  
 | 
      
         | 63 |  |  | void SymbolInit()
 | 
      
         | 64 |  |  | {
 | 
      
         | 65 |  |  |    HashInfo.size = 65536;
 | 
      
         | 66 |  |  |    HashInfo.width = sizeof(SYM);
 | 
      
         | 67 |  |  |    if ((HashInfo.table = calloc(HashInfo.size, sizeof(SYM))) == NULL) {
 | 
      
         | 68 |  |  |       exit (1);
 | 
      
         | 69 |  |  |    }
 | 
      
         | 70 |  |  | }
 | 
      
         | 71 |  |  |  
 | 
      
         | 72 |  |  | // ----------------------------------------------------------------------------
 | 
      
         | 73 |  |  | // ----------------------------------------------------------------------------
 | 
      
         | 74 |  |  |  
 | 
      
         | 75 |  |  | char *segname(int seg)
 | 
      
         | 76 |  |  | {
 | 
      
         | 77 |  |  |      switch(seg) {
 | 
      
         | 78 |  |  |      case codeseg: return "code";
 | 
      
         | 79 |  |  |      case dataseg: return "data";
 | 
      
         | 80 |  |  |      case bssseg: return "bss";
 | 
      
         | 81 |  |  |      case tlsseg: return "tls";
 | 
      
         | 82 |  |  |      case rodataseg: return "rodata";
 | 
      
         | 83 |  |  |      case constseg: return "const";
 | 
      
         | 84 |  |  |      default: return "???";
 | 
      
         | 85 |  |  |      }
 | 
      
         | 86 |  |  | }
 | 
      
         | 87 |  |  |  
 | 
      
         | 88 |  |  | // ----------------------------------------------------------------------------
 | 
      
         | 89 |  |  | // Do a binary search to find the symbol.
 | 
      
         | 90 |  |  | // ----------------------------------------------------------------------------
 | 
      
         | 91 |  |  |  
 | 
      
         | 92 |  |  | SYM *find_symbol(char *name)
 | 
      
         | 93 |  |  | {
 | 
      
         | 94 |  |  |     SYM *p;
 | 
      
         | 95 |  |  |  
 | 
      
         | 96 |  |  |     p = (SYM *)htFind2(&HashInfo, name);
 | 
      
         | 97 |  |  |     return p;
 | 
      
         | 98 |  |  | }
 | 
      
         | 99 |  |  |  
 | 
      
         | 100 |  |  |  
 | 
      
         | 101 |  |  | // ----------------------------------------------------------------------------
 | 
      
         | 102 |  |  | // ----------------------------------------------------------------------------
 | 
      
         | 103 |  |  |  
 | 
      
         | 104 |  |  | SYM *insert_symbol(SYM *sym)
 | 
      
         | 105 |  |  | {
 | 
      
         | 106 |  |  |     SYM *p;
 | 
      
         | 107 |  |  |  
 | 
      
         | 108 |  |  |     if (!(p=(SYM *)htInsert(&HashInfo, sym)))
 | 
      
         | 109 |  |  |        printf("failed to insert symbol\r\n");
 | 
      
         | 110 |  |  |     if (p)
 | 
      
         | 111 |  |  |        p->ord = p-(SYM *)(HashInfo.table);
 | 
      
         | 112 |  |  |     return p;
 | 
      
         | 113 |  |  | }
 | 
      
         | 114 |  |  |  
 | 
      
         | 115 |  |  |  
 | 
      
         | 116 |  |  | // ----------------------------------------------------------------------------
 | 
      
         | 117 |  |  | // When the symbol is first setup we force the value to be a large one in
 | 
      
         | 118 |  |  | // order to force the assembler to generate a maximum size prefix in case the
 | 
      
         | 119 |  |  | // symbol ends up being unresolved.
 | 
      
         | 120 |  |  | // ----------------------------------------------------------------------------
 | 
      
         | 121 |  |  |  
 | 
      
         | 122 |  |  | SYM *new_symbol(char *name)
 | 
      
         | 123 |  |  | {
 | 
      
         | 124 |  |  |     SYM *p;
 | 
      
         | 125 |  |  |     SYM ts;
 | 
      
         | 126 |  |  |  
 | 
      
         | 127 |  |  |     if (p = find_symbol(name)) {
 | 
      
         | 128 |  |  |         printf("Symbol already in table.\r\n");
 | 
      
         | 129 |  |  |         return p;
 | 
      
         | 130 |  |  |     }
 | 
      
         | 131 |  |  |     if (numsym > 65525) {
 | 
      
         | 132 |  |  |         printf("Too many symbols.\r\n");
 | 
      
         | 133 |  |  |         return (SYM *)NULL;
 | 
      
         | 134 |  |  |     }
 | 
      
         | 135 |  |  |     if (pass > 5) {
 | 
      
         | 136 |  |  |         //printf("%s: added\r\n", name);
 | 
      
         | 137 |  |  |     }
 | 
      
         | 138 |  |  |      ts.name = nmTable.AddName(name);
 | 
      
         | 139 |  |  | //     strncpy(syms[numsym].name, name, sizeof(syms[numsym].name)/sizeof(char)-1);
 | 
      
         | 140 |  |  | //     syms[numsym].name[199] = '\0';
 | 
      
         | 141 |  |  |      ts.value.low = 0x8000000000000000LL | numsym;
 | 
      
         | 142 |  |  |          ts.value.high = 0;
 | 
      
         | 143 |  |  |      ts.defined = 0;
 | 
      
         | 144 |  |  |      ts.segment = segment;
 | 
      
         | 145 |  |  |      ts.scope = ' ';
 | 
      
         | 146 |  |  |      ts.isExtern = 0;
 | 
      
         | 147 |  |  |      p = insert_symbol(&ts);
 | 
      
         | 148 |  |  |      numsym++;
 | 
      
         | 149 |  |  |      return p;
 | 
      
         | 150 |  |  | }
 | 
      
         | 151 |  |  |  
 | 
      
         | 152 |  |  |  
 | 
      
         | 153 |  |  | // ----------------------------------------------------------------------------
 | 
      
         | 154 |  |  | // ----------------------------------------------------------------------------
 | 
      
         | 155 |  |  |  
 | 
      
         | 156 |  |  | void DumpSymbols()
 | 
      
         | 157 |  |  | {
 | 
      
         | 158 |  |  |     int nn,ii,blnk,count;
 | 
      
         | 159 |  |  |     SYM *dp, *pt;
 | 
      
         | 160 |  |  |  
 | 
      
         | 161 |  |  |    pt = (SYM *)HashInfo.table;
 | 
      
         | 162 |  |  |  
 | 
      
         | 163 |  |  |    // Pack any 'holes' in the table
 | 
      
         | 164 |  |  |    for(blnk= ii = count = 0; count < HashInfo.size; count++, ii++) {
 | 
      
         | 165 |  |  |       dp = &pt[ii];
 | 
      
         | 166 |  |  |       if (dp->name) {
 | 
      
         | 167 |  |  |          if (blnk > 0)
 | 
      
         | 168 |  |  |             memmove(&pt[ii-blnk], &pt[ii], (HashInfo.size - count) * sizeof(SYM));
 | 
      
         | 169 |  |  |          ii -= blnk;
 | 
      
         | 170 |  |  |          blnk = 0;
 | 
      
         | 171 |  |  |       }
 | 
      
         | 172 |  |  |       else
 | 
      
         | 173 |  |  |          blnk++;
 | 
      
         | 174 |  |  |    }
 | 
      
         | 175 |  |  |  
 | 
      
         | 176 |  |  |    // Sort the table
 | 
      
         | 177 |  |  |    qsort(pt, ii, sizeof(SYM), icmp);
 | 
      
         | 178 |  |  |  
 | 
      
         | 179 |  |  |  
 | 
      
         | 180 |  |  |     fprintf(ofp, "%d symbols\n", numsym);
 | 
      
         | 181 |  |  |     fprintf(ofp, "  Symbol Name                              seg     address bits\n");
 | 
      
         | 182 |  |  |     for (nn = 0; nn < ii; nn++) {
 | 
      
         | 183 |  |  | //        qq = symorder[nn];
 | 
      
         | 184 |  |  |         dp = &pt[nn];
 | 
      
         | 185 |  |  |         if (dp->name)
 | 
      
         | 186 |  |  |         fprintf(ofp, "%c %-40s %6s  %06llx %d\n", dp->phaserr, nmTable.GetName(dp->name), segname(dp->segment), dp->value.low, dp->bits);
 | 
      
         | 187 |  |  |     }
 | 
      
         | 188 |  |  | }
 |