Page 1 of 1

GCC Linker - Alignment

Posted: Mon Dec 28, 2015 3:23 pm
by tsdnz
EDIT: SOLVED!!

Hi, checking the output file I have noticed nop's inserted.

I have tried to change the linker.ld file but I am having trouble with alignment.
I would like .text to start after .StartKernel aligned to 0x10.

Code: Select all

.StartKernel  :
	{
		*(.StartKernel)
	}

	.text ALIGN(0x10) : 
	{
		*(.text)
	}
, but here is the output, with nop's inserted:

Code: Select all

My starting code......
30201d:	ff e0                	jmp    rax

Disassembly of section .text:

0000000000302020 <_ZN6Kernel23tEthernetCardStatistics5Intel6I8259921GetMissedPacketsCountERNS_20tEthernetCardDetailsE-0x3e0>:
  302020:	66 2e 0f 1f 84 00 00 	nop    WORD PTR cs:[rax+rax*1+0x0]
  302027:	00 00 00 
  30202a:	66 2e 0f 1f 84 00 00 	nop    WORD PTR cs:[rax+rax*1+0x0]
  302031:	00 00 00 
  302034:	66 2e 0f 1f 84 00 00 	nop    WORD PTR cs:[rax+rax*1+0x0]
  30203b:	00 00 00 
  30203e:	66 2e 0f 1f 84 00 00 	nop    WORD PTR cs:[rax+rax*1+0x0]
  302045:	00 00 00 
  302048:	66 2e 0f 1f 84 00 00 	nop    WORD PTR cs:[rax+rax*1+0x0]
  30204f:	00 00 00 
  302052:	66 2e 0f 1f 84 00 00 	nop    WORD PTR cs:[rax+rax*1+0x0]
, these nops continue to here:

Code: Select all

3023fe:	66 90                	xchg   ax,ax

0000000000302400 <_ZN6Kernel23tEthernetCardStatistics5Intel6I8259921GetMissedPacketsCountERNS_20tEthernetCardDetailsE>:
  302400:	8b b7 d8 10 00 00    	mov    esi,DWORD PTR [rdi+0x10d8]
, here are the sections:

Code: Select all

6 .StartKernel  0000001f  0000000000302000  0000000000302000  00102000  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  7 .text         0000cec6  0000000000302020  0000000000302020  00102020  2**10
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
.
What I would like is <_ZN6Kernel23tEthernetCardStatistics5Intel6I8259921GetMissedPacketsCountERNS_20tEthernetCardDetailsE>: to start at 0000000000302020, where it is currently 0000000000302400

Any ideas greatly appreciated.

Thanks, Ali

Re: GCC Linker - Alignment

Posted: Mon Dec 28, 2015 4:33 pm
by iansjack
As I read that the linker has inserted .text at the correct place, it's just that .text does not contain the data you think it should. As you have just given a snippet of a linker file, and nothing else, it's really not possible to guess why that is. The first thing I would do in your place would be to inspect the object files that you are linking.

Re: GCC Linker - Alignment

Posted: Mon Dec 28, 2015 4:52 pm
by tsdnz
Hi, here is the entire linker file.

Code: Select all

ENTRY(StartKernel)
SECTIONS
{
	.GDT 0x5000 (NOLOAD) :
	{
		*(.GDT)
	}

	.LocalAPICAddress 0x48A0 (NOLOAD) :
	{
		*(.LocalAPICAddress)
	}

	.DefaultExceptionHandler14 0x48A8 (NOLOAD) :
	{
		*(.DefaultExceptionHandler14)
	}

	.DefaultExceptionHandler0 0x48B0 (NOLOAD) :
	{
		*(.DefaultExceptionHandler0)
	}

	.DefaultExceptionHandler19 0x48B8 (NOLOAD) :
	{
		*(.DefaultExceptionHandler19)
	}

	.UserSpaceAndKernelSharedData 0x160000000000 (NOLOAD) :
	{
		*(.UserSpaceAndKernelSharedData)
	}

	.StartKernel 0x302000 :
	{
		*(.StartKernel)
	}

	.text ALIGN(0x10) : 
	{
		*(.text)
	}

	.CodeAndData ALIGN(0x10) :
	{
		*(.text.*)
		*(.data)
		*(.data.*)
		*(.rodata)
		*(.rodata.*)
	}

	.bss ALIGN(0x10) :
	{
		. = ALIGN(16);
		*(COMMON)
		*(.bss)
		. = ALIGN(16);
	}

	.staticmemory ALIGN(0x10) (NOLOAD) : 
	{
		. = ALIGN(16);
		*(.staticmemory)
	}

	.FileEnd ALIGN(0x10) (NOLOAD) :
	{
		*(.FileEnd)
	}

	/DISCARD/ :
	{
		*(.eh_frame) *(.eh_frame.*) *(.ctors) *(.ctors.*)
	}

}
iansjack wrote:As I read that the linker has inserted .text at the correct place, it's just that .text does not contain the data you think it should. As you have just given a snippet of a linker file, and nothing else, it's really not possible to guess why that is. The first thing I would do in your place would be to inspect the object files that you are linking.

Re: GCC Linker - Alignment

Posted: Mon Dec 28, 2015 4:53 pm
by tsdnz
And the sections:

Code: Select all

DatabaseServer.lkr:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .GDT          00001028  0000000000005000  0000000000005000  000048a0  2**5
                  ALLOC
  1 .LocalAPICAddress 00000008  00000000000048a0  00000000000048a0  000048a0  2**3
                  ALLOC
  2 .DefaultExceptionHandler14 00000008  00000000000048a8  00000000000048a8  000048a0  2**3
                  ALLOC
  3 .DefaultExceptionHandler0 00000008  00000000000048b0  00000000000048b0  000048a0  2**3
                  ALLOC
  4 .DefaultExceptionHandler19 00000008  00000000000048b8  00000000000048b8  000048a0  2**3
                  ALLOC
  5 .UserSpaceAndKernelSharedData 00000070  0000160000000000  0000160000000000  00200000  2**5
                  ALLOC
  6 .StartKernel  0000001f  0000000000302000  0000000000302000  00102000  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  7 .text         0000c667  0000000000302020  0000000000302020  00102020  2**10
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  8 .CodeAndData  0003f830  000000000030e690  000000000030e690  0010e690  2**6
                  CONTENTS, ALLOC, LOAD, CODE
  9 .bss          00b65480  000000000034dec0  000000000034dec0  0014dec0  2**12
                  ALLOC
 10 .staticmemory 00000008  0000000000eb3340  0000000000eb3340  0014dec0  2**3
                  ALLOC
 11 .comment      00000011  0000000000000000  0000000000000000  0014dec0  2**0
                  CONTENTS, READONLY

Re: GCC Linker - Alignment

Posted: Mon Dec 28, 2015 5:03 pm
by tsdnz
Right, cheers. Found it, as usual, after plenty of searching, and a post.

I am using GCC to create assembly which I copy and paste into the Kernel.
The function was told to align 1024.
Not sure why but the first function in .text also followed this.

Here is the offending function:

Code: Select all

// Compile this in release mode and copy the hex output
void AlignTo(1024) asmExceptionHandler()
, where AlignTo is:

Code: Select all

#define AlignTo(To) __attribute__ ((aligned (To)))
iansjack wrote:As I read that the linker has inserted .text at the correct place, it's just that .text does not contain the data you think it should. As you have just given a snippet of a linker file, and nothing else, it's really not possible to guess why that is. The first thing I would do in your place would be to inspect the object files that you are linking.