Category: Kernel

Start bochs with a fixed window location

Bochs doesn’t has a parameter to specific the startup window position on screen. If you are compiling boots with SDL2. You can edit boots_source/gui/sdl2.cc, and change the first SDL_WINDOWPOS_UNDEFINED to x position and second SDL_WINDOWPOS_UNDEFINED to y position.

  window = SDL_CreateWindow(
    BOCHS_WINDOW_NAME,
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    640,
    480,
    SDL_WINDOW_SHOWN
    );

read count : 4

bochs breakpoint panel

extended netbeans, auto scan .c/.cc/.cpp files to find my bochs breakpoint marco, double click it will jump to there. Small tool, but save me a lot of time.pause_bochs-panel

read count : 19

my crazy kmalloc bug

i was assume the continous linear addresses are in same 2nd level page table, so i was in shit for few months. The probabilities of this bug is 1/4096, hard to discover. kmalloc-bug

read count : 18

dwarf is a too old and too inaccurate protocol to representing source code

dwarf is a too old and too inaccurate protocol to representing source code, i compile the following c code by gcc 5.2.0 and linked it by binutils 2.25

int testVarFunc() {
	int var1 = 1980;
	for (int y = 0; y < var1; y++) {
		int var2 = var1 + y*y;
	}
	int var4 = 2204;
	for (int y2 = 0; y2 < var4; y2++) {
		int var3 = var1 + y2*y2;
	}
	int var5 = 1234;
	var5 = var1*var4;
	return var5;
}

Then i use objdump 2.25 to disassemble it, the c source code line information is too in accurate, and i didn’t use turn on any gcc optimization. As you can see the below disassembly, the testVarFunc() appeared twice.

int testVarFunc() {
 1602903:       55                      push   %ebp
 1602904:       89 e5                   mov    %esp,%ebp
 1602906:       83 ec 20                sub    $0x20,%esp
        int var1 = 1980;
 1602909:       c7 45 f4 bc 07 00 00    movl   $0x7bc,-0xc(%ebp)
        for (int y = 0; y < var1; y++) {
 1602910:       c7 45 fc 00 00 00 00    movl   $0x0,-0x4(%ebp)
 1602917:       8b 45 fc                mov    -0x4(%ebp),%eax
 160291a:       3b 45 f4                cmp    -0xc(%ebp),%eax
 160291d:       7d 16                   jge    1602935 <_Z11testVarFuncv+0x32>
                int var2 = var1 + y*y;
 160291f:       8b 45 fc                mov    -0x4(%ebp),%eax
 1602922:       0f af 45 fc             imul   -0x4(%ebp),%eax
 1602926:       89 c2                   mov    %eax,%edx
 1602928:       8b 45 f4                mov    -0xc(%ebp),%eax
 160292b:       01 d0                   add    %edx,%eax
 160292d:       89 45 f0                mov    %eax,-0x10(%ebp)
        }
}

