home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / elf.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  74.6 KB  |  2,998 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software Foundation,
  17. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #include "GBA.h"
  24. #include "Port.h"
  25. #include "elf.h"
  26. #include "NLS.h"
  27.  
  28. #define elfReadMemory(addr) \
  29.   READ32LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask]))
  30.  
  31. #define DW_TAG_array_type             0x01
  32. #define DW_TAG_enumeration_type       0x04
  33. #define DW_TAG_formal_parameter       0x05
  34. #define DW_TAG_label                  0x0a
  35. #define DW_TAG_lexical_block          0x0b
  36. #define DW_TAG_member                 0x0d
  37. #define DW_TAG_pointer_type           0x0f
  38. #define DW_TAG_reference_type         0x10
  39. #define DW_TAG_compile_unit           0x11
  40. #define DW_TAG_structure_type         0x13
  41. #define DW_TAG_subroutine_type        0x15
  42. #define DW_TAG_typedef                0x16
  43. #define DW_TAG_union_type             0x17
  44. #define DW_TAG_unspecified_parameters 0x18
  45. #define DW_TAG_inheritance            0x1c
  46. #define DW_TAG_inlined_subroutine     0x1d
  47. #define DW_TAG_subrange_type          0x21
  48. #define DW_TAG_base_type              0x24
  49. #define DW_TAG_const_type             0x26
  50. #define DW_TAG_enumerator             0x28
  51. #define DW_TAG_subprogram             0x2e
  52. #define DW_TAG_variable               0x34
  53. #define DW_TAG_volatile_type          0x35
  54.  
  55. #define DW_AT_sibling              0x01
  56. #define DW_AT_location             0x02
  57. #define DW_AT_name                 0x03 
  58. #define DW_AT_byte_size            0x0b
  59. #define DW_AT_bit_offset           0x0c
  60. #define DW_AT_bit_size             0x0d
  61. #define DW_AT_stmt_list            0x10
  62. #define DW_AT_low_pc               0x11
  63. #define DW_AT_high_pc              0x12
  64. #define DW_AT_language             0x13
  65. #define DW_AT_compdir              0x1b
  66. #define DW_AT_const_value          0x1c
  67. #define DW_AT_containing_type      0x1d
  68. #define DW_AT_inline               0x20
  69. #define DW_AT_producer             0x25
  70. #define DW_AT_prototyped           0x27
  71. #define DW_AT_upper_bound          0x2f
  72. #define DW_AT_abstract_origin      0x31
  73. #define DW_AT_accessibility        0x32
  74. #define DW_AT_artificial           0x34
  75. #define DW_AT_data_member_location 0x38
  76. #define DW_AT_decl_file            0x3a
  77. #define DW_AT_decl_line            0x3b
  78. #define DW_AT_declaration          0x3c
  79. #define DW_AT_encoding             0x3e
  80. #define DW_AT_external             0x3f
  81. #define DW_AT_frame_base           0x40
  82. #define DW_AT_macro_info           0x43
  83. #define DW_AT_specification        0x47
  84. #define DW_AT_type                 0x49
  85. #define DW_AT_virtuality           0x4c
  86. #define DW_AT_vtable_elem_location 0x4d
  87. // DWARF 2.1/3.0 extensions
  88. #define DW_AT_entry_pc             0x52
  89. #define DW_AT_ranges               0x55
  90. // ARM Compiler extensions
  91. #define DW_AT_proc_body            0x2000
  92. #define DW_AT_save_offset          0x2001
  93. #define DW_AT_user_2002            0x2002
  94. // MIPS extensions
  95. #define DW_AT_MIPS_linkage_name    0x2007
  96.  
  97. #define DW_FORM_addr      0x01
  98. #define DW_FORM_data2     0x05
  99. #define DW_FORM_data4     0x06
  100. #define DW_FORM_string    0x08
  101. #define DW_FORM_block     0x09
  102. #define DW_FORM_block1    0x0a
  103. #define DW_FORM_data1     0x0b
  104. #define DW_FORM_flag      0x0c
  105. #define DW_FORM_sdata     0x0d
  106. #define DW_FORM_strp      0x0e
  107. #define DW_FORM_udata     0x0f
  108. #define DW_FORM_ref_addr  0x10
  109. #define DW_FORM_ref4      0x13
  110. #define DW_FORM_ref_udata 0x15
  111. #define DW_FORM_indirect  0x16
  112.  
  113. #define DW_OP_addr        0x03
  114. #define DW_OP_plus_uconst 0x23
  115. #define DW_OP_reg0        0x50
  116. #define DW_OP_reg1        0x51
  117. #define DW_OP_reg2        0x52
  118. #define DW_OP_reg3        0x53
  119. #define DW_OP_reg4        0x54
  120. #define DW_OP_reg5        0x55
  121. #define DW_OP_reg6        0x56
  122. #define DW_OP_reg7        0x57
  123. #define DW_OP_reg8        0x58
  124. #define DW_OP_reg9        0x59
  125. #define DW_OP_reg10       0x5a
  126. #define DW_OP_reg11       0x5b
  127. #define DW_OP_reg12       0x5c
  128. #define DW_OP_reg13       0x5d
  129. #define DW_OP_reg14       0x5e
  130. #define DW_OP_reg15       0x5f
  131. #define DW_OP_fbreg       0x91
  132.  
  133. #define DW_LNS_extended_op      0x00
  134. #define DW_LNS_copy             0x01
  135. #define DW_LNS_advance_pc       0x02
  136. #define DW_LNS_advance_line     0x03
  137. #define DW_LNS_set_file         0x04
  138. #define DW_LNS_set_column       0x05
  139. #define DW_LNS_negate_stmt      0x06
  140. #define DW_LNS_set_basic_block  0x07
  141. #define DW_LNS_const_add_pc     0x08
  142. #define DW_LNS_fixed_advance_pc 0x09
  143.  
  144. #define DW_LNE_end_sequence 0x01
  145. #define DW_LNE_set_address  0x02
  146. #define DW_LNE_define_file  0x03
  147.  
  148. #define DW_CFA_advance_loc      0x01
  149. #define DW_CFA_offset           0x02
  150. #define DW_CFA_restore          0x03
  151. #define DW_CFA_set_loc          0x01
  152. #define DW_CFA_advance_loc1     0x02
  153. #define DW_CFA_advance_loc2     0x03
  154. #define DW_CFA_advance_loc4     0x04
  155. #define DW_CFA_offset_extended  0x05
  156. #define DW_CFA_restore_extended 0x06
  157. #define DW_CFA_undefined        0x07
  158. #define DW_CFA_same_value       0x08
  159. #define DW_CFA_register         0x09
  160. #define DW_CFA_remember_state   0x0a
  161. #define DW_CFA_restore_state    0x0b
  162. #define DW_CFA_def_cfa          0x0c
  163. #define DW_CFA_def_cfa_register 0x0d
  164. #define DW_CFA_def_cfa_offset   0x0e
  165. #define DW_CFA_nop              0x00
  166.  
  167. #define CASE_TYPE_TAG \
  168.     case DW_TAG_const_type:\
  169.     case DW_TAG_volatile_type:\
  170.     case DW_TAG_pointer_type:\
  171.     case DW_TAG_base_type:\
  172.     case DW_TAG_array_type:\
  173.     case DW_TAG_structure_type:\
  174.     case DW_TAG_union_type:\
  175.     case DW_TAG_typedef:\
  176.     case DW_TAG_subroutine_type:\
  177.     case DW_TAG_enumeration_type:\
  178.     case DW_TAG_enumerator:\
  179.     case DW_TAG_reference_type
  180.  
  181. struct ELFcie {
  182.   ELFcie *next;
  183.   u32 offset;
  184.   u8 *augmentation;
  185.   u32 codeAlign;
  186.   s32 dataAlign;
  187.   int returnAddress;
  188.   u8 *data;
  189.   u32 dataLen;
  190. };
  191.  
  192. struct ELFfde {
  193.   ELFcie *cie;
  194.   u32 address;
  195.   u32 end;
  196.   u8 *data;
  197.   u32 dataLen;
  198. };
  199.  
  200. enum ELFRegMode {
  201.   REG_NOT_SET,
  202.   REG_OFFSET,
  203.   REG_REGISTER
  204. };
  205.  
  206.  
  207. struct ELFFrameStateRegister {
  208.   ELFRegMode mode;
  209.   int reg;
  210.   s32 offset;
  211. };
  212.  
  213. struct ELFFrameStateRegisters {
  214.   ELFFrameStateRegister regs[16];
  215.   ELFFrameStateRegisters *previous;
  216. };
  217.  
  218. enum ELFCfaMode {
  219.   CFA_NOT_SET,
  220.   CFA_REG_OFFSET
  221. };
  222.  
  223. struct ELFFrameState {
  224.   ELFFrameStateRegisters registers;
  225.  
  226.   ELFCfaMode cfaMode;
  227.   int cfaRegister;
  228.   s32 cfaOffset;
  229.  
  230.   u32 pc;
  231.  
  232.   int dataAlign;
  233.   int codeAlign;
  234.   int returnAddress;
  235. };
  236.  
  237. extern bool cpuIsMultiBoot;
  238.  
  239. Symbol *elfSymbols = NULL;
  240. char *elfSymbolsStrTab = NULL;
  241. int elfSymbolsCount = 0;
  242.  
  243. ELFSectionHeader **elfSectionHeaders = NULL;
  244. char *elfSectionHeadersStringTable = NULL;
  245. int elfSectionHeadersCount = 0;
  246. u8 *elfFileData = NULL;
  247.  
  248. CompileUnit *elfCompileUnits = NULL;
  249. DebugInfo *elfDebugInfo = NULL;
  250. char *elfDebugStrings = NULL;
  251.  
  252. ELFcie *elfCies = NULL;
  253. ELFfde **elfFdes = NULL;
  254. int elfFdeCount = 0;
  255.  
  256. CompileUnit *elfCurrentUnit = NULL;
  257.  
  258. u32 elfRead4Bytes(u8 *);
  259. u16 elfRead2Bytes(u8 *);
  260.  
  261. CompileUnit *elfGetCompileUnit(u32 addr)
  262. {
  263.   if(elfCompileUnits) {
  264.     CompileUnit *unit = elfCompileUnits;
  265.     while(unit) {
  266.       if(unit->lowPC) {
  267.         if(addr >= unit->lowPC && addr < unit->highPC)
  268.           return unit;
  269.       } else {
  270.         ARanges *r = unit->ranges;
  271.         if(r) {
  272.           int count = r->count;
  273.           for(int j = 0; j < count; j++) {
  274.             if(addr >= r->ranges[j].lowPC && addr < r->ranges[j].highPC)
  275.               return unit;
  276.           }
  277.         }
  278.       }
  279.       unit = unit->next;
  280.     }
  281.   }
  282.   return NULL;
  283. }
  284.  
  285. char *elfGetAddressSymbol(u32 addr)
  286. {
  287.   static char buffer[256];
  288.  
  289.   CompileUnit *unit = elfGetCompileUnit(addr);
  290.   // found unit, need to find function
  291.   if(unit) {
  292.     Function *func = unit->functions;
  293.     while(func) {
  294.       if(addr >= func->lowPC && addr < func->highPC) {
  295.         int offset = addr - func->lowPC;
  296.         char *name = func->name;
  297.         if(!name)
  298.           name = "";
  299.         if(offset)
  300.           sprintf(buffer, "%s+%d", name, offset);
  301.         else
  302.           strcpy(buffer, name);
  303.         return buffer;
  304.       }
  305.       func = func->next;
  306.     }
  307.   }
  308.  
  309.   if(elfSymbolsCount) {
  310.     for(int i = 0; i < elfSymbolsCount; i++) {
  311.       Symbol *s = &elfSymbols[i];
  312.       if((addr >= s->value)  && addr < (s->value+s->size)) {
  313.         int offset = addr-s->value;
  314.         char *name = s->name;
  315.         if(name == NULL)
  316.           name = "";
  317.         if(offset)
  318.           sprintf(buffer, "%s+%d", name, addr-s->value);
  319.         else
  320.           strcpy(buffer, name);
  321.         return buffer;
  322.       } else if(addr == s->value) {
  323.         if(s->name)
  324.           strcpy(buffer, s->name);
  325.         else
  326.           strcpy(buffer, "");
  327.         return buffer;
  328.       }
  329.     }
  330.   }
  331.   
  332.   return "";
  333. }
  334.  
  335. bool elfFindLineInModule(u32 *addr, char *name, int line)
  336. {
  337.   CompileUnit *unit = elfCompileUnits;
  338.  
  339.   while(unit) {
  340.     if(unit->lineInfoTable) {
  341.       int i;
  342.       int count = unit->lineInfoTable->fileCount;
  343.       char *found = NULL;
  344.       for(i = 0; i < count; i++) {
  345.         if(strcmp(name, unit->lineInfoTable->files[i]) == 0) {
  346.           found = unit->lineInfoTable->files[i];
  347.           break;
  348.         }
  349.       }
  350.       // found a matching filename... try to find line now
  351.       if(found) {
  352.         LineInfoItem *table = unit->lineInfoTable->lines;
  353.         count = unit->lineInfoTable->number;
  354.         for(i = 0; i < count; i++) {
  355.           if(table[i].file == found && table[i].line == line) {
  356.             *addr = table[i].address;
  357.             return true;
  358.           }
  359.         }
  360.         // we can only find a single match
  361.         return false;
  362.       }
  363.     }
  364.     unit = unit->next;
  365.   }
  366.   return false;
  367. }
  368.  
  369. int elfFindLine(CompileUnit *unit, Function * /* func */, u32 addr, char **f)
  370. {
  371.   int currentLine = -1;
  372.   if(unit->hasLineInfo) {
  373.     int count = unit->lineInfoTable->number;
  374.     LineInfoItem *table = unit->lineInfoTable->lines;
  375.     int i;
  376.     for(i = 0; i < count; i++) {
  377.       if(addr <= table[i].address)
  378.         break;
  379.     }
  380.     if(i == count)
  381.       i--;
  382.     *f = table[i].file;
  383.     currentLine = table[i].line;
  384.   }
  385.   return currentLine;
  386. }
  387.  
  388. bool elfFindLineInUnit(u32 *addr, CompileUnit *unit, int line)
  389. {
  390.   if(unit->hasLineInfo) {
  391.     int count = unit->lineInfoTable->number;
  392.     LineInfoItem *table = unit->lineInfoTable->lines;
  393.     int i;
  394.     for(i = 0; i < count; i++) {
  395.       if(line == table[i].line) {
  396.         *addr = table[i].address;
  397.         return true;
  398.       }
  399.     }
  400.   }
  401.   return false;
  402. }
  403.  
  404. bool elfGetCurrentFunction(u32 addr, Function **f, CompileUnit **u)
  405. {
  406.   CompileUnit *unit = elfGetCompileUnit(addr);
  407.   // found unit, need to find function
  408.   if(unit) {
  409.     Function *func = unit->functions;
  410.     while(func) {
  411.       if(addr >= func->lowPC && addr < func->highPC) {
  412.         *f = func;
  413.         *u = unit;
  414.         return true;
  415.       }
  416.       func = func->next;
  417.     }
  418.   }
  419.   return false;
  420. }
  421.  
  422. bool elfGetObject(char *name, Function *f, CompileUnit *u, Object **o)
  423. {
  424.   if(f && u) {
  425.     Object *v = f->variables;
  426.  
  427.     while(v) {
  428.       if(strcmp(name, v->name) == 0) {
  429.         *o = v;
  430.         return true;
  431.       }
  432.       v = v->next;
  433.     }
  434.     v = f->parameters;
  435.     while(v) {
  436.       if(strcmp(name, v->name) == 0) {
  437.         *o = v;
  438.         return true;
  439.       }
  440.       v = v->next;
  441.     }
  442.     v = u->variables;
  443.     while(v) {
  444.       if(strcmp(name, v->name) == 0) {
  445.         *o = v;
  446.         return true;
  447.       }
  448.       v = v->next;
  449.     }
  450.   }
  451.   
  452.   CompileUnit *c = elfCompileUnits;
  453.   
  454.   while(c) {
  455.     if(c != u) {
  456.       Object *v = c->variables;
  457.       while(v) {
  458.         if(strcmp(name, v->name) == 0) {
  459.           *o = v;
  460.           return true;
  461.         }
  462.         v = v->next;
  463.       }
  464.     }
  465.     c = c->next;
  466.   }
  467.   
  468.   return false;
  469. }
  470.  
  471. char *elfGetSymbol(int i, u32 *value, u32 *size, int *type)
  472. {
  473.   if(i < elfSymbolsCount) {
  474.     Symbol *s = &elfSymbols[i];
  475.     *value = s->value;
  476.     *size = s->size;
  477.     *type = s->type;
  478.     return s->name;
  479.   }
  480.   return NULL;
  481. }
  482.  
  483. bool elfGetSymbolAddress(char *sym, u32 *addr, u32 *size, int *type)
  484. {
  485.   if(elfSymbolsCount) {
  486.     for(int i = 0; i < elfSymbolsCount; i++) {
  487.       Symbol *s = &elfSymbols[i];
  488.       if(strcmp(sym, s->name) == 0) {
  489.         *addr = s->value;
  490.         *size = s->size;
  491.         *type = s->type;
  492.         return true;
  493.       }
  494.     }
  495.   }
  496.   return false;
  497. }
  498.  
  499. ELFfde *elfGetFde(u32 address)
  500. {
  501.   if(elfFdes) {
  502.     int i;
  503.     for(i = 0; i < elfFdeCount; i++) {
  504.       if(address >= elfFdes[i]->address &&
  505.          address < elfFdes[i]->end) {
  506.         return elfFdes[i];
  507.       }
  508.     }
  509.   }
  510.  
  511.   return NULL;
  512. }
  513.  
  514. void elfExecuteCFAInstructions(ELFFrameState *state, u8 *data, u32 len,
  515.                                u32 pc)
  516. {
  517.   u8 *end = data + len;
  518.   int bytes;
  519.   int reg;
  520.   ELFFrameStateRegisters *fs;
  521.  
  522.   while(data < end && state->pc < pc) {
  523.     u8 op = *data++;
  524.  
  525.     switch(op >> 6) {
  526.     case DW_CFA_advance_loc:
  527.       state->pc += (op & 0x3f) * state->codeAlign;
  528.       break;
  529.     case DW_CFA_offset:
  530.       reg = op & 0x3f;
  531.       state->registers.regs[reg].mode = REG_OFFSET;
  532.       state->registers.regs[reg].offset = state->dataAlign *
  533.         (s32)elfReadLEB128(data, &bytes);
  534.       data += bytes;
  535.       break;
  536.     case DW_CFA_restore:
  537.       // we don't care much about the other possible settings,
  538.       // so just setting to unset is enough for now
  539.       state->registers.regs[op & 0x3f].mode = REG_NOT_SET;
  540.       break;
  541.     case 0:
  542.       switch(op & 0x3f) {
  543.       case DW_CFA_nop:
  544.           break;
  545.       case DW_CFA_advance_loc1:
  546.         state->pc += state->codeAlign * (*data++);
  547.         break;
  548.       case DW_CFA_advance_loc2:
  549.         state->pc += state->codeAlign * elfRead2Bytes(data);
  550.         data += 2;
  551.         break;
  552.       case DW_CFA_advance_loc4:
  553.         state->pc += state->codeAlign * elfRead4Bytes(data);
  554.         data += 4;
  555.         break;
  556.       case DW_CFA_offset_extended:
  557.         reg = elfReadLEB128(data, &bytes);
  558.         data += bytes;
  559.         state->registers.regs[reg].mode = REG_OFFSET;
  560.         state->registers.regs[reg].offset = state->dataAlign *
  561.           (s32)elfReadLEB128(data, &bytes);
  562.         data += bytes;
  563.         break;
  564.       case DW_CFA_restore_extended:
  565.       case DW_CFA_undefined:
  566.       case DW_CFA_same_value:
  567.         reg = elfReadLEB128(data, &bytes);
  568.         data += bytes;
  569.         state->registers.regs[reg].mode = REG_NOT_SET;
  570.         break;
  571.       case DW_CFA_register:
  572.         reg = elfReadLEB128(data, &bytes);
  573.         data += bytes;
  574.         state->registers.regs[reg].mode = REG_REGISTER;
  575.         state->registers.regs[reg].reg = elfReadLEB128(data, &bytes);
  576.         data += bytes;
  577.         break;
  578.       case DW_CFA_remember_state:
  579.         fs = (ELFFrameStateRegisters *)calloc(1,
  580.                                               sizeof(ELFFrameStateRegisters));
  581.         memcpy(fs, &state->registers, sizeof(ELFFrameStateRegisters));
  582.         state->registers.previous = fs;
  583.         break;
  584.       case DW_CFA_restore_state:
  585.         if(state->registers.previous == NULL) {
  586.           printf("Error: previous frame state is NULL.\n");
  587.           return;
  588.         }
  589.         fs = state->registers.previous;
  590.         memcpy(&state->registers, fs, sizeof(ELFFrameStateRegisters));
  591.         free(fs);
  592.         break;
  593.       case DW_CFA_def_cfa:
  594.         state->cfaRegister = elfReadLEB128(data, &bytes);
  595.         data += bytes;
  596.         state->cfaOffset = (s32)elfReadLEB128(data, &bytes);
  597.         data += bytes;
  598.         state->cfaMode = CFA_REG_OFFSET;
  599.         break;
  600.       case DW_CFA_def_cfa_register:
  601.         state->cfaRegister = elfReadLEB128(data, &bytes);
  602.         data += bytes;
  603.         state->cfaMode = CFA_REG_OFFSET;
  604.         break;
  605.       case DW_CFA_def_cfa_offset:
  606.         state->cfaOffset = (s32)elfReadLEB128(data, &bytes);
  607.         data += bytes;
  608.         state->cfaMode = CFA_REG_OFFSET;
  609.         break;
  610.       default:
  611.         printf("Unknown CFA opcode %08x\n", op);
  612.         return;
  613.       }
  614.       break;
  615.     default:
  616.       printf("Unknown CFA opcode %08x\n", op);
  617.       return;
  618.     }
  619.   }
  620. }
  621.  
  622. ELFFrameState *elfGetFrameState(ELFfde *fde, u32 address)
  623. {
  624.   ELFFrameState *state = (ELFFrameState *)calloc(1, sizeof(ELFFrameState));
  625.   state->pc = fde->address;
  626.   state->dataAlign = fde->cie->dataAlign;
  627.   state->codeAlign = fde->cie->codeAlign;
  628.   state->returnAddress = fde->cie->returnAddress;
  629.   
  630.   elfExecuteCFAInstructions(state,
  631.                             fde->cie->data,
  632.                             fde->cie->dataLen,
  633.                             0xffffffff);
  634.   elfExecuteCFAInstructions(state,
  635.                             fde->data,
  636.                             fde->dataLen,
  637.                             address);
  638.  
  639.   return state;
  640. }
  641.  
  642. void elfPrintCallChain(u32 address)
  643. {
  644.   int count = 1;
  645.  
  646.   reg_pair regs[15];
  647.   reg_pair newRegs[15];
  648.  
  649.   memcpy(®s[0], ®[0], sizeof(reg_pair) * 15);
  650.   
  651.   while(count < 20) {
  652.     char *addr = elfGetAddressSymbol(address);
  653.     if(*addr == 0)
  654.       addr = "???";
  655.     
  656.     printf("%08x %s\n", address, addr);
  657.     
  658.     ELFfde *fde = elfGetFde(address);
  659.     
  660.     if(fde == NULL) {
  661.       break;
  662.     }
  663.     
  664.     ELFFrameState *state = elfGetFrameState(fde, address);
  665.     
  666.     if(!state) {
  667.       break;
  668.     }
  669.     
  670.     if(state->cfaMode == CFA_REG_OFFSET) {
  671.       memcpy(&newRegs[0], ®s[0], sizeof(reg_pair) * 15);      
  672.       u32 addr = 0;
  673.       for(int i = 0; i < 15; i++) {
  674.         ELFFrameStateRegister *r = &state->registers.
  675.           regs[i];
  676.         
  677.         switch(r->mode) {
  678.         case REG_NOT_SET:
  679.           newRegs[i].I = regs[i].I;
  680.           break;
  681.         case REG_OFFSET:
  682.           newRegs[i].I = elfReadMemory(regs[state->cfaRegister].I +
  683.                                        state->cfaOffset +
  684.                                        r->offset);
  685.           break;
  686.         case REG_REGISTER:
  687.           newRegs[i].I = regs[r->reg].I;
  688.           break;
  689.         default:
  690.           printf("Unknown register mode: %d\n", r->mode);
  691.           break;
  692.         }
  693.       }
  694.       memcpy(regs, newRegs, sizeof(reg_pair)*15);
  695.       addr = newRegs[14].I;
  696.       addr &= 0xfffffffe;
  697.       address = addr;
  698.       count++;
  699.     } else {
  700.       printf("CFA not set\n");
  701.       break;
  702.     }
  703.     if(state->registers.previous) {
  704.       ELFFrameStateRegisters *prev = state->registers.previous;
  705.  
  706.       while(prev) {
  707.         ELFFrameStateRegisters *p = prev->previous;
  708.         free(prev);
  709.         prev = p;
  710.       }
  711.     }
  712.     free(state);
  713.   }
  714. }
  715.  
  716. u32 elfDecodeLocation(Function *f, ELFBlock *o, LocationType *type, u32 base)
  717. {
  718.   u32 framebase = 0;
  719.   if(f && f->frameBase) {
  720.     ELFBlock *b = f->frameBase;
  721.     switch(*b->data) {
  722.     case DW_OP_reg0:
  723.     case DW_OP_reg1:
  724.     case DW_OP_reg2:
  725.     case DW_OP_reg3:
  726.     case DW_OP_reg4:
  727.     case DW_OP_reg5:
  728.     case DW_OP_reg6:
  729.     case DW_OP_reg7:
  730.     case DW_OP_reg8:
  731.     case DW_OP_reg9:
  732.     case DW_OP_reg10:
  733.     case DW_OP_reg11:
  734.     case DW_OP_reg12:
  735.     case DW_OP_reg13:
  736.     case DW_OP_reg14:
  737.     case DW_OP_reg15:
  738.       framebase = reg[*b->data-0x50].I;
  739.       break;
  740.     default:
  741.       fprintf(stderr, "Unknown frameBase %02x\n", *b->data);
  742.       break;
  743.     }
  744.   }
  745.   
  746.   ELFBlock *loc = o;
  747.   u32 location = 0;
  748.   int bytes = 0;
  749.   if(loc) {
  750.     switch(*loc->data) {
  751.     case DW_OP_addr:
  752.       location = elfRead4Bytes(loc->data+1);
  753.       *type = LOCATION_memory;
  754.       break;
  755.     case DW_OP_plus_uconst:
  756.       location = base + elfReadLEB128(loc->data+1, &bytes);
  757.       *type = LOCATION_memory;
  758.       break;
  759.     case DW_OP_reg0:
  760.     case DW_OP_reg1:
  761.     case DW_OP_reg2:
  762.     case DW_OP_reg3:
  763.     case DW_OP_reg4:
  764.     case DW_OP_reg5:
  765.     case DW_OP_reg6:
  766.     case DW_OP_reg7:
  767.     case DW_OP_reg8:
  768.     case DW_OP_reg9:
  769.     case DW_OP_reg10:
  770.     case DW_OP_reg11:
  771.     case DW_OP_reg12:
  772.     case DW_OP_reg13:
  773.     case DW_OP_reg14:
  774.     case DW_OP_reg15:      
  775.       location = *loc->data - 0x50;
  776.       *type = LOCATION_register;
  777.       break;
  778.     case DW_OP_fbreg:
  779.       {
  780.         int bytes;
  781.         s32 off = elfReadSignedLEB128(loc->data+1, &bytes);
  782.         location = framebase + off;
  783.         *type = LOCATION_memory;
  784.       }
  785.       break;
  786.     default:
  787.       fprintf(stderr, "Unknown location %02x\n", *loc->data);
  788.       break;
  789.     }
  790.   }
  791.   return location;
  792. }
  793.  
  794. u32 elfDecodeLocation(Function *f, ELFBlock *o, LocationType *type)
  795. {
  796.   return elfDecodeLocation(f, o, type, 0);
  797. }
  798.  
  799. // reading function
  800.  
  801. u32 elfRead4Bytes(u8 *data)
  802. {
  803.   u32 value = *data++;
  804.   value |= (*data++ << 8);
  805.   value |= (*data++ << 16);
  806.   value |= (*data << 24);
  807.   return value;
  808. }
  809.  
  810. u16 elfRead2Bytes(u8 *data)
  811. {
  812.   u16 value = *data++;
  813.   value |= (*data << 8);
  814.   return value;
  815. }
  816.  
  817. char *elfReadString(u8 *data, int *bytesRead)
  818. {
  819.   if(*data == 0) {
  820.     *bytesRead = 1;
  821.     return NULL;
  822.   }
  823.   *bytesRead = strlen((char *)data) + 1;
  824.   return (char *)data;
  825. }
  826.  
  827. s32 elfReadSignedLEB128(u8 *data, int *bytesRead)
  828. {
  829.   s32 result = 0;
  830.   int shift = 0;
  831.   int count = 0;
  832.   
  833.   u8 byte;
  834.   do {
  835.     byte = *data++;
  836.     count++;
  837.     result |= (byte & 0x7f) << shift;
  838.     shift += 7;
  839.   } while(byte & 0x80);
  840.   if((shift < 32) && (byte & 0x40))
  841.     result |= -(1 << shift);
  842.   *bytesRead = count;
  843.   return result;
  844. }
  845.  
  846. u32 elfReadLEB128(u8 *data, int *bytesRead)
  847. {
  848.   u32 result = 0;
  849.   int shift = 0;
  850.   int count = 0;
  851.   u8 byte;
  852.   do {
  853.     byte = *data++;
  854.     count++;
  855.     result |= (byte & 0x7f) << shift;
  856.     shift += 7;
  857.   } while(byte & 0x80);
  858.   *bytesRead = count;
  859.   return result;
  860. }
  861.  
  862. u8 *elfReadSection(u8 *data, ELFSectionHeader *sh)
  863. {
  864.   return data + READ32LE(&sh->offset);
  865. }
  866.  
  867. ELFSectionHeader *elfGetSectionByName(char *name)
  868. {
  869.   for(int i = 0; i < elfSectionHeadersCount; i++) {
  870.     if(strcmp(name,
  871.               &elfSectionHeadersStringTable[READ32LE(&elfSectionHeaders[i]->
  872.                                                      name)]) == 0) {
  873.       return elfSectionHeaders[i];
  874.     }
  875.   }
  876.   return NULL;
  877. }
  878.  
  879. ELFSectionHeader *elfGetSectionByNumber(int number)
  880. {
  881.   if(number < elfSectionHeadersCount) {
  882.     return elfSectionHeaders[number];
  883.   }
  884.   return NULL;
  885. }
  886.  
  887. CompileUnit *elfGetCompileUnitForData(u8 *data)
  888. {
  889.   u8 *end = elfCurrentUnit->top + 4 + elfCurrentUnit->length;
  890.  
  891.   if(data >= elfCurrentUnit->top && data < end)
  892.     return elfCurrentUnit;
  893.  
  894.   CompileUnit *unit = elfCompileUnits;
  895.  
  896.   while(unit) {
  897.     end = unit->top + 4 + unit->length;
  898.  
  899.     if(data >= unit->top && data < end)
  900.       return unit;
  901.  
  902.     unit = unit->next;
  903.   }
  904.  
  905.   printf("Error: cannot find reference to compile unit at offset %08x\n",
  906.          (int)(data - elfDebugInfo->infodata));
  907.   exit(-1);
  908. }
  909.  
  910. u8 *elfReadAttribute(u8 *data, ELFAttr *attr)
  911. {
  912.   int bytes;
  913.   int form = attr->form;
  914.  start:
  915.   switch(form) {
  916.   case DW_FORM_addr:
  917.     attr->value = elfRead4Bytes(data);
  918.     data += 4;
  919.     break;
  920.   case DW_FORM_data2:
  921.     attr->value = elfRead2Bytes(data);
  922.     data += 2;
  923.     break;
  924.   case DW_FORM_data4:
  925.     attr->value = elfRead4Bytes(data);
  926.     data += 4;
  927.     break;
  928.   case DW_FORM_string:
  929.     attr->string = (char *)data;
  930.     data += strlen(attr->string)+1;
  931.     break;
  932.   case DW_FORM_strp:
  933.     attr->string = elfDebugStrings + elfRead4Bytes(data);
  934.     data += 4;
  935.     break;
  936.   case DW_FORM_block:
  937.     attr->block = (ELFBlock *)malloc(sizeof(ELFBlock));
  938.     attr->block->length = elfReadLEB128(data, &bytes);
  939.     data += bytes;
  940.     attr->block->data = data;
  941.     data += attr->block->length;
  942.     break;
  943.   case DW_FORM_block1:
  944.     attr->block = (ELFBlock *)malloc(sizeof(ELFBlock));
  945.     attr->block->length = *data++;
  946.     attr->block->data = data;
  947.     data += attr->block->length;
  948.     break;
  949.   case DW_FORM_data1:
  950.     attr->value = *data++;
  951.     break;
  952.   case DW_FORM_flag:
  953.     attr->flag = (*data++) ? true : false;
  954.     break;
  955.   case DW_FORM_sdata:
  956.     attr->value = elfReadSignedLEB128(data, &bytes);
  957.     data += bytes;
  958.     break;
  959.   case DW_FORM_udata:
  960.     attr->value = elfReadLEB128(data, &bytes);
  961.     data += bytes;
  962.     break;
  963.   case DW_FORM_ref_addr:
  964.     attr->value = (elfDebugInfo->infodata + elfRead4Bytes(data)) -
  965.       elfGetCompileUnitForData(data)->top;
  966.     data += 4;
  967.     break;
  968.   case DW_FORM_ref4:
  969.     attr->value = elfRead4Bytes(data);
  970.     data += 4;
  971.     break;
  972.   case DW_FORM_ref_udata:
  973.     attr->value = (elfDebugInfo->infodata +
  974.                    (elfGetCompileUnitForData(data)->top -
  975.                     elfDebugInfo->infodata) +
  976.                    elfReadLEB128(data, &bytes)) -
  977.       elfCurrentUnit->top;
  978.     data += bytes;
  979.     break;    
  980.   case DW_FORM_indirect:
  981.     form = elfReadLEB128(data, &bytes);
  982.     data += bytes;
  983.     goto start;
  984.   default:
  985.     fprintf(stderr, "Unsupported FORM %02x\n", form);
  986.     exit(-1);
  987.   }
  988.   return data;
  989. }
  990.  
  991. ELFAbbrev *elfGetAbbrev(ELFAbbrev **table, u32 number)
  992. {
  993.   int hash = number % 121;
  994.  
  995.   ELFAbbrev *abbrev = table[hash];
  996.  
  997.   while(abbrev) {
  998.     if(abbrev->number == number)
  999.       return abbrev;
  1000.     abbrev = abbrev->next;
  1001.   }
  1002.   return NULL;
  1003. }
  1004.  
  1005. ELFAbbrev **elfReadAbbrevs(u8 *data, u32 offset)
  1006. {
  1007.   data += offset;
  1008.   ELFAbbrev **abbrevs = (ELFAbbrev **)calloc(sizeof(ELFAbbrev *)*121,1);
  1009.   int bytes = 0;
  1010.   u32 number = elfReadLEB128(data, &bytes);
  1011.   data += bytes;
  1012.   while(number) {
  1013.     ELFAbbrev *abbrev = (ELFAbbrev *)calloc(sizeof(ELFAbbrev),1);
  1014.  
  1015.     // read tag information
  1016.     abbrev->number = number;
  1017.     abbrev->tag = elfReadLEB128(data, &bytes);
  1018.     data += bytes;
  1019.     abbrev->hasChildren = *data++ ? true: false;
  1020.  
  1021.     // read attributes
  1022.     int name = elfReadLEB128(data, &bytes);
  1023.     data += bytes;
  1024.     int form = elfReadLEB128(data, &bytes);
  1025.     data += bytes;
  1026.  
  1027.     while(name) {
  1028.       if((abbrev->numAttrs % 4) == 0) {
  1029.         abbrev->attrs = (ELFAttr *)realloc(abbrev->attrs,
  1030.                                            (abbrev->numAttrs + 4) *
  1031.                                            sizeof(ELFAttr));
  1032.       }
  1033.       abbrev->attrs[abbrev->numAttrs].name = name;
  1034.       abbrev->attrs[abbrev->numAttrs++].form = form;
  1035.  
  1036.       name = elfReadLEB128(data, &bytes);
  1037.       data += bytes;
  1038.       form = elfReadLEB128(data, &bytes);
  1039.       data += bytes;
  1040.     }
  1041.  
  1042.     int hash = number % 121;
  1043.     abbrev->next = abbrevs[hash];
  1044.     abbrevs[hash] = abbrev;
  1045.  
  1046.     number = elfReadLEB128(data, &bytes);
  1047.     data += bytes;
  1048.  
  1049.     if(elfGetAbbrev(abbrevs, number) != NULL)
  1050.       break;
  1051.   }
  1052.  
  1053.   return abbrevs;
  1054. }
  1055.  
  1056. void elfParseCFA(u8 *top)
  1057. {
  1058.   ELFSectionHeader *h = elfGetSectionByName(".debug_frame");
  1059.  
  1060.   if(h == NULL) {
  1061.     return;
  1062.   }
  1063.  
  1064.   u8 *data = elfReadSection(top, h);
  1065.  
  1066.   u8 *topOffset = data;
  1067.  
  1068.   u8 *end = data + READ32LE(&h->size);
  1069.  
  1070.   ELFcie *cies = NULL;
  1071.   
  1072.   while(data < end) {
  1073.     u32 offset = data - topOffset;
  1074.     u32 len = elfRead4Bytes(data);
  1075.     data += 4;
  1076.  
  1077.     u8 *dataEnd = data + len;
  1078.     
  1079.     u32 id = elfRead4Bytes(data);
  1080.     data += 4;
  1081.     
  1082.     if(id == 0xffffffff) {
  1083.       // skip version
  1084.       *data++;
  1085.  
  1086.       ELFcie *cie = (ELFcie *)calloc(1, sizeof(ELFcie));
  1087.  
  1088.       cie->next = cies;
  1089.       cies = cie;
  1090.  
  1091.       cie->offset = offset;
  1092.       
  1093.       cie->augmentation = data;
  1094.       while(*data)
  1095.         data++;
  1096.       data++;
  1097.       
  1098.       if(*cie->augmentation) {
  1099.         fprintf(stderr, "Error: augmentation not supported\n");
  1100.         exit(-1);
  1101.       }
  1102.       
  1103.       int bytes;
  1104.       cie->codeAlign = elfReadLEB128(data, &bytes);
  1105.       data += bytes;
  1106.       
  1107.       cie->dataAlign = elfReadSignedLEB128(data, &bytes);
  1108.       data += bytes;
  1109.       
  1110.       cie->returnAddress = *data++;
  1111.  
  1112.       cie->data = data;
  1113.       cie->dataLen = dataEnd - data;
  1114.     } else {
  1115.       ELFfde *fde = (ELFfde *)calloc(1, sizeof(ELFfde));
  1116.  
  1117.       ELFcie *cie = cies;
  1118.  
  1119.       while(cie != NULL) {
  1120.         if(cie->offset == id)
  1121.           break;
  1122.         cie = cie->next;
  1123.       }
  1124.  
  1125.       if(!cie) {
  1126.         fprintf(stderr, "Cannot find CIE %08x\n", id);
  1127.         exit(-1);
  1128.       }
  1129.  
  1130.       fde->cie = cie;
  1131.       
  1132.       fde->address = elfRead4Bytes(data);
  1133.       data += 4;
  1134.       
  1135.       fde->end = fde->address + elfRead4Bytes(data);
  1136.       data += 4;
  1137.  
  1138.       fde->data = data;
  1139.       fde->dataLen = dataEnd - data;
  1140.  
  1141.       if((elfFdeCount %10) == 0) {
  1142.         elfFdes = (ELFfde **)realloc(elfFdes, (elfFdeCount+10) *
  1143.                                     sizeof(ELFfde *));
  1144.       }
  1145.       elfFdes[elfFdeCount++] = fde;
  1146.     }
  1147.     data = dataEnd;
  1148.   }
  1149.  
  1150.   elfCies = cies;
  1151. }
  1152.  
  1153. void elfAddLine(LineInfo *l, u32 a, int file, int line, int *max)
  1154. {
  1155.   if(l->number == *max) {
  1156.     *max += 1000;
  1157.     l->lines = (LineInfoItem *)realloc(l->lines, *max*sizeof(LineInfoItem));
  1158.   }
  1159.   LineInfoItem *li = &l->lines[l->number];
  1160.   li->file = l->files[file-1];
  1161.   li->address = a;
  1162.   li->line = line;
  1163.   l->number++;
  1164. }
  1165.  
  1166. void elfParseLineInfo(CompileUnit *unit, u8 *top)
  1167. {
  1168.   ELFSectionHeader *h = elfGetSectionByName(".debug_line");
  1169.   if(h == NULL) {
  1170.     fprintf(stderr, "No line information found\n");
  1171.     return;
  1172.   }
  1173.   LineInfo *l = unit->lineInfoTable = (LineInfo *)calloc(1, sizeof(LineInfo));
  1174.   l->number = 0;
  1175.   int max = 1000;
  1176.   l->lines = (LineInfoItem *)malloc(1000*sizeof(LineInfoItem));
  1177.   
  1178.   u8 *data = elfReadSection(top, h);
  1179.   data += unit->lineInfo;
  1180.   u32 totalLen = elfRead4Bytes(data);
  1181.   data += 4;
  1182.   u8 *end = data + totalLen;
  1183.   //  u16 version = elfRead2Bytes(data);
  1184.   data += 2;
  1185.   //  u32 offset = elfRead4Bytes(data);
  1186.   data += 4;
  1187.   int minInstrSize = *data++;
  1188.   int defaultIsStmt = *data++;
  1189.   int lineBase = (s8)*data++;
  1190.   int lineRange = *data++;
  1191.   int opcodeBase = *data++;
  1192.   u8 *stdOpLen = (u8 *)malloc(opcodeBase * sizeof(u8));
  1193.   stdOpLen[0] = 1;
  1194.   int i;
  1195.   for(i = 1; i < opcodeBase; i++)
  1196.     stdOpLen[i] = *data++;
  1197.  
  1198.   free(stdOpLen);// todo
  1199.   int bytes = 0;
  1200.  
  1201.   char *s;
  1202.   while((s = elfReadString(data, &bytes)) != NULL) {
  1203.     data += bytes;
  1204.     //    fprintf(stderr, "Directory is %s\n", s);
  1205.   }
  1206.   data += bytes;
  1207.   int count = 4;
  1208.   int index = 0;
  1209.   l->files = (char **)malloc(sizeof(char *)*count);
  1210.   
  1211.   while((s = elfReadString(data, &bytes)) != NULL) {
  1212.     l->files[index++] = s;
  1213.     
  1214.     data += bytes;
  1215.     // directory
  1216.     elfReadLEB128(data, &bytes);
  1217.     data += bytes;
  1218.     // time
  1219.     elfReadLEB128(data, &bytes);
  1220.     data += bytes;
  1221.     // size
  1222.     elfReadLEB128(data, &bytes);
  1223.     data += bytes;
  1224.     //    fprintf(stderr, "File is %s\n", s);
  1225.     if(index == count) {
  1226.       count += 4;
  1227.       l->files = (char **)realloc(l->files, sizeof(char *)*count);
  1228.     }
  1229.   }
  1230.   l->fileCount = index;
  1231.   data += bytes;
  1232.  
  1233.   while(data < end) {
  1234.     u32 address = 0;
  1235.     int file = 1;
  1236.     int line = 1;
  1237.     int col = 0;
  1238.     int isStmt = defaultIsStmt;
  1239.     int basicBlock = 0;
  1240.     int endSeq = 0;
  1241.  
  1242.     while(!endSeq) {
  1243.       int op = *data++;
  1244.       switch(op) {
  1245.       case DW_LNS_extended_op:
  1246.         {
  1247.           data++;
  1248.           op = *data++;
  1249.           switch(op) {
  1250.           case DW_LNE_end_sequence:
  1251.             endSeq = 1;
  1252.             break;
  1253.           case DW_LNE_set_address:
  1254.             address = elfRead4Bytes(data);
  1255.             data += 4;
  1256.             break;
  1257.           default:
  1258.             fprintf(stderr, "Unknown extended LINE opcode %02x\n", op);
  1259.             exit(-1);
  1260.           }
  1261.         }
  1262.         break;
  1263.       case DW_LNS_copy:
  1264.         //      fprintf(stderr, "Address %08x line %d (%d)\n", address, line, file);
  1265.         elfAddLine(l, address, file, line, &max);
  1266.         basicBlock = 0;
  1267.         break;
  1268.       case DW_LNS_advance_pc:
  1269.         address += minInstrSize * elfReadLEB128(data, &bytes);
  1270.         data += bytes;
  1271.         break;
  1272.       case DW_LNS_advance_line:
  1273.         line += elfReadSignedLEB128(data, &bytes);
  1274.         data += bytes;
  1275.         break;
  1276.       case DW_LNS_set_file:
  1277.         file = elfReadLEB128(data, &bytes);
  1278.         data += bytes;
  1279.         break;
  1280.       case DW_LNS_set_column:
  1281.         col = elfReadLEB128(data, &bytes);
  1282.         data += bytes;
  1283.         break;
  1284.       case DW_LNS_negate_stmt:
  1285.         isStmt = !isStmt;
  1286.         break;
  1287.       case DW_LNS_set_basic_block:
  1288.         basicBlock = 1;
  1289.         break;
  1290.       case DW_LNS_const_add_pc:
  1291.         address += (minInstrSize *((255 - opcodeBase)/lineRange));
  1292.         break;
  1293.       case DW_LNS_fixed_advance_pc:
  1294.         address += elfRead2Bytes(data);
  1295.         data += 2;
  1296.         break;
  1297.       default:
  1298.         op = op - opcodeBase;
  1299.         address += (op / lineRange) * minInstrSize;
  1300.         line += lineBase + (op % lineRange);
  1301.         elfAddLine(l, address, file, line, &max);        
  1302.         //        fprintf(stderr, "Address %08x line %d (%d)\n", address, line,file);
  1303.         basicBlock = 1;
  1304.         break;
  1305.       }
  1306.     }
  1307.   }
  1308.   l->lines = (LineInfoItem *)realloc(l->lines, l->number*sizeof(LineInfoItem));
  1309. }
  1310.  
  1311. u8 *elfSkipData(u8 *data, ELFAbbrev *abbrev, ELFAbbrev **abbrevs)
  1312. {
  1313.   int i;
  1314.   int bytes;
  1315.   
  1316.   for(i = 0; i < abbrev->numAttrs; i++) {
  1317.     data = elfReadAttribute(data,  &abbrev->attrs[i]);
  1318.     if(abbrev->attrs[i].form == DW_FORM_block1)
  1319.       free(abbrev->attrs[i].block);
  1320.   }
  1321.     
  1322.   if(abbrev->hasChildren) {
  1323.     int nesting = 1;
  1324.     while(nesting) {
  1325.       u32 abbrevNum = elfReadLEB128(data, &bytes);
  1326.       data += bytes;
  1327.       
  1328.       if(!abbrevNum) {
  1329.         nesting--;
  1330.         continue;
  1331.       }
  1332.       
  1333.       abbrev = elfGetAbbrev(abbrevs, abbrevNum);
  1334.       
  1335.       for(i = 0; i < abbrev->numAttrs; i++) {
  1336.         data = elfReadAttribute(data,  &abbrev->attrs[i]);
  1337.         if(abbrev->attrs[i].form == DW_FORM_block1)
  1338.           free(abbrev->attrs[i].block);
  1339.       }
  1340.       
  1341.       if(abbrev->hasChildren) {
  1342.         nesting++;
  1343.       }
  1344.     }
  1345.   }
  1346.   return data;
  1347. }
  1348.  
  1349. Type *elfParseType(CompileUnit *unit, u32);
  1350. u8 *elfParseObject(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit,
  1351.                    Object **object);
  1352. u8 *elfParseFunction(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit,
  1353.                      Function **function);
  1354. void elfCleanUp(Function *);
  1355.  
  1356. void elfAddType(Type *type, CompileUnit *unit, u32 offset)
  1357. {
  1358.   if(type->next == NULL) {
  1359.     if(unit->types != type && type->offset == 0) {
  1360.       type->offset = offset;
  1361.       type->next = unit->types;
  1362.       unit->types = type;
  1363.     }
  1364.   }  
  1365. }
  1366.  
  1367. void elfParseType(u8 *data, u32 offset, ELFAbbrev *abbrev, CompileUnit *unit,
  1368.                   Type **type)
  1369. {
  1370.   switch(abbrev->tag) {
  1371.   case DW_TAG_typedef:
  1372.     {
  1373.       u32 typeref = 0;
  1374.       char *name = NULL;      
  1375.       for(int i = 0; i < abbrev->numAttrs; i++) {
  1376.         ELFAttr *attr = &abbrev->attrs[i];
  1377.         data = elfReadAttribute(data, attr);
  1378.         switch(attr->name) {
  1379.         case DW_AT_name:
  1380.           name = attr->string;
  1381.           break;
  1382.         case DW_AT_type:
  1383.           typeref = attr->value;
  1384.           break;
  1385.         case DW_AT_decl_file:
  1386.         case DW_AT_decl_line:
  1387.           break;          
  1388.         default:
  1389.           fprintf(stderr, "Unknown attribute for typedef %02x\n", attr->name);
  1390.           break;
  1391.         }
  1392.       }
  1393.       if(abbrev->hasChildren)
  1394.         fprintf(stderr, "Unexpected children for typedef\n");
  1395.       *type = elfParseType(unit, typeref);
  1396.       if(name)
  1397.         (*type)->name = name;
  1398.       return;
  1399.     }
  1400.     break;
  1401.   case DW_TAG_union_type:
  1402.   case DW_TAG_structure_type:
  1403.     {
  1404.       Type *t = (Type *)calloc(sizeof(Type), 1);
  1405.       if(abbrev->tag == DW_TAG_structure_type)
  1406.         t->type = TYPE_struct;
  1407.       else
  1408.         t->type = TYPE_union;
  1409.       
  1410.       Struct *s = (Struct *)calloc(sizeof(Struct), 1);
  1411.       t->structure = s;
  1412.       elfAddType(t, unit, offset);
  1413.       
  1414.       for(int i = 0; i < abbrev->numAttrs; i++) {
  1415.         ELFAttr *attr = &abbrev->attrs[i];
  1416.         data = elfReadAttribute(data, attr);
  1417.         switch(attr->name) {
  1418.         case DW_AT_name:
  1419.           t->name = attr->string;
  1420.           break;
  1421.         case DW_AT_byte_size:
  1422.           t->size = attr->value;
  1423.           break;
  1424.         case DW_AT_decl_file:
  1425.         case DW_AT_decl_line:
  1426.         case DW_AT_sibling:
  1427.         case DW_AT_containing_type: // todo?
  1428.         case DW_AT_declaration:
  1429.   case DW_AT_specification: // TODO:
  1430.           break;
  1431.         default:
  1432.           fprintf(stderr, "Unknown attribute for struct %02x\n", attr->name);
  1433.           break;
  1434.         }
  1435.       }
  1436.       if(abbrev->hasChildren) {
  1437.         int bytes;
  1438.         u32 num = elfReadLEB128(data, &bytes);
  1439.         data += bytes;
  1440.         int index = 0;
  1441.         while(num) {
  1442.           ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num);
  1443.           
  1444.           switch(abbr->tag) {
  1445.           case DW_TAG_member:
  1446.             {
  1447.               if((index % 4) == 0)
  1448.                 s->members = (Member *)realloc(s->members,
  1449.                                                sizeof(Member)*(index+4));
  1450.               Member *m = &s->members[index];
  1451.               m->location = NULL;
  1452.               m->bitOffset = 0;
  1453.               m->bitSize = 0;
  1454.               m->byteSize = 0;
  1455.               for(int i = 0; i < abbr->numAttrs; i++) {
  1456.                 ELFAttr *attr = &abbr->attrs[i];
  1457.                 data = elfReadAttribute(data, attr);
  1458.                 switch(attr->name) {
  1459.                 case DW_AT_name:
  1460.                   m->name = attr->string;
  1461.                   break;
  1462.                 case DW_AT_type:
  1463.                   m->type = elfParseType(unit, attr->value);
  1464.                   break;
  1465.                 case DW_AT_data_member_location:
  1466.                   m->location = attr->block;
  1467.                   break;
  1468.                 case DW_AT_byte_size:
  1469.                   m->byteSize = attr->value;
  1470.                   break;
  1471.                 case DW_AT_bit_offset:
  1472.                   m->bitOffset = attr->value;
  1473.                   break;
  1474.                 case DW_AT_bit_size:
  1475.                   m->bitSize = attr->value;
  1476.                   break;
  1477.                 case DW_AT_decl_file:
  1478.                 case DW_AT_decl_line:
  1479.                 case DW_AT_accessibility:
  1480.                 case DW_AT_artificial: // todo?
  1481.                   break;
  1482.                 default:
  1483.                   fprintf(stderr, "Unknown member attribute %02x\n",
  1484.                           attr->name);
  1485.                 }
  1486.               }
  1487.               index++;
  1488.             }
  1489.             break;
  1490.           case DW_TAG_subprogram:
  1491.             {
  1492.               Function *fnc = NULL;
  1493.               data = elfParseFunction(data, abbr, unit, &fnc);
  1494.               if(fnc != NULL) {
  1495.                 if(unit->lastFunction)
  1496.                   unit->lastFunction->next = fnc;
  1497.                 else
  1498.                   unit->functions = fnc;
  1499.                 unit->lastFunction = fnc;
  1500.               }
  1501.             }
  1502.             break;
  1503.           case DW_TAG_inheritance:
  1504.             // TODO: add support
  1505.             data = elfSkipData(data, abbr, unit->abbrevs);
  1506.             break;
  1507.           CASE_TYPE_TAG:
  1508.             // skip types... parsed only when used
  1509.             data = elfSkipData(data, abbr, unit->abbrevs);
  1510.             break;
  1511.           case DW_TAG_variable:
  1512.             data = elfSkipData(data, abbr, unit->abbrevs);
  1513.             break;
  1514.           default:
  1515.             fprintf(stderr, "Unknown struct tag %02x %s\n", abbr->tag, t->name);
  1516.             data = elfSkipData(data, abbr, unit->abbrevs);
  1517.             break;
  1518.           }
  1519.           num = elfReadLEB128(data, &bytes);
  1520.           data += bytes;
  1521.         }
  1522.         s->memberCount = index;
  1523.       }
  1524.       *type = t;
  1525.       return;
  1526.     }
  1527.     break;
  1528.   case DW_TAG_base_type:
  1529.     {
  1530.       Type *t = (Type *)calloc(sizeof(Type), 1);
  1531.  
  1532.       t->type = TYPE_base;
  1533.       elfAddType(t, unit, offset);
  1534.       for(int i = 0; i < abbrev->numAttrs; i++) {
  1535.         ELFAttr *attr = &abbrev->attrs[i];
  1536.         data = elfReadAttribute(data, attr);
  1537.         switch(attr->name) {
  1538.         case DW_AT_name:
  1539.           t->name = attr->string;
  1540.           break;
  1541.         case DW_AT_encoding:
  1542.           t->encoding = attr->value;
  1543.           break;
  1544.         case DW_AT_byte_size:
  1545.           t->size = attr->value;
  1546.           break;
  1547.         case DW_AT_bit_size:
  1548.           t->bitSize = attr->value;
  1549.           break;
  1550.         default:
  1551.           fprintf(stderr, "Unknown attribute for base type %02x\n",
  1552.                   attr->name);
  1553.           break;
  1554.         }
  1555.       }
  1556.       if(abbrev->hasChildren)
  1557.         fprintf(stderr, "Unexpected children for base type\n");
  1558.       *type = t;
  1559.       return;
  1560.     }
  1561.     break;
  1562.   case DW_TAG_pointer_type:
  1563.     {
  1564.       Type *t = (Type *)calloc(sizeof(Type), 1);
  1565.  
  1566.       t->type = TYPE_pointer;
  1567.  
  1568.       elfAddType(t, unit, offset);
  1569.       
  1570.       for(int i = 0; i < abbrev->numAttrs; i++) {
  1571.         ELFAttr *attr = &abbrev->attrs[i];
  1572.         data =elfReadAttribute(data, attr);
  1573.         switch(attr->name) {
  1574.         case DW_AT_type:
  1575.           t->pointer = elfParseType(unit, attr->value);
  1576.           break;
  1577.         case DW_AT_byte_size:
  1578.           t->size = attr->value;
  1579.           break;
  1580.         default:
  1581.           fprintf(stderr, "Unknown pointer type attribute %02x\n", attr->name);
  1582.           break;
  1583.         }
  1584.       }
  1585.       if(abbrev->hasChildren)
  1586.         fprintf(stderr, "Unexpected children for pointer type\n");
  1587.       *type = t;
  1588.       return;
  1589.     }
  1590.     break;
  1591.   case DW_TAG_reference_type:
  1592.     {
  1593.       Type *t = (Type *)calloc(sizeof(Type), 1);
  1594.  
  1595.       t->type = TYPE_reference;
  1596.  
  1597.       elfAddType(t, unit, offset);
  1598.       
  1599.       for(int i = 0; i < abbrev->numAttrs; i++) {
  1600.         ELFAttr *attr = &abbrev->attrs[i];
  1601.         data =elfReadAttribute(data, attr);
  1602.         switch(attr->name) {
  1603.         case DW_AT_type:
  1604.           t->pointer = elfParseType(unit, attr->value);
  1605.           break;
  1606.         case DW_AT_byte_size:
  1607.           t->size = attr->value;
  1608.           break;
  1609.         default:
  1610.           fprintf(stderr, "Unknown ref type attribute %02x\n", attr->name);
  1611.           break;
  1612.         }
  1613.       }
  1614.       if(abbrev->hasChildren)
  1615.         fprintf(stderr, "Unexpected children for ref type\n");
  1616.       *type = t;
  1617.       return;
  1618.     }
  1619.     break;
  1620.   case DW_TAG_volatile_type:
  1621.     {
  1622.       u32 typeref = 0;
  1623.       
  1624.       for(int i = 0; i < abbrev->numAttrs; i++) {
  1625.         ELFAttr *attr = &abbrev->attrs[i];
  1626.         data = elfReadAttribute(data, attr);
  1627.         switch(attr->name) {
  1628.         case DW_AT_type:
  1629.           typeref = attr->value;
  1630.           break;
  1631.         default:
  1632.           fprintf(stderr, "Unknown volatile attribute for type %02x\n",
  1633.                   attr->name);
  1634.           break;
  1635.         }
  1636.       }
  1637.       if(abbrev->hasChildren)
  1638.         fprintf(stderr, "Unexpected children for volatile type\n");
  1639.       *type = elfParseType(unit, typeref);
  1640.       return;
  1641.     }
  1642.     break;
  1643.   case DW_TAG_const_type:
  1644.     {
  1645.       u32 typeref = 0;
  1646.       
  1647.       for(int i = 0; i < abbrev->numAttrs; i++) {
  1648.         ELFAttr *attr = &abbrev->attrs[i];
  1649.         data = elfReadAttribute(data, attr);
  1650.         switch(attr->name) {
  1651.         case DW_AT_type:
  1652.           typeref = attr->value;
  1653.           break;
  1654.         default:
  1655.           fprintf(stderr, "Unknown const attribute for type %02x\n",
  1656.                   attr->name);
  1657.           break;
  1658.         }
  1659.       }
  1660.       if(abbrev->hasChildren)
  1661.         fprintf(stderr, "Unexpected children for const type\n");
  1662.       *type = elfParseType(unit, typeref);
  1663.       return;
  1664.     }
  1665.     break;
  1666.   case DW_TAG_enumeration_type:
  1667.     {
  1668.       Type *t = (Type *)calloc(sizeof(Type), 1);
  1669.       t->type = TYPE_enum;
  1670.       Enum *e = (Enum *)calloc(sizeof(Enum), 1);
  1671.       t->enumeration = e;
  1672.       elfAddType(t, unit, offset);
  1673.       int count = 0;
  1674.       for(int i = 0; i < abbrev->numAttrs; i++) {
  1675.         ELFAttr *attr = &abbrev->attrs[i];
  1676.         data = elfReadAttribute(data, attr);
  1677.         switch(attr->name) {
  1678.         case DW_AT_name:
  1679.           t->name = attr->string;
  1680.           break;
  1681.         case DW_AT_byte_size:
  1682.           t->size = attr->value;
  1683.           break;
  1684.         case DW_AT_sibling:
  1685.         case DW_AT_decl_file:
  1686.         case DW_AT_decl_line:
  1687.           break;
  1688.         default:
  1689.           fprintf(stderr, "Unknown enum attribute %02x\n", attr->name);
  1690.         }
  1691.       }
  1692.       if(abbrev->hasChildren) {
  1693.         int bytes;
  1694.         u32 num = elfReadLEB128(data, &bytes);
  1695.         data += bytes;
  1696.         while(num) {
  1697.           ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num);
  1698.           
  1699.           switch(abbr->tag) {
  1700.           case DW_TAG_enumerator:
  1701.             {
  1702.               count++;
  1703.               e->members = (EnumMember *)realloc(e->members,
  1704.                                                  count*sizeof(EnumMember));
  1705.               EnumMember *m = &e->members[count-1];
  1706.               for(int i = 0; i < abbr->numAttrs; i++) {
  1707.                 ELFAttr *attr = &abbr->attrs[i];
  1708.                 data = elfReadAttribute(data, attr);
  1709.                 switch(attr->name) {
  1710.                 case DW_AT_name:
  1711.                   m->name = attr->string;
  1712.                   break;
  1713.                 case DW_AT_const_value:
  1714.                   m->value = attr->value;
  1715.                   break;
  1716.                 default:
  1717.                   fprintf(stderr, "Unknown sub param attribute %02x\n",
  1718.                           attr->name);
  1719.                 }
  1720.               }
  1721.             }
  1722.             break;
  1723.           default:
  1724.             fprintf(stderr, "Unknown enum tag %02x\n", abbr->tag);
  1725.             data = elfSkipData(data, abbr, unit->abbrevs);          
  1726.             break;
  1727.           }
  1728.           num = elfReadLEB128(data, &bytes);
  1729.           data += bytes;
  1730.         }
  1731.       }
  1732.       e->count = count;
  1733.       *type = t;
  1734.       return;
  1735.     }
  1736.     break;
  1737.   case DW_TAG_subroutine_type:
  1738.     {
  1739.       Type *t = (Type *)calloc(sizeof(Type), 1);
  1740.       t->type = TYPE_function;
  1741.       FunctionType *f = (FunctionType *)calloc(sizeof(FunctionType), 1);
  1742.       t->function = f;      
  1743.       elfAddType(t, unit, offset);
  1744.       for(int i = 0; i < abbrev->numAttrs; i++) {
  1745.         ELFAttr *attr = &abbrev->attrs[i];
  1746.         data = elfReadAttribute(data, attr);
  1747.         switch(attr->name) {
  1748.         case DW_AT_prototyped:
  1749.         case DW_AT_sibling:          
  1750.           break;
  1751.         case DW_AT_type:
  1752.           f->returnType = elfParseType(unit, attr->value);
  1753.           break;
  1754.         default:
  1755.           fprintf(stderr, "Unknown subroutine attribute %02x\n", attr->name);
  1756.         }
  1757.       }
  1758.       if(abbrev->hasChildren) {
  1759.         int bytes;
  1760.         u32 num = elfReadLEB128(data, &bytes);
  1761.         data += bytes;
  1762.         Object *lastVar = NULL;        
  1763.         while(num) {
  1764.           ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num);
  1765.           
  1766.           switch(abbr->tag) {
  1767.           case DW_TAG_formal_parameter:
  1768.             {
  1769.               Object *o;
  1770.               data = elfParseObject(data, abbr, unit, &o);
  1771.               if(f->args)
  1772.                 lastVar->next = o;
  1773.               else
  1774.                 f->args = o;
  1775.               lastVar = o;
  1776.             }
  1777.             break;
  1778.           case DW_TAG_unspecified_parameters:
  1779.             // no use in the debugger yet
  1780.             data = elfSkipData(data, abbr, unit->abbrevs);
  1781.             break;
  1782.           CASE_TYPE_TAG:
  1783.             // skip types... parsed only when used
  1784.             data = elfSkipData(data, abbr, unit->abbrevs);
  1785.             break;
  1786.           default:
  1787.             fprintf(stderr, "Unknown subroutine tag %02x\n", abbr->tag);
  1788.             data = elfSkipData(data, abbr, unit->abbrevs);          
  1789.             break;
  1790.           }
  1791.           num = elfReadLEB128(data, &bytes);
  1792.           data += bytes;
  1793.         }
  1794.       }
  1795.       *type = t;
  1796.       return;
  1797.     }
  1798.     break;
  1799.   case DW_TAG_array_type:
  1800.     {
  1801.       u32 typeref = 0;
  1802.       int i;
  1803.       Array *array = (Array *)calloc(sizeof(Array), 1);
  1804.       Type *t = (Type *)calloc(sizeof(Type), 1);
  1805.       t->type = TYPE_array;
  1806.       elfAddType(t, unit, offset);
  1807.       
  1808.       for(i = 0; i < abbrev->numAttrs; i++) {
  1809.         ELFAttr *attr = &abbrev->attrs[i];
  1810.         data = elfReadAttribute(data, attr);
  1811.         switch(attr->name) {
  1812.         case DW_AT_sibling:
  1813.           break;
  1814.         case DW_AT_type:
  1815.           typeref = attr->value;
  1816.           array->type = elfParseType(unit, typeref);
  1817.           break;
  1818.         default:
  1819.           fprintf(stderr, "Unknown array attribute %02x\n", attr->name);
  1820.         }
  1821.       }
  1822.       if(abbrev->hasChildren) {
  1823.         int bytes;
  1824.         u32 num = elfReadLEB128(data, &bytes);
  1825.         data += bytes;
  1826.         int index = 0;
  1827.         int maxBounds = 0;
  1828.         while(num) {
  1829.           ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num);
  1830.           
  1831.           switch(abbr->tag) {
  1832.           case DW_TAG_subrange_type:
  1833.             {
  1834.               if(maxBounds == index) {
  1835.                 maxBounds += 4;
  1836.                 array->bounds = (int *)realloc(array->bounds,
  1837.                                                sizeof(int)*maxBounds);
  1838.               }
  1839.               for(int i = 0; i < abbr->numAttrs; i++) {
  1840.                 ELFAttr *attr = &abbr->attrs[i];
  1841.                 data = elfReadAttribute(data, attr);
  1842.                 switch(attr->name) {
  1843.                 case DW_AT_upper_bound:
  1844.                   array->bounds[index] = attr->value+1;
  1845.                   break;
  1846.                 case DW_AT_type: // ignore
  1847.                   break;
  1848.                 default:
  1849.                   fprintf(stderr, "Unknown subrange attribute %02x\n",
  1850.                           attr->name);
  1851.                 }
  1852.               }
  1853.               index++;
  1854.             }
  1855.             break;
  1856.           default:
  1857.             fprintf(stderr, "Unknown array tag %02x\n", abbr->tag);
  1858.             data = elfSkipData(data, abbr, unit->abbrevs);          
  1859.             break;
  1860.           }
  1861.           num = elfReadLEB128(data, &bytes);
  1862.           data += bytes;
  1863.         }
  1864.         array->maxBounds = index;
  1865.       }
  1866.       t->size = array->type->size;
  1867.       for(i = 0; i < array->maxBounds; i++)
  1868.         t->size *= array->bounds[i];
  1869.       t->array = array;
  1870.       *type = t;
  1871.       return;
  1872.     }
  1873.     break;
  1874.   default:
  1875.     fprintf(stderr, "Unknown type TAG %02x\n", abbrev->tag);
  1876.     exit(-1);
  1877.   }
  1878. }
  1879.  
  1880. Type *elfParseType(CompileUnit *unit, u32 offset)
  1881. {
  1882.   Type *t = unit->types;
  1883.  
  1884.   while(t) {
  1885.     if(t->offset == offset)
  1886.       return t;
  1887.     t = t->next;
  1888.   }
  1889.   if(offset == 0) {
  1890.     Type *t = (Type *)calloc(sizeof(Type), 1);
  1891.     t->type = TYPE_void;
  1892.     t->offset = 0;
  1893.     elfAddType(t, unit, 0);
  1894.     return t;
  1895.   }
  1896.   u8 *data = unit->top + offset;
  1897.   int bytes;
  1898.   int abbrevNum = elfReadLEB128(data, &bytes);
  1899.   data += bytes;
  1900.   Type *type = NULL;
  1901.  
  1902.   ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum);
  1903.   
  1904.   elfParseType(data, offset, abbrev, unit, &type);
  1905.   return type;
  1906. }
  1907.  
  1908. void elfGetObjectAttributes(CompileUnit *unit, u32 offset, Object *o)
  1909. {
  1910.   u8 *data = unit->top + offset;
  1911.   int bytes;
  1912.   u32 abbrevNum = elfReadLEB128(data, &bytes);
  1913.   data += bytes;
  1914.   
  1915.   if(!abbrevNum) {
  1916.     return;
  1917.   }
  1918.  
  1919.   ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum);
  1920.   
  1921.   for(int i = 0; i < abbrev->numAttrs; i++) {
  1922.     ELFAttr *attr = &abbrev->attrs[i];
  1923.     data = elfReadAttribute(data, attr);
  1924.     switch(attr->name) {
  1925.     case DW_AT_location:
  1926.       o->location = attr->block;
  1927.       break;
  1928.     case DW_AT_name:
  1929.       if(o->name == NULL)
  1930.         o->name = attr->string;
  1931.       break;
  1932.     case DW_AT_MIPS_linkage_name:
  1933.       o->name = attr->string;
  1934.       break;
  1935.     case DW_AT_decl_file:
  1936.       o->file = attr->value;
  1937.       break;
  1938.     case DW_AT_decl_line:
  1939.       o->line = attr->value;
  1940.       break;
  1941.     case DW_AT_type:
  1942.       o->type = elfParseType(unit, attr->value);
  1943.       break;
  1944.     case DW_AT_external:
  1945.       o->external = attr->flag;
  1946.       break;
  1947.     case DW_AT_const_value:      
  1948.     case DW_AT_abstract_origin:
  1949.     case DW_AT_declaration:
  1950.     case DW_AT_artificial:
  1951.       // todo
  1952.       break;
  1953.     case DW_AT_specification:
  1954.       // TODO:
  1955.       break;
  1956.     default:
  1957.       fprintf(stderr, "Unknown object attribute %02x\n", attr->name);
  1958.       break;
  1959.     }
  1960.   }  
  1961. }
  1962.  
  1963. u8 *elfParseObject(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit,
  1964.                    Object **object)
  1965. {
  1966.   Object *o = (Object *)calloc(sizeof(Object), 1);
  1967.  
  1968.   o->next = NULL;
  1969.  
  1970.   for(int i = 0; i < abbrev->numAttrs; i++) {
  1971.     ELFAttr *attr = &abbrev->attrs[i];
  1972.     data = elfReadAttribute(data, attr);
  1973.     switch(attr->name) {
  1974.     case DW_AT_location:
  1975.       o->location = attr->block;
  1976.       break;
  1977.     case DW_AT_name:
  1978.       if(o->name == NULL)
  1979.         o->name = attr->string;
  1980.       break;
  1981.     case DW_AT_MIPS_linkage_name:
  1982.       o->name = attr->string;
  1983.       break;
  1984.     case DW_AT_decl_file:
  1985.       o->file = attr->value;
  1986.       break;
  1987.     case DW_AT_decl_line:
  1988.       o->line = attr->value;
  1989.       break;
  1990.     case DW_AT_type:
  1991.       o->type = elfParseType(unit, attr->value);
  1992.       break;
  1993.     case DW_AT_external:
  1994.       o->external = attr->flag;
  1995.       break;
  1996.     case DW_AT_abstract_origin:
  1997.       elfGetObjectAttributes(unit, attr->value, o);
  1998.       break;
  1999.     case DW_AT_const_value:      
  2000.     case DW_AT_declaration:
  2001.     case DW_AT_artificial:
  2002.       break;
  2003.     case DW_AT_specification:
  2004.       // TODO:
  2005.       break;
  2006.     default:
  2007.       fprintf(stderr, "Unknown object attribute %02x\n", attr->name);
  2008.       break;
  2009.     }
  2010.   }
  2011.   *object = o;
  2012.   return data;
  2013. }
  2014.  
  2015. u8 *elfParseBlock(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit,
  2016.                   Function *func, Object **lastVar)
  2017. {
  2018.   int bytes;
  2019.   u32 start = func->lowPC;
  2020.   u32 end = func->highPC;
  2021.   
  2022.   for(int i = 0; i < abbrev->numAttrs; i++) {
  2023.     ELFAttr *attr = &abbrev->attrs[i];
  2024.     data = elfReadAttribute(data, attr);
  2025.     switch(attr->name) {
  2026.     case DW_AT_sibling:
  2027.       break;
  2028.     case DW_AT_low_pc:
  2029.       start = attr->value;
  2030.       break;
  2031.     case DW_AT_high_pc:
  2032.       end = attr->value;
  2033.       break;
  2034.     case DW_AT_ranges: // ignore for now
  2035.       break;
  2036.     default:
  2037.       fprintf(stderr, "Unknown block attribute %02x\n", attr->name);
  2038.       break;
  2039.     }
  2040.   }
  2041.   
  2042.   if(abbrev->hasChildren) {
  2043.     int nesting = 1;
  2044.  
  2045.     while(nesting) {
  2046.       u32 abbrevNum = elfReadLEB128(data, &bytes);
  2047.       data += bytes;
  2048.       
  2049.       if(!abbrevNum) {
  2050.         nesting--;
  2051.         continue;
  2052.       }
  2053.  
  2054.       abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum);
  2055.       
  2056.       switch(abbrev->tag) {
  2057.       CASE_TYPE_TAG: // types only parsed when used
  2058.       case DW_TAG_label: // not needed
  2059.         data = elfSkipData(data, abbrev, unit->abbrevs);
  2060.         break;  
  2061.       case DW_TAG_lexical_block:
  2062.         data = elfParseBlock(data, abbrev, unit, func, lastVar);
  2063.         break;
  2064.       case DW_TAG_subprogram:
  2065.         {
  2066.           Function *f = NULL;
  2067.           data = elfParseFunction(data, abbrev, unit, &f);
  2068.           if(f != NULL) {
  2069.             if(unit->lastFunction)
  2070.               unit->lastFunction->next = f;
  2071.             else
  2072.               unit->functions = f;
  2073.             unit->lastFunction = f;
  2074.           }
  2075.         }
  2076.         break;
  2077.       case DW_TAG_variable:
  2078.         {
  2079.           Object *o;
  2080.           data = elfParseObject(data, abbrev, unit, &o);
  2081.           if(o->startScope == 0)
  2082.             o->startScope = start;
  2083.           if(o->endScope == 0)
  2084.             o->endScope = 0;
  2085.           if(func->variables)
  2086.             (*lastVar)->next = o;
  2087.           else
  2088.             func->variables = o;
  2089.           *lastVar = o;   
  2090.         }
  2091.         break;
  2092.       case DW_TAG_inlined_subroutine:
  2093.         // TODO:
  2094.         data = elfSkipData(data, abbrev, unit->abbrevs);
  2095.         break;
  2096.       default:
  2097.         {
  2098.           fprintf(stderr, "Unknown block TAG %02x\n", abbrev->tag);
  2099.           data = elfSkipData(data, abbrev, unit->abbrevs);        
  2100.         }
  2101.         break;
  2102.       }
  2103.     }
  2104.   }
  2105.   return data;  
  2106. }
  2107.  
  2108. void elfGetFunctionAttributes(CompileUnit *unit, u32 offset, Function *func)
  2109. {
  2110.   u8 *data = unit->top + offset;
  2111.   int bytes;
  2112.   u32 abbrevNum = elfReadLEB128(data, &bytes);
  2113.   data += bytes;
  2114.   
  2115.   if(!abbrevNum) {
  2116.     return;
  2117.   }
  2118.  
  2119.   ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum);
  2120.  
  2121.   for(int i = 0; i < abbrev->numAttrs; i++) {
  2122.     ELFAttr *attr = &abbrev->attrs[i];
  2123.     data = elfReadAttribute(data, attr);
  2124.     
  2125.     switch(attr->name) {
  2126.     case DW_AT_sibling:
  2127.       break;      
  2128.     case DW_AT_name:
  2129.       if(func->name == NULL)
  2130.         func->name = attr->string;
  2131.       break;
  2132.     case DW_AT_MIPS_linkage_name:
  2133.       func->name = attr->string;
  2134.       break;
  2135.     case DW_AT_low_pc:
  2136.       func->lowPC = attr->value;
  2137.       break;
  2138.     case DW_AT_high_pc:
  2139.       func->highPC = attr->value;
  2140.       break;
  2141.     case DW_AT_decl_file:
  2142.       func->file = attr->value;
  2143.       break;
  2144.     case DW_AT_decl_line:
  2145.       func->line = attr->value;
  2146.       break;
  2147.     case DW_AT_external:
  2148.       func->external = attr->flag;
  2149.       break;
  2150.     case DW_AT_frame_base:
  2151.       func->frameBase = attr->block;
  2152.       break;
  2153.     case DW_AT_type:
  2154.       func->returnType = elfParseType(unit, attr->value);
  2155.       break;
  2156.     case DW_AT_inline:
  2157.     case DW_AT_specification:
  2158.     case DW_AT_declaration:
  2159.     case DW_AT_artificial:
  2160.     case DW_AT_prototyped:
  2161.     case DW_AT_proc_body:
  2162.     case DW_AT_save_offset:
  2163.     case DW_AT_user_2002:
  2164.     case DW_AT_virtuality:
  2165.     case DW_AT_containing_type:
  2166.     case DW_AT_accessibility:
  2167.       // todo;
  2168.       break;
  2169.     case DW_AT_vtable_elem_location:
  2170.       free(attr->block);
  2171.       break;
  2172.     default:
  2173.       fprintf(stderr, "Unknown function attribute %02x\n", attr->name);
  2174.       break;    
  2175.     }
  2176.   }
  2177.  
  2178.   return;
  2179. }
  2180.  
  2181. u8 *elfParseFunction(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit,
  2182.                      Function **f)
  2183. {
  2184.   Function *func = (Function *)calloc(sizeof(Function), 1);
  2185.   *f = func;
  2186.    
  2187.   int bytes;
  2188.   bool mangled = false;
  2189.   bool declaration = false;
  2190.   for(int i = 0; i < abbrev->numAttrs; i++) {
  2191.     ELFAttr *attr = &abbrev->attrs[i];
  2192.     data = elfReadAttribute(data, attr);
  2193.     switch(attr->name) {
  2194.     case DW_AT_sibling:
  2195.       break;
  2196.     case DW_AT_name:
  2197.       if(func->name == NULL)
  2198.         func->name = attr->string;
  2199.       break;
  2200.     case DW_AT_MIPS_linkage_name:
  2201.       func->name = attr->string;
  2202.       mangled = true;
  2203.       break;
  2204.     case DW_AT_low_pc:
  2205.       func->lowPC = attr->value;
  2206.       break;
  2207.     case DW_AT_high_pc:
  2208.       func->highPC = attr->value;
  2209.       break;
  2210.     case DW_AT_prototyped:
  2211.       break;
  2212.     case DW_AT_decl_file:
  2213.       func->file = attr->value;
  2214.       break;
  2215.     case DW_AT_decl_line:
  2216.       func->line = attr->value;
  2217.       break;
  2218.     case DW_AT_external:
  2219.       func->external = attr->flag;
  2220.       break;
  2221.     case DW_AT_frame_base:
  2222.       func->frameBase = attr->block;
  2223.       break;
  2224.     case DW_AT_type:
  2225.       func->returnType = elfParseType(unit, attr->value);
  2226.       break;
  2227.     case DW_AT_abstract_origin:
  2228.       elfGetFunctionAttributes(unit, attr->value, func);
  2229.       break;
  2230.     case DW_AT_declaration:
  2231.       declaration = attr->flag;
  2232.       break;
  2233.     case DW_AT_inline:
  2234.     case DW_AT_specification:
  2235.     case DW_AT_artificial:
  2236.     case DW_AT_proc_body:
  2237.     case DW_AT_save_offset:
  2238.     case DW_AT_user_2002:
  2239.     case DW_AT_virtuality:
  2240.     case DW_AT_containing_type:
  2241.     case DW_AT_accessibility:      
  2242.       // todo;
  2243.       break;
  2244.     case DW_AT_vtable_elem_location:
  2245.       free(attr->block);
  2246.       break;
  2247.     default:
  2248.       fprintf(stderr, "Unknown function attribute %02x\n", attr->name);
  2249.       break;
  2250.     }
  2251.   }
  2252.  
  2253.   if(declaration) {
  2254.     elfCleanUp(func);
  2255.     free(func);
  2256.     *f = NULL;
  2257.  
  2258.     while(1) {
  2259.       u32 abbrevNum = elfReadLEB128(data, &bytes);
  2260.       data += bytes;
  2261.  
  2262.       if(!abbrevNum) {
  2263.         return data;
  2264.       }
  2265.  
  2266.       abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum);
  2267.  
  2268.       data = elfSkipData(data, abbrev, unit->abbrevs);
  2269.     }
  2270.   }
  2271.   
  2272.   if(abbrev->hasChildren) {
  2273.     int nesting = 1;
  2274.     Object *lastParam = NULL;
  2275.     Object *lastVar = NULL;
  2276.  
  2277.     while(nesting) {
  2278.       u32 abbrevNum = elfReadLEB128(data, &bytes);
  2279.       data += bytes;
  2280.       
  2281.       if(!abbrevNum) {
  2282.         nesting--;
  2283.         continue;
  2284.       }
  2285.  
  2286.       abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum);
  2287.       
  2288.       switch(abbrev->tag) {
  2289.       CASE_TYPE_TAG: // no need to parse types. only parsed when used
  2290.       case DW_TAG_label: // not needed
  2291.         data = elfSkipData(data, abbrev, unit->abbrevs);
  2292.         break;
  2293.       case DW_TAG_subprogram:
  2294.         {
  2295.           Function *fnc=NULL;
  2296.           data = elfParseFunction(data, abbrev, unit, &fnc);
  2297.           if(fnc != NULL) {
  2298.             if(unit->lastFunction == NULL)
  2299.               unit->functions = fnc;
  2300.             else
  2301.               unit->lastFunction->next = fnc;
  2302.             unit->lastFunction = fnc;
  2303.           }
  2304.         }
  2305.         break;
  2306.       case DW_TAG_lexical_block:
  2307.         {
  2308.           data = elfParseBlock(data, abbrev, unit, func, &lastVar);
  2309.         }
  2310.         break;
  2311.       case DW_TAG_formal_parameter:
  2312.         {
  2313.           Object *o;
  2314.           data = elfParseObject(data, abbrev, unit, &o);
  2315.           if(func->parameters)
  2316.             lastParam->next = o;
  2317.           else
  2318.             func->parameters = o;
  2319.           lastParam = o;
  2320.         }
  2321.         break;
  2322.       case DW_TAG_variable:
  2323.         {
  2324.           Object *o;
  2325.           data = elfParseObject(data, abbrev, unit, &o);
  2326.           if(func->variables)
  2327.             lastVar->next = o;
  2328.           else
  2329.             func->variables = o;
  2330.           lastVar = o;
  2331.         }
  2332.         break;
  2333.       case DW_TAG_unspecified_parameters:
  2334.       case DW_TAG_inlined_subroutine:
  2335.         {
  2336.           // todo
  2337.           for(int i = 0; i < abbrev->numAttrs; i++) {
  2338.             data = elfReadAttribute(data,  &abbrev->attrs[i]);
  2339.             if(abbrev->attrs[i].form == DW_FORM_block1)
  2340.               free(abbrev->attrs[i].block);
  2341.           }
  2342.           
  2343.           if(abbrev->hasChildren)
  2344.             nesting++;
  2345.         }
  2346.         break;
  2347.       default:
  2348.         {
  2349.           fprintf(stderr, "Unknown function TAG %02x\n", abbrev->tag);
  2350.           data = elfSkipData(data, abbrev, unit->abbrevs);                
  2351.         }
  2352.         break;
  2353.       }
  2354.     }
  2355.   }
  2356.   return data;
  2357. }
  2358.  
  2359. u8 *elfParseUnknownData(u8 *data, ELFAbbrev *abbrev, ELFAbbrev **abbrevs)
  2360. {
  2361.   int i;
  2362.   int bytes;
  2363.   //  switch(abbrev->tag) {
  2364.   //  default:
  2365.     fprintf(stderr, "Unknown TAG %02x\n", abbrev->tag);
  2366.     
  2367.     for(i = 0; i < abbrev->numAttrs; i++) {
  2368.       data = elfReadAttribute(data,  &abbrev->attrs[i]);
  2369.       if(abbrev->attrs[i].form == DW_FORM_block1)
  2370.         free(abbrev->attrs[i].block);
  2371.     }
  2372.     
  2373.     if(abbrev->hasChildren) {
  2374.       int nesting = 1;
  2375.       while(nesting) {
  2376.         u32 abbrevNum = elfReadLEB128(data, &bytes);
  2377.         data += bytes;
  2378.         
  2379.         if(!abbrevNum) {
  2380.           nesting--;
  2381.           continue;
  2382.         }
  2383.         
  2384.         abbrev = elfGetAbbrev(abbrevs, abbrevNum);
  2385.         
  2386.         fprintf(stderr, "Unknown TAG %02x\n", abbrev->tag);
  2387.         
  2388.         for(i = 0; i < abbrev->numAttrs; i++) {
  2389.           data = elfReadAttribute(data,  &abbrev->attrs[i]);
  2390.           if(abbrev->attrs[i].form == DW_FORM_block1)
  2391.             free(abbrev->attrs[i].block);
  2392.         }
  2393.         
  2394.         if(abbrev->hasChildren) {
  2395.           nesting++;
  2396.         }
  2397.       }
  2398.     }
  2399.     //  }
  2400.   return data;
  2401. }
  2402.  
  2403. u8 *elfParseCompileUnitChildren(u8 *data, CompileUnit *unit)
  2404. {
  2405.   int bytes;
  2406.   u32 abbrevNum = elfReadLEB128(data, &bytes);
  2407.   data += bytes;
  2408.   Object *lastObj = NULL;
  2409.   while(abbrevNum) {
  2410.     ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum);
  2411.     switch(abbrev->tag) {
  2412.     case DW_TAG_subprogram:
  2413.       {
  2414.         Function *func = NULL;
  2415.         data = elfParseFunction(data, abbrev, unit, &func);
  2416.         if(func != NULL) {
  2417.           if(unit->lastFunction)
  2418.             unit->lastFunction->next = func;
  2419.           else
  2420.             unit->functions = func;
  2421.           unit->lastFunction = func;
  2422.         }
  2423.       }
  2424.       break;
  2425.     CASE_TYPE_TAG:
  2426.       data = elfSkipData(data, abbrev, unit->abbrevs);
  2427.       break;
  2428.     case DW_TAG_variable:
  2429.       {
  2430.         Object *var = NULL;
  2431.         data = elfParseObject(data, abbrev, unit, &var);
  2432.         if(lastObj)
  2433.           lastObj->next = var;
  2434.         else
  2435.           unit->variables = var;
  2436.         lastObj = var;
  2437.       }
  2438.       break;
  2439.     default:
  2440.       data = elfParseUnknownData(data, abbrev, unit->abbrevs);
  2441.       break;
  2442.     }
  2443.     
  2444.     abbrevNum = elfReadLEB128(data, &bytes);
  2445.     data += bytes;
  2446.   }
  2447.   return data;
  2448. }
  2449.  
  2450.  
  2451. CompileUnit *elfParseCompUnit(u8 *data, u8 *abbrevData)
  2452. {
  2453.   int bytes;
  2454.   u8 *top = data;
  2455.   
  2456.   u32 length = elfRead4Bytes(data);
  2457.   data += 4;
  2458.   
  2459.   u16 version = elfRead2Bytes(data);
  2460.   data += 2;
  2461.   
  2462.   u32 offset = elfRead4Bytes(data);
  2463.   data += 4;
  2464.   
  2465.   u8 addrSize = *data++;
  2466.  
  2467.   if(version != 2) {
  2468.     fprintf(stderr, "Unsupported debugging information version %d\n", version);
  2469.     return NULL;
  2470.   }
  2471.   
  2472.   if(addrSize != 4) {
  2473.     fprintf(stderr, "Unsupported address size %d\n", addrSize);
  2474.     return NULL;
  2475.   }
  2476.  
  2477.   ELFAbbrev **abbrevs = elfReadAbbrevs(abbrevData, offset);
  2478.  
  2479.   u32 abbrevNum = elfReadLEB128(data, &bytes);
  2480.   data += bytes;
  2481.  
  2482.   ELFAbbrev *abbrev = elfGetAbbrev(abbrevs, abbrevNum);
  2483.  
  2484.   CompileUnit *unit = (CompileUnit *)calloc(sizeof(CompileUnit), 1);
  2485.   unit->top = top;
  2486.   unit->length = length;
  2487.   unit->abbrevs = abbrevs;
  2488.   unit->next = NULL;
  2489.  
  2490.   elfCurrentUnit = unit;
  2491.   
  2492.   int i;
  2493.   
  2494.   for(i = 0; i < abbrev->numAttrs; i++) {
  2495.     ELFAttr *attr = &abbrev->attrs[i];
  2496.     data = elfReadAttribute(data, attr);
  2497.  
  2498.     switch(attr->name) {
  2499.     case DW_AT_name:
  2500.       unit->name = attr->string;
  2501.       break;
  2502.     case DW_AT_stmt_list:
  2503.       unit->hasLineInfo = true;
  2504.       unit->lineInfo = attr->value;
  2505.       break;
  2506.     case DW_AT_low_pc:
  2507.       unit->lowPC = attr->value;
  2508.       break;
  2509.     case DW_AT_high_pc:
  2510.       unit->highPC = attr->value;
  2511.       break;
  2512.     case DW_AT_compdir:
  2513.       unit->compdir = attr->string;
  2514.       break;
  2515.       // ignore
  2516.     case DW_AT_language:
  2517.     case DW_AT_producer:
  2518.     case DW_AT_macro_info:
  2519.     case DW_AT_entry_pc:
  2520.       break;
  2521.     default:
  2522.       fprintf(stderr, "Unknown attribute %02x\n", attr->name);
  2523.       break;
  2524.     }
  2525.   }
  2526.  
  2527.   if(abbrev->hasChildren)
  2528.     elfParseCompileUnitChildren(data, unit);
  2529.  
  2530.   return unit;
  2531. }
  2532.  
  2533. void elfParseAranges(u8 *data)
  2534. {
  2535.   ELFSectionHeader *sh = elfGetSectionByName(".debug_aranges");
  2536.   if(sh == NULL) {
  2537.     fprintf(stderr, "No aranges found\n");
  2538.     return;
  2539.   }
  2540.  
  2541.   data = elfReadSection(data, sh);
  2542.   u8 *end = data + READ32LE(&sh->size);
  2543.  
  2544.   int max = 4;
  2545.   ARanges *ranges = (ARanges *)calloc(sizeof(ARanges), 4);
  2546.  
  2547.   int index = 0;
  2548.   
  2549.   while(data < end) {
  2550.     u32 len = elfRead4Bytes(data);
  2551.     data += 4;
  2552.     //    u16 version = elfRead2Bytes(data);
  2553.     data += 2;
  2554.     u32 offset = elfRead4Bytes(data);
  2555.     data += 4;
  2556.     //    u8 addrSize = *data++;
  2557.     //    u8 segSize = *data++;
  2558.     data += 2; // remove if uncommenting above
  2559.     data += 4;
  2560.     ranges[index].count = (len-20)/8;
  2561.     ranges[index].offset = offset;
  2562.     ranges[index].ranges = (ARange *)calloc(sizeof(ARange), (len-20)/8);
  2563.     int i = 0;
  2564.     while(true) {
  2565.       u32 addr = elfRead4Bytes(data);
  2566.       data += 4;
  2567.       u32 len = elfRead4Bytes(data);
  2568.       data += 4;
  2569.       if(addr == 0 && len == 0)
  2570.         break;
  2571.       ranges[index].ranges[i].lowPC = addr;
  2572.       ranges[index].ranges[i].highPC = addr+len;
  2573.       i++;
  2574.     }
  2575.     index++;
  2576.     if(index == max) {
  2577.       max += 4;
  2578.       ranges = (ARanges *)realloc(ranges, max*sizeof(ARanges));
  2579.     }
  2580.   }
  2581.   elfDebugInfo->numRanges = index;
  2582.   elfDebugInfo->ranges = ranges;
  2583. }
  2584.  
  2585. void elfReadSymtab(u8 *data)
  2586. {
  2587.   ELFSectionHeader *sh = elfGetSectionByName(".symtab");
  2588.   int table = READ32LE(&sh->link);
  2589.  
  2590.   char *strtable = (char *)elfReadSection(data, elfGetSectionByNumber(table));
  2591.   
  2592.   ELFSymbol *symtab = (ELFSymbol *)elfReadSection(data, sh);
  2593.  
  2594.   int count = READ32LE(&sh->size) / sizeof(ELFSymbol);
  2595.   elfSymbolsCount = 0;
  2596.  
  2597.   elfSymbols = (Symbol *)malloc(sizeof(Symbol)*count);
  2598.  
  2599.   int i;
  2600.   
  2601.   for(i = 0; i < count; i++) {
  2602.     ELFSymbol *s = &symtab[i];
  2603.     int type = s->info & 15;
  2604.     int binding = s->info >> 4;
  2605.  
  2606.     if(binding) {
  2607.       Symbol *sym = &elfSymbols[elfSymbolsCount];
  2608.       sym->name = &strtable[READ32LE(&s->name)];
  2609.       sym->binding = binding;
  2610.       sym->type = type;
  2611.       sym->value = READ32LE(&s->value);
  2612.       sym->size = READ32LE(&s->size);
  2613.       elfSymbolsCount++;
  2614.     }
  2615.   }
  2616.   for(i = 0; i < count; i++) {
  2617.     ELFSymbol *s = &symtab[i];
  2618.     int bind = s->info>>4;
  2619.     int type = s->info & 15;
  2620.  
  2621.     if(!bind) {
  2622.       Symbol *sym = &elfSymbols[elfSymbolsCount];
  2623.       sym->name = &strtable[READ32LE(&s->name)];
  2624.       sym->binding = (s->info >> 4);
  2625.       sym->type = type;
  2626.       sym->value = READ32LE(&s->value);
  2627.       sym->size = READ32LE(&s->size);
  2628.       elfSymbolsCount++;
  2629.     }
  2630.   }  
  2631.   elfSymbolsStrTab = strtable;
  2632.   //  free(symtab);
  2633. }
  2634.  
  2635. bool elfReadProgram(ELFHeader *eh, u8 *data, int& size, bool parseDebug)
  2636. {
  2637.   int count = READ16LE(&eh->e_phnum);
  2638.   int i;
  2639.   
  2640.   if(READ32LE(&eh->e_entry) == 0x2000000)
  2641.     cpuIsMultiBoot = true;
  2642.  
  2643.   // read program headers... should probably move this code down
  2644.   u8 *p = data + READ32LE(&eh->e_phoff);
  2645.   size = 0;
  2646.   for(i = 0; i < count; i++) {
  2647.     ELFProgramHeader *ph = (ELFProgramHeader *)p;
  2648.     p += sizeof(ELFProgramHeader);
  2649.     if(READ16LE(&eh->e_phentsize) != sizeof(ELFProgramHeader)) {
  2650.       p += READ16LE(&eh->e_phentsize) - sizeof(ELFProgramHeader);
  2651.     }
  2652.  
  2653.     //    printf("PH %d %08x %08x %08x %08x %08x %08x %08x %08x\n",
  2654.     //     i, ph->type, ph->offset, ph->vaddr, ph->paddr,
  2655.     //     ph->filesz, ph->memsz, ph->flags, ph->align);
  2656.     if(cpuIsMultiBoot) {
  2657.       if(READ32LE(&ph->paddr) >= 0x2000000 &&
  2658.          READ32LE(&ph->paddr) <= 0x203ffff) {
  2659.         memcpy(&workRAM[READ32LE(&ph->paddr) & 0x3ffff],
  2660.                data + READ32LE(&ph->offset),
  2661.                READ32LE(&ph->filesz));
  2662.       }      
  2663.     } else {
  2664.       if(READ32LE(&ph->paddr) >= 0x8000000 &&
  2665.          READ32LE(&ph->paddr) <= 0x9ffffff) {
  2666.         memcpy(&rom[READ32LE(&ph->paddr) & 0x1ffffff],
  2667.                data + READ32LE(&ph->offset),
  2668.                READ32LE(&ph->filesz));
  2669.         size += READ32LE(&ph->filesz);
  2670.       }
  2671.     }
  2672.   }
  2673.  
  2674.   char *stringTable = NULL;
  2675.  
  2676.   // read section headers
  2677.   p = data + READ32LE(&eh->e_shoff);
  2678.   count = READ16LE(&eh->e_shnum);
  2679.  
  2680.   ELFSectionHeader **sh = (ELFSectionHeader **)
  2681.     malloc(sizeof(ELFSectionHeader *) * count);
  2682.   
  2683.   for(i = 0; i < count; i++) {
  2684.     sh[i] = (ELFSectionHeader *)p;
  2685.     p += sizeof(ELFSectionHeader);
  2686.     if(READ16LE(&eh->e_shentsize) != sizeof(ELFSectionHeader))
  2687.       p += READ16LE(&eh->e_shentsize) - sizeof(ELFSectionHeader);
  2688.   }
  2689.  
  2690.   if(READ16LE(&eh->e_shstrndx) != 0) {
  2691.     stringTable = (char *)elfReadSection(data,
  2692.                                          sh[READ16LE(&eh->e_shstrndx)]);
  2693.   }
  2694.   
  2695.   elfSectionHeaders = sh;
  2696.   elfSectionHeadersStringTable = stringTable;
  2697.   elfSectionHeadersCount = count;
  2698.  
  2699.   for(i = 0; i < count; i++) {
  2700.     //    printf("SH %d %-20s %08x %08x %08x %08x %08x %08x %08x %08x\n",
  2701.     //   i, &stringTable[sh[i]->name], sh[i]->name, sh[i]->type,
  2702.     //   sh[i]->flags, sh[i]->addr, sh[i]->offset, sh[i]->size,
  2703.     //   sh[i]->link, sh[i]->info);
  2704.     if(READ32LE(&sh[i]->flags) & 2) { // load section
  2705.       if(cpuIsMultiBoot) {
  2706.         if(READ32LE(&sh[i]->addr) >= 0x2000000 &&
  2707.            READ32LE(&sh[i]->addr) <= 0x203ffff) {
  2708.           memcpy(&workRAM[READ32LE(&sh[i]->addr) & 0x3ffff], data +
  2709.                  READ32LE(&sh[i]->offset),
  2710.                  READ32LE(&sh[i]->size));
  2711.         }      
  2712.       } else {
  2713.         if(READ32LE(&sh[i]->addr) >= 0x8000000 &&
  2714.            READ32LE(&sh[i]->addr) <= 0x9ffffff) {
  2715.           memcpy(&rom[READ32LE(&sh[i]->addr) & 0x1ffffff],
  2716.                  data + READ32LE(&sh[i]->offset),
  2717.                  READ32LE(&sh[i]->size));
  2718.           size += READ32LE(&sh[i]->size);
  2719.         }
  2720.       }      
  2721.     }
  2722.   }
  2723.  
  2724.   if(parseDebug) {
  2725.     fprintf(stderr, "Parsing debug info\n");
  2726.  
  2727.     ELFSectionHeader *dbgHeader = elfGetSectionByName(".debug_info");
  2728.     if(dbgHeader == NULL) {
  2729.       fprintf(stderr, "Cannot find debug information\n");
  2730.       goto end;      
  2731.     }
  2732.  
  2733.     ELFSectionHeader *h = elfGetSectionByName(".debug_abbrev");
  2734.     if(h == NULL) {
  2735.       fprintf(stderr, "Cannot find abbreviation table\n");
  2736.       goto end;
  2737.     }
  2738.     
  2739.     elfDebugInfo = (DebugInfo *)calloc(sizeof(DebugInfo), 1);
  2740.     u8 *abbrevdata = elfReadSection(data, h);
  2741.  
  2742.     h = elfGetSectionByName(".debug_str");
  2743.  
  2744.     if(h == NULL)
  2745.       elfDebugStrings = NULL;
  2746.     else
  2747.       elfDebugStrings = (char *)elfReadSection(data, h);
  2748.     
  2749.     u8 *debugdata = elfReadSection(data, dbgHeader);
  2750.     
  2751.     elfDebugInfo->debugdata = data;
  2752.     elfDebugInfo->infodata = debugdata;
  2753.     
  2754.     u32 total = READ32LE(&dbgHeader->size);
  2755.     u8 *end = debugdata + total;
  2756.     u8 *ddata = debugdata;
  2757.     
  2758.     CompileUnit *last = NULL;
  2759.     CompileUnit *unit = NULL;
  2760.     
  2761.     while(ddata < end) {
  2762.       unit = elfParseCompUnit(ddata, abbrevdata);
  2763.       unit->offset = ddata-debugdata;
  2764.       elfParseLineInfo(unit, data);
  2765.       if(last == NULL)
  2766.         elfCompileUnits = unit;
  2767.       else
  2768.         last->next = unit;
  2769.       last = unit;
  2770.       ddata += 4 + unit->length;
  2771.     }
  2772.     elfParseAranges(data);
  2773.     CompileUnit *comp = elfCompileUnits;
  2774.     while(comp) {
  2775.       ARanges *r = elfDebugInfo->ranges;
  2776.       for(int i = 0; i < elfDebugInfo->numRanges; i++)
  2777.         if(r[i].offset == comp->offset) {
  2778.           comp->ranges = &r[i];
  2779.           break;
  2780.         }
  2781.       comp = comp->next;
  2782.     }
  2783.     elfParseCFA(data);
  2784.     elfReadSymtab(data);
  2785.   }
  2786.  end:  
  2787.   if(sh) {
  2788.     free(sh);
  2789.   }
  2790.  
  2791.   elfSectionHeaders = NULL;
  2792.   elfSectionHeadersStringTable = NULL;
  2793.   elfSectionHeadersCount = 0;
  2794.   
  2795.   return true;
  2796. }
  2797.  
  2798. extern bool parseDebug;
  2799.  
  2800. bool elfRead(const char *name, int& siz, FILE *f)
  2801. {
  2802.   fseek(f, 0, SEEK_END);
  2803.   long size = ftell(f);
  2804.   elfFileData = (u8 *)malloc(size);
  2805.   fseek(f, 0, SEEK_SET);
  2806.   fread(elfFileData, 1, size, f);
  2807.   fclose(f);
  2808.   
  2809.   ELFHeader *header = (ELFHeader *)elfFileData;
  2810.   
  2811.   if(READ32LE(&header->magic) != 0x464C457F ||
  2812.      READ16LE(&header->e_machine) != 40 ||
  2813.      header->clazz != 1) {
  2814.     systemMessage(0, N_("Not a valid ELF file %s"), name);
  2815.     free(elfFileData);
  2816.     elfFileData = NULL;
  2817.     return false;
  2818.   }
  2819.  
  2820.   if(!elfReadProgram(header, elfFileData, siz, parseDebug)) {
  2821.     free(elfFileData);
  2822.     elfFileData = NULL;
  2823.     return false;
  2824.   }
  2825.   
  2826.   return true;
  2827. }
  2828.  
  2829. void elfCleanUp(Object *o)
  2830. {
  2831.   free(o->location);
  2832. }
  2833.  
  2834. void elfCleanUp(Function *func)
  2835. {
  2836.   Object *o = func->parameters;
  2837.   while(o) {
  2838.     elfCleanUp(o);
  2839.     Object *next = o->next;
  2840.     free(o);
  2841.     o = next;
  2842.   }
  2843.  
  2844.   o = func->variables;
  2845.   while(o) {
  2846.     elfCleanUp(o);
  2847.     Object *next = o->next;
  2848.     free(o);
  2849.     o = next;
  2850.   }
  2851.   free(func->frameBase);
  2852. }
  2853.  
  2854. void elfCleanUp(ELFAbbrev **abbrevs)
  2855. {
  2856.   for(int i = 0; i < 121; i++) {
  2857.     ELFAbbrev *abbrev = abbrevs[i];
  2858.  
  2859.     while(abbrev) {
  2860.       free(abbrev->attrs);
  2861.       ELFAbbrev *next = abbrev->next;
  2862.       free(abbrev);
  2863.       
  2864.       abbrev = next;
  2865.     }
  2866.   }
  2867. }
  2868.  
  2869. void elfCleanUp(Type *t)
  2870. {
  2871.   switch(t->type) {
  2872.   case TYPE_function:
  2873.     if(t->function) {
  2874.       Object *o = t->function->args;
  2875.       while(o) {
  2876.         elfCleanUp(o);
  2877.         Object *next = o->next;
  2878.         free(o);
  2879.         o = next;
  2880.       }
  2881.       free(t->function);      
  2882.     }
  2883.     break;
  2884.   case TYPE_array:
  2885.     if(t->array) {
  2886.       free(t->array->bounds);
  2887.       free(t->array);
  2888.     }
  2889.     break;
  2890.   case TYPE_struct:
  2891.   case TYPE_union:
  2892.     if(t->structure) {
  2893.       for(int i = 0; i < t->structure->memberCount; i++) {
  2894.         free(t->structure->members[i].location);
  2895.       }
  2896.       free(t->structure->members);
  2897.       free(t->structure);
  2898.     }
  2899.     break;
  2900.   case TYPE_enum:
  2901.     if(t->enumeration) {
  2902.       free(t->enumeration->members);
  2903.       free(t->enumeration);
  2904.     }
  2905.     break;
  2906.   case TYPE_base:
  2907.   case TYPE_pointer:
  2908.   case TYPE_void:
  2909.   case TYPE_reference:
  2910.     break; // nothing to do
  2911.   }
  2912. }
  2913.  
  2914. void elfCleanUp(CompileUnit *comp)
  2915. {
  2916.   elfCleanUp(comp->abbrevs);
  2917.   free(comp->abbrevs);
  2918.   Function *func = comp->functions;
  2919.   while(func) {
  2920.     elfCleanUp(func);
  2921.     Function *next = func->next;
  2922.     free(func);
  2923.     func = next;
  2924.   }
  2925.   Type *t = comp->types;
  2926.   while(t) {
  2927.     elfCleanUp(t);
  2928.     Type *next = t->next;
  2929.     free(t);
  2930.     t = next;
  2931.   }
  2932.   Object *o = comp->variables;
  2933.   while(o) {
  2934.     elfCleanUp(o);
  2935.     Object *next = o->next;
  2936.     free(o);
  2937.     o = next;
  2938.   }
  2939.   if(comp->lineInfoTable) {
  2940.     free(comp->lineInfoTable->lines);
  2941.     free(comp->lineInfoTable->files);
  2942.     free(comp->lineInfoTable);
  2943.   }
  2944. }
  2945.  
  2946. void elfCleanUp()
  2947. {
  2948.   CompileUnit *comp = elfCompileUnits;
  2949.  
  2950.   while(comp) {
  2951.     elfCleanUp(comp);
  2952.     CompileUnit *next = comp->next;
  2953.     free(comp);
  2954.     comp = next;
  2955.   }
  2956.   elfCompileUnits = NULL;
  2957.   free(elfSymbols);
  2958.   elfSymbols = NULL;
  2959.   //  free(elfSymbolsStrTab);
  2960.   elfSymbolsStrTab = NULL;
  2961.  
  2962.   elfDebugStrings = NULL;
  2963.   if(elfDebugInfo) {
  2964.     int num = elfDebugInfo->numRanges;
  2965.     int i;
  2966.     for(i = 0; i < num; i++) {
  2967.       free(elfDebugInfo->ranges[i].ranges);
  2968.     }
  2969.     free(elfDebugInfo->ranges);
  2970.     free(elfDebugInfo);
  2971.     elfDebugInfo = NULL;
  2972.   }
  2973.  
  2974.   if(elfFdes) {
  2975.     if(elfFdeCount) {
  2976.       for(int i = 0; i < elfFdeCount; i++)
  2977.         free(elfFdes[i]);
  2978.     }
  2979.     free(elfFdes);
  2980.     
  2981.     elfFdes = NULL;
  2982.     elfFdeCount = 0;
  2983.   }
  2984.  
  2985.   ELFcie *cie = elfCies;
  2986.   while(cie) {
  2987.     ELFcie *next = cie->next;
  2988.     free(cie);
  2989.     cie = next;
  2990.   }
  2991.   elfCies = NULL;
  2992.  
  2993.   if(elfFileData) {
  2994.     free(elfFileData);
  2995.     elfFileData = NULL;
  2996.   }
  2997. }
  2998.