Subversion Repositories Kolibri OS

Rev

Rev 4888 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;;;;;;;;;;;;;;;;;;;;;;;; Working with program symbols ;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4.  
  5. include 'sort.inc'
  6.  
  7. ; compare proc for sorter
  8. compare:
  9.         cmpsd
  10.         jnz     @f
  11.         cmp     esi, edi
  12.  
  13.   @@:
  14.         ret
  15.  
  16. ; compare proc for sorter 2
  17. compare2:
  18.         cmpsd
  19.  
  20.   @@:
  21.         cmpsb
  22.         jnz     @f
  23.         cmp     byte [esi-1], 0
  24.         jnz     @b
  25.         cmp     esi, edi
  26.  
  27.   @@:
  28.         ret
  29.  
  30. free_symbols:
  31.         mov     ecx, [symbols]
  32.         jecxz   @f
  33.         mcall   68, 13
  34.         and     [symbols], 0
  35.         and     [num_symbols], 0
  36.  
  37.   @@:
  38.         ret
  39.  
  40.  
  41. ;-----------------------------------------------------------------------------
  42. ;                        Load symbols event
  43.  
  44. OnLoadSymbols.fileerr:
  45.         test    ebp, ebp
  46.         jz      @f
  47.         mcall   68, 13, edi
  48.         ret
  49.  
  50.   @@:
  51.         push    eax
  52.         mcall   68, 13, edi
  53.         mov     esi, aCannotLoadFile
  54.         call    put_message_nodraw
  55.         pop     eax
  56.         cmp     eax, 0x20
  57.         jae     .unk
  58.         mov     esi, [load_err_msgs + eax*4]
  59.         test    esi, esi
  60.         jnz     put_message
  61.  
  62.   .unk:
  63.         mov     esi, unk_err_msg2
  64.         jmp     put_message
  65.  
  66.  
  67. OnLoadSymbols:
  68.         xor     ebp, ebp
  69. ; load input file
  70.         mov     esi, [curarg]
  71.         call    free_symbols
  72.  
  73.   .silent:
  74. ; esi = ptr to symbols filename
  75.         xor     edi, edi
  76.         cmp     [num_symbols], edi              ; Any previously loaded symbols?
  77.         je      .loadfile
  78.         call    free_symbols                    ; Yep, free them
  79.  
  80.   .loadfile:
  81.         mov     ebx, fn70_attr_block            ; Get file attributes
  82.         mov     [ebx+21], esi
  83.         mcall   70
  84.         test    eax, eax
  85.         jnz     .fileerr
  86.         cmp     dword [fileattr+36], edi        ; Is upper dword of filesize larger then 0?
  87.         jnz     .memerr
  88.         mov     ecx, dword [fileattr+32]        ; Lower dword of filesize
  89.         mcall   68, 12                          ; allocate the memory
  90.         test    eax, eax
  91.         jz      .memerr
  92.         mov     edi, eax
  93.         mov     ebx, fn70_read_block
  94.         mov     [ebx+12], ecx
  95.         mov     [ebx+16], edi
  96.         mov     [ebx+21], esi
  97.         mcall   70                              ; Read the file into the allocated buffer
  98.         test    eax, eax
  99.         jnz     .fileerr
  100.  
  101.  
  102. ; calculate memory requirements to load debug symbols
  103.  
  104.         lea     edx, [ecx+edi-1]                ; edx = EOF-1
  105.         mov     esi, edi
  106.         xor     ecx, ecx
  107.  
  108.         mov     [symbol_section], 1    ;;;;;
  109.  
  110.   .calcloop:
  111.         cmp     esi, edx
  112.         jae     .calcdone
  113.         cmp     byte[esi], ' '          ; skip spaces at the beginning of a line
  114.         jne     .not_space
  115.         inc     esi
  116.         jmp     .calcloop
  117.   .not_space:
  118.         cmp     byte[esi], '.'
  119.         jne     .not_section
  120.         inc     esi
  121.         mov     [symbol_section], 0
  122.         cmp     dword[esi], 'text'
  123.         je      .section_ok
  124.         cmp     dword[esi], 'data'
  125.         je      .section_ok
  126.         cmp     dword[esi], 'bss '
  127.         jne     .skipline
  128.   .section_ok:
  129.         inc     [symbol_section]
  130.         jmp     .skipline
  131.   .not_section:
  132.         cmp     [symbol_section], 0
  133.         je      .skipline
  134.  
  135.         cmp     word[esi], '0x'
  136.         jne     .skipline
  137.         inc     esi
  138.         inc     esi
  139.  
  140.   @@:
  141.         cmp     esi, edx
  142.         jae     .calcdone
  143.         lodsb
  144.         or      al, 20h
  145.         sub     al, '0'
  146.         cmp     al, 9
  147.         jbe     @b
  148.         sub     al, 'a'-'0'-10
  149.         cmp     al, 15
  150.         jbe     @b
  151.         dec     esi
  152.  
  153.   @@:
  154.         cmp     esi, edx
  155.         ja      .calcdone
  156.         lodsb
  157.         cmp     al, 20h
  158.         je      @b
  159.         jb      .calcloop
  160.         cmp     al, 9
  161.         jz      @b
  162.         add     ecx, 12+1
  163.         inc     [num_symbols]
  164.  
  165.   @@:
  166.         inc     ecx
  167.         cmp     esi, edx
  168.         ja      .calcdone
  169.         lodsb
  170.         cmp     al, 0xD
  171.         jz      .calcloop
  172.         cmp     al, 0xA
  173.         jz      .calcloop
  174.         jmp     @b
  175.  
  176.   .skipline:
  177.         cmp     esi, edx
  178.         jae     .calcdone
  179.         lodsb
  180.         cmp     al, 0xD
  181.         jz      .calcloop
  182.         cmp     al, 0xA
  183.         jz      .calcloop
  184.         jmp     .skipline
  185.  
  186.   .calcdone:
  187.  
  188. ; Allocate memory to place the debug symbols in
  189.  
  190.         mcall   68, 12
  191.         test    eax, eax
  192.         jnz     .memok
  193.         inc     ebx
  194.         mov     ecx, edi
  195.         mov     al, 68
  196.         mcall
  197.  
  198.   .memerr:
  199.         mov     esi, aNoMemory
  200.         jmp     put_message
  201.  
  202.   .memok:
  203.         mov     [symbols], eax
  204.         mov     ebx, eax
  205.         push    edi
  206.         mov     esi, edi
  207.         mov     edi, [num_symbols]
  208.         lea     ebp, [eax+edi*4]
  209.         lea     edi, [eax+edi*8]
  210.  
  211. ; Actual loading of the debug symbols
  212. ; esi->input, edx->EOF, ebx->ptrs, edi->names
  213.  
  214.         mov     [symbol_section], 1    ;;;;;
  215.  
  216.   .readloop:
  217.         cmp     esi, edx
  218.         jae     .readdone
  219.         cmp     byte[esi], ' '
  220.         jne     .not_space2
  221.         inc     esi
  222.         jmp     .readloop
  223.   .not_space2:
  224.         cmp     byte[esi], '.'
  225.         jne     .not_section2
  226.         inc     esi
  227.         mov     [symbol_section], 0
  228.         cmp     dword[esi], 'text'
  229.         je      .section_ok2
  230.         cmp     dword[esi], 'data'
  231.         je      .section_ok2
  232.         cmp     dword[esi], 'bss '
  233.         jne     .readline
  234.   .section_ok2:
  235.         inc     [symbol_section]
  236.         jmp     .readline
  237.   .not_section2:
  238.         cmp     [symbol_section], 0
  239.         je      .readline
  240.  
  241.         cmp     word[esi], '0x'
  242.         jnz     .readline
  243.         inc     esi
  244.         inc     esi
  245.         xor     eax, eax
  246.         xor     ecx, ecx
  247.  
  248.   @@:
  249.         shl     ecx, 4
  250.         add     ecx, eax
  251.         cmp     esi, edx
  252.         jae     .readdone
  253.         lodsb
  254.         or      al, 20h
  255.         sub     al, '0'
  256.         cmp     al, 9
  257.         jbe     @b
  258.         sub     al, 'a'-'0'-10
  259.         cmp     al, 15
  260.         jbe     @b
  261.         dec     esi
  262.  
  263.   @@:
  264.         cmp     esi, edx
  265.         ja      .readdone
  266.         lodsb
  267.         cmp     al, 20h
  268.         jz      @b
  269.         jb      .readloop
  270.         cmp     al, 9
  271.         jz      @b
  272.         mov     dword [ebx], edi
  273.         add     ebx, 4
  274.         mov     dword [ebp], edi
  275.         add     ebp, 4
  276.         mov     dword [edi], ecx
  277.         add     edi, 4
  278.         stosb
  279.  
  280.   @@:
  281.         xor     eax, eax
  282.         stosb
  283.         cmp     esi, edx
  284.         ja      .readdone
  285.         lodsb
  286.         cmp     al, 0xD
  287.         jz      .readloop
  288.         cmp     al, 0xA
  289.         jz      .readloop
  290.         mov     byte [edi-1], al
  291.         jmp     @b
  292.  
  293.   .readline:
  294.         cmp     esi, edx
  295.         jae     .readdone
  296.         lodsb
  297.         cmp     al, 0xD
  298.         jz      .readloop
  299.         cmp     al, 0xA
  300.         jz      .readloop
  301.         jmp     .readline
  302.  
  303.   .readdone:
  304.         pop     ecx
  305.         mcall   68, 13
  306.         mov     ecx, [num_symbols]
  307.         mov     edx, [symbols]
  308.         mov     ebx, compare
  309.         call    sort
  310.         mov     ecx, [num_symbols]
  311.         lea     edx, [edx+ecx*4]
  312.         mov     ebx, compare2
  313.         call    sort
  314.         mov     esi, aSymbolsLoaded
  315.         call    put_message
  316.         jmp     draw_disasm
  317.  
  318. ;-----------------------------------------------------------------------------
  319. ;
  320. ; in: EAX = address
  321. ; out: ESI, CF
  322.  
  323. find_symbol:
  324.         cmp     [num_symbols], 0
  325.         jnz     @f
  326.  
  327.   .ret0:
  328.         xor     esi, esi
  329.         stc
  330.         ret
  331.  
  332.   @@:
  333.         push    ebx ecx edx
  334.         xor     edx, edx
  335.         mov     esi, [symbols]
  336.         mov     ecx, [num_symbols]
  337.         mov     ebx, [esi]
  338.         cmp     [ebx], eax
  339.         jz      .donez
  340.         jb      @f
  341.         pop     edx ecx ebx
  342.         jmp     .ret0
  343.  
  344.   @@:
  345.     ; invariant: symbols_addr[edx] < eax < symbols_addr[ecx]
  346.     ; TODO: add meaningful label names
  347.   .0:
  348.         push    edx
  349.  
  350.   .1:
  351.         add     edx, ecx
  352.         sar     edx, 1
  353.         cmp     edx, [esp]
  354.         jz      .done2
  355.         mov     ebx, [esi+edx*4]
  356.         cmp     [ebx], eax
  357.         jz      .done
  358.         ja      .2
  359.         mov     [esp], edx
  360.         jmp     .1
  361.  
  362.   .2:
  363.         mov     ecx, edx
  364.         pop     edx
  365.         jmp     .0
  366.  
  367.   .donecont:
  368.         dec     edx
  369.  
  370.   .done:
  371.         test    edx, edx
  372.         jz      @f
  373.         mov     ebx, [esi+edx*4-4]
  374.         cmp     [ebx], eax
  375.         jz      .donecont
  376.  
  377.   @@:
  378.         pop     ecx
  379.  
  380.   .donez:
  381.         mov     esi, [esi+edx*4]
  382.         add     esi, 4
  383.         pop     edx ecx ebx
  384.         clc
  385.         ret
  386.  
  387.   .done2:
  388.         lea     esi, [esi+edx*4]
  389.         pop     ecx edx ecx ebx
  390.         stc
  391.         ret
  392.  
  393. ;-----------------------------------------------------------------------------
  394. ;
  395. ; in: esi->name
  396. ; out: if found: CF = 0, EAX = value
  397. ;      otherwise CF = 1
  398. find_symbol_name:
  399.         cmp     [num_symbols], 0
  400.         jnz     @f
  401.  
  402.   .stc_ret:
  403.         stc
  404.         ret
  405.  
  406.   @@:
  407.         push    ebx ecx edx edi
  408.         push    -1
  409.         pop     edx
  410.         mov     ebx, [symbols]
  411.         mov     ecx, [num_symbols]
  412.         lea     ebx, [ebx+ecx*4]
  413.    
  414.     ; invariant: symbols_name[edx] < name < symbols_name[ecx]
  415.   .0:
  416.         push    edx
  417.  
  418.   .1:
  419.         add     edx, ecx
  420.         sar     edx, 1
  421.         cmp     edx, [esp]
  422.         jz      .done2
  423.         call    .cmp
  424.         jz      .done
  425.         jb      .2
  426.         mov     [esp], edx
  427.         jmp     .1
  428.  
  429.   .2:
  430.         mov     ecx, edx
  431.         pop     edx
  432.         jmp     .0
  433.  
  434.   .done:
  435.         pop     ecx
  436.  
  437.   .donez:
  438.         mov     eax, [ebx+edx*4]
  439.         mov     eax, [eax]
  440.         pop     edi edx ecx ebx
  441.         clc
  442.         ret
  443.  
  444.   .done2:
  445.         pop     edx edi edx ecx ebx
  446.         stc
  447.         ret
  448.  
  449.   .cmp:
  450.         mov     edi, [ebx+edx*4]
  451.         push    esi
  452.         add     edi, 4
  453.  
  454.   @@:
  455.         cmpsb
  456.         jnz     @f
  457.         cmp     byte [esi-1], 0
  458.         jnz     @b
  459.  
  460.   @@:
  461.         pop     esi
  462.         ret