int testVarFunc() {
        int var1 = 1980;
        for (int y = 0; y < var1; y++) {
 1602930:       ff 45 fc                incl   -0x4(%ebp)
 1602933:       eb e2                   jmp    1602917 <_Z11testVarFuncv+0x14>
                int var2 = var1 + y*y;
        }
        int var4 = 2204;
 1602935:       c7 45 ec 9c 08 00 00    movl   $0x89c,-0x14(%ebp)
        for (int y2 = 0; y2 < var4; y2++) {
 160293c:       c7 45 f8 00 00 00 00    movl   $0x0,-0x8(%ebp)
 1602943:       8b 45 f8                mov    -0x8(%ebp),%eax
 1602946:       3b 45 ec                cmp    -0x14(%ebp),%eax
 1602949:       7d 16                   jge    1602961 <_Z11testVarFuncv+0x5e>
                int var3 = var1 + y2*y2;
 160294b:       8b 45 f8                mov    -0x8(%ebp),%eax
 160294e:       0f af 45 f8             imul   -0x8(%ebp),%eax
 1602952:       89 c2                   mov    %eax,%edx
 1602954:       8b 45 f4                mov    -0xc(%ebp),%eax
 1602957:       01 d0                   add    %edx,%eax
 1602959:       89 45 e8                mov    %eax,-0x18(%ebp)
        int var1 = 1980;
        for (int y = 0; y < var1; y++) {
                int var2 = var1 + y*y;
        }
        int var4 = 2204;
        for (int y2 = 0; y2 < var4; y2++) {
 160295c:       ff 45 f8                incl   -0x8(%ebp)
160295f:       eb e2                   jmp    1602943 <_Z11testVarFuncv+0x40>
                int var3 = var1 + y2*y2;
        }
        int var5 = 1234;
 1602961:       c7 45 e4 d2 04 00 00    movl   $0x4d2,-0x1c(%ebp)
        var5 = var1*var4;
 1602968:       8b 45 f4                mov    -0xc(%ebp),%eax
 160296b:       0f af 45 ec             imul   -0x14(%ebp),%eax
 160296f:       89 45 e4                mov    %eax,-0x1c(%ebp)
        return var5;
 1602972:       8b 45 e4                mov    -0x1c(%ebp),%eax
}
 1602975:       c9                      leave  
 1602976:       c3                      ret    

read count : 22

a super skill to help you understand .init and .fini

a super skill to help you understand .init and .fini, just do “LD_DEBUG=libs ./a.out”, read detail http://www.bnikolic.co.uk/blog/linux-ld-debug.html

One thing need to remind you, loader pass control to a.out *BEFORE* it calls the fini.

/root>LD_DEBUG=libs ./a.out
      8044:	find library=libstdc++.so.6 [0]; searching
      8044:	 search cache=/etc/ld.so.cache
      8044:	  trying file=/usr/lib/x86_64-linux-gnu/libstdc++.so.6
      8044:	
      8044:	find library=libgcc_s.so.1 [0]; searching
      8044:	 search cache=/etc/ld.so.cache
      8044:	  trying file=/lib/x86_64-linux-gnu/libgcc_s.so.1
      8044:	
      8044:	find library=libc.so.6 [0]; searching
      8044:	 search cache=/etc/ld.so.cache
      8044:	  trying file=/lib/x86_64-linux-gnu/libc.so.6
      8044:	
      8044:	find library=libm.so.6 [0]; searching
      8044:	 search cache=/etc/ld.so.cache
      8044:	  trying file=/lib/x86_64-linux-gnu/libm.so.6
      8044:	
      8044:	
      8044:	calling init: /lib/x86_64-linux-gnu/libc.so.6
      8044:	
      8044:	
      8044:	calling init: /lib/x86_64-linux-gnu/libm.so.6
      8044:	
      8044:	
      8044:	calling init: /lib/x86_64-linux-gnu/libgcc_s.so.1
      8044:	
      8044:	
      8044:	calling init: /usr/lib/x86_64-linux-gnu/libstdc++.so.6
      8044:	
      8044:	
      8044:	initialize program: ./a.out
      8044:	
      8044:	
      8044:	transferring control: ./a.out
      8044:	
4660
      8044:	
      8044:	calling fini: ./a.out [0]
      8044:	
      8044:	
      8044:	calling fini: /usr/lib/x86_64-linux-gnu/libstdc++.so.6 [0]
      8044:	
      8044:	
      8044:	calling fini: /lib/x86_64-linux-gnu/libgcc_s.so.1 [0]
      8044:	
      8044:	
      8044:	calling fini: /lib/x86_64-linux-gnu/libm.so.6 [0]
      8044:	

read count : 31

discovered something about .init and .fini sections

discovered something about .init and .fini sections, if you compile your source file into .o, no .init and .fini sections exist. But if you compile your code into target exe, .init and .fini are there. My question is: where does those .init and .fini come from the .o?

read count : 19