Page 2 of 2

Re: Problem with strings

Posted: Tue Sep 04, 2012 3:01 pm
by Khronos
What is the difference between *(.rodata) and *(.rodata*)?

Code: Select all

khronos@CASA:~/Tanis Kernel$ objdump -s kernel.o

kernel.o:     file format elf64-x86-64

Contents of section .text:
 0000 4883ec28 89fa89f0 66895424 0c668944  H..(....f.T$.f.D
 0010 2408c744 24104865 6c6cc744 24146f21  $..D$.Hell.D$.o!
 0020 0a00e800 000000e8 00000000 bf000000  ................
 0030 00b80000 0000e800 00000048 8d442410  ...........H.D$.
 0040 4889c7b8 00000000 e8000000 00488b05  H............H..
 0050 00000000 4889c7e8 00000000 e8000000  ....H...........
 0060 00bf0000 0000b800 000000e8 00000000  ................
 0070 ebfe                                 ..              
Contents of section .data:
 0000 00000000 00000000 00000000 00000000  ................
 0010 00000000 00000000 00000000 00000000  ................
 0020 00000000 00000000 00000000 00000000  ................
 0030 00000000 00000000 00000000 00000000  ................
 0040 00000000 00000000 00000000 00000000  ................
 0050 00000000 00000000 00000000 00000000  ................
 0060 00000000 00000000 00000000 00000000  ................
 0070 00000000 00000000 00000000 00000000  ................
 0080 00000000 00000000 00000000 00000000  ................
 0090 00000000 00000000 00000000 00000000  ................
 00a0 00000000 00000000 00000000 00000000  ................
 00b0 00000000 00000000 00000000 00000000  ................
 00c0 00000000 00000000 00000000 00000000  ................
 00d0 00000000 00000000 00000000 00000000  ................
 00e0 00000000 00000000 00000000 00000000  ................
 00f0 00000000 00000000                    ........        
Contents of section .rodata:
 0000 30202d20 44697669 73696f6e 20627920  0 - Division by 
 0010 7a65726f 20657863 65707469 6f6e0000  zero exception..
 0020 31202d20 44656275 67206578 63657074  1 - Debug except
 0030 696f6e00 0032202d 204e6f6e 206d6173  ion..2 - Non mas
 0040 6b61626c 6520696e 74657272 75707400  kable interrupt.
 0050 0033202d 20427265 616b706f 696e7420  .3 - Breakpoint 
 0060 65786365 7074696f 6e000034 202d2027  exception..4 - '
 0070 496e746f 20646574 65637465 64206f76  Into detected ov
 0080 6572666c 6f770000 35202d20 4f757420  erflow..5 - Out 
 0090 6f662062 6f756e64 73206578 63657074  of bounds except
 00a0 696f6e00 0036202d 20496e76 616c6964  ion..6 - Invalid
 00b0 206f7063 6f646520 65786365 7074696f   opcode exceptio
 00c0 6e000037 202d204e 6f20636f 70726f63  n..7 - No coproc
 00d0 6573736f 72206578 63657074 696f6e00  essor exception.
 00e0 0038202d 20446f75 626c6520 6661756c  .8 - Double faul
 00f0 74000000 00000000 39202d20 436f7072  t.......9 - Copr
 0100 6f636573 736f7220 7365676d 656e7420  ocessor segment 
 0110 6f766572 72756e00 00313020 2d204261  overrun..10 - Ba
 0120 64205453 53000031 31202d20 5365676d  d TSS..11 - Segm
 0130 656e7420 6e6f7420 70726573 656e7400  ent not present.
 0140 00313220 2d205374 61636b20 6661756c  .12 - Stack faul
 0150 74000000 00000000 3133202d 2047656e  t.......13 - Gen
 0160 6572616c 2070726f 74656374 696f6e20  eral protection 
 0170 6661756c 74000031 34202d20 50616765  fault..14 - Page
 0180 20666175 6c740000 3135202d 20556e6b   fault..15 - Unk
 0190 6e6f776e 20696e74 65727275 70742065  nown interrupt e
 01a0 78636570 74696f6e 00003136 202d2043  xception..16 - C
 01b0 6f70726f 63657373 6f722066 61756c74  oprocessor fault
 01c0 00000000 00000000 3137202d 20416c69  ........17 - Ali
 01d0 676e6d65 6e742063 6865636b 20657863  gnment check exc
 01e0 65707469 6f6e0000 3138202d 204d6163  eption..18 - Mac
 01f0 68696e65 20636865 636b2065 78636570  hine check excep
 0200 74696f6e 00003139 202d2052 65736572  tion..19 - Reser
 0210 76656420 65786365 7074696f 6e000032  ved exception..2
 0220 30202d20 52657365 72766564 20657863  0 - Reserved exc
 0230 65707469 6f6e0000 3231202d 20526573  eption..21 - Res
 0240 65727665 64206578 63657074 696f6e00  erved exception.
 0250 00323220 2d205265 73657276 65642065  .22 - Reserved e
 0260 78636570 74696f6e 00003233 202d2052  xception..23 - R
 0270 65736572 76656420 65786365 7074696f  eserved exceptio
 0280 6e000032 34202d20 52657365 72766564  n..24 - Reserved
 0290 20657863 65707469 6f6e0000 3235202d   exception..25 -
 02a0 20526573 65727665 64206578 63657074   Reserved except
 02b0 696f6e00 00323620 2d205265 73657276  ion..26 - Reserv
 02c0 65642065 78636570 74696f6e 00003237  ed exception..27
 02d0 202d2052 65736572 76656420 65786365   - Reserved exce
 02e0 7074696f 6e000032 38202d20 52657365  ption..28 - Rese
 02f0 72766564 20657863 65707469 6f6e0000  rved exception..
 0300 3239202d 20526573 65727665 64206578  29 - Reserved ex
 0310 63657074 696f6e00 00000000 00000000  ception.........
 0320 3330202d 20526573 65727665 64206578  30 - Reserved ex
 0330 63657074 696f6e00 3331202d 20526573  ception.31 - Res
 0340 65727665 64206578 63657074 696f6e00  erved exception.
 0350 00416469 6f730a00 496e6963 69616e64  .Adios..Iniciand
 0360 6f204944 542e2e2e 0a00               o IDT.....      
Contents of section .comment:
 0000 00474343 3a202855 62756e74 752f4c69  .GCC: (Ubuntu/Li
 0010 6e61726f 20342e36 2e332d31 7562756e  naro 4.6.3-1ubun
 0020 74753529 20342e36 2e3300             tu5) 4.6.3.     
Contents of section .eh_frame:
 0000 14000000 00000000 017a5200 01781001  .........zR..x..
 0010 1b0c0708 90010000 14000000 1c000000  ................
 0020 00000000 72000000 00440e30 00000000  ....r....D.0....
Then, I understand that the sections I need are: .text, .rodata and .bss (for the asm objects).

The linker script must be like this:

Code: Select all

OUTPUT_FORMAT(binary)
ENTRY(loader)
SECTIONS 
{ 
    . = 0x100000;
    .text :
    { 
        code = .; 
        *(.text)
		*(.rodata*) 
        text_end = .;
    } 
    .bss :  
    { 
        bss = text_end; 
        *(.bss*) 
        bss_end = .;
    } 
    end = .; 
}
no?

Thanks for your patience.

Re: Problem with strings

Posted: Tue Sep 04, 2012 3:15 pm
by bluemoon
Khronos wrote:Then, I understand that the sections I need are: .text, .rodata and .bss (for the asm objects).
No. Don't you see the zero in the .data section? Zero is still bits of information.
Soon you will also encounter .rdata, and sections for exception handling - to save your time to re-discover the fact, there are documents, wiki and tutorials.

Re: Problem with strings

Posted: Tue Sep 04, 2012 3:32 pm
by Khronos
Hi bluemoon, I have read this topics:

http://wiki.osdev.org/Linker_Scripts
http://wiki.osdev.org/GCC_Cross-Compiler

and others, but I don't find the solution... :oops:

Can you help me with the linker script?

As you can see the input files are elf-86-64 objects, and the entry symbol is a ASM function called loader.

Thanks.

Re: Problem with strings

Posted: Tue Sep 04, 2012 3:50 pm
by bluemoon
Well, you asked.

When working on x86_64 target there are frequently encountered issue by most developer, one of it is relocation.

The problem you described, correct me if I am wrong, is that you can print local strings, but cannot print global strings (or print garbage).
And I look at your workflow, you compiled the kernel as PE-x86_64 and truncate it into raw binary - this potentially messed object offsets.
The reason of function call and local string works perfectly is, I guess, due to the fact that they are default to use relative addressing in 64-bit targets.

My advice is, debug the offset and memory content of the string in bochs, and compare them with objdump.

Re: Problem with strings

Posted: Tue Sep 04, 2012 5:45 pm
by Khronos
Thanks for your advice bluemoon. After hours, suspending bochs and exploring the memory.ram file I have understood, partially, the linker script under MinGW.

Code: Select all

OUTPUT_FORMAT("pe-x86-64")
ENTRY(loader)
SECTIONS 
{
    . = 0x001216;
    .text :
    { 
        text = .; 
        *(.text) 
        text_end = .;
    }
    . = 0x001F76;
    .rodata :  /* rodata contiene las cadenas */
    { 
        rodata = text_end; 
	*(.rdata)
        rodata_end  = .;      
    }
    .data : 
    { 
        data = rodata_end; 
        *(.data) 
        data_end = .;
    } 
    .bss :  
    { 
        bss = data_end; 
        *(.bss) 
        bss_end = .;
    } 
    end = .; 
}
I load the Stage_2 in 0x1000 and the size of the Stage_2 is 0x216 bytes, then, the starting address is 0x001216. Now, I have strings miss-aligned, but these is for tomorrow. Now I gonna sleep.

Thanks.

Re: Problem with strings

Posted: Wed Sep 05, 2012 2:52 pm
by Khronos
I want to thank all for your help, it was very useful. Thanks, a lot of.

After understand all of the points described in my previous answer, in 5 minutos y fix it!

Here is the final linker script for MinGW.

Code: Select all

OUTPUT_FORMAT("pe-x86-64")
ENTRY(loader)
SECTIONS 
{
    .text 0x001216 :
	{
		*(.text)
	}
	.rdata :
	{
		*(.rdata)
	}
	.data :
	{
		*(.data)
	}
	.bss :
	{
		*(.bss)
	}
}
Porting it to Linux, it should be easy :D

Thanks.