ld: section .data [n -> n] overlaps section .text [n -> ]

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
casnix
Member
Member
Posts: 67
Joined: Fri Jan 14, 2011 7:24 pm

ld: section .data [n -> n] overlaps section .text [n -> ]

Post by casnix »

I have been forced (due to the lack of a universal firmware) to write my own virtual firmware to best emulate what my OS needs on EVERY machine (excluding my IBM PS/1). My code (I hope) isn't causing the problem (in the title), I believe it's the linker script.

Anyway, here's the command line and error:

Code: Select all

root@##-systems$ ld link.ld -o vfirmA01.bin setup.o panic.o main.o VFxcns1s.o VFxcns1bs.o VFxcys1k.o
ld: section .data [00000000001001d5 -> 00000000001011d4] overlaps section .text [0000000000100000 -> 0000000000101fff]
ld: vfirmA01.bin: Not enough room for program headers (allocated 2, need 3)
ld: final link failed: Bad value
Linker script:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
*scratches head and burns PC tower* I am stumped. I've searched google, and somehow the most related case was with a PERL script. PERL and LD...Now my brain's exploded.

Anyway, maybe I've missed something very, VERY obvious. Any suggestions?

Thanks,
Casnix
You are a computer.
~ MCS ~
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: ld: section .data [n -> n] overlaps section .text [n ->

Post by Chandra »

casnix wrote:Linker script:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
Probably some problem with sections alignment. Not sure, though.
Replace ALIGN(4096) with ALIGN(8192) and see if it brings any improvement.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: ld: section .data [n -> n] overlaps section .text [n ->

Post by xenos »

It seems like ld is confused because you are using "data" before assigning a value. What happens if you do it like this?

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  data = .;
  .data : AT(phys + (data - code))
  {
    *(.data)
    . = ALIGN(4096);
  }
  bss = .;
  .bss : AT(phys + (bss - code))
  {
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
But since you are using physical addresses which are identical to the virtual addresses, you probably don't need to specify a LMA at all, so the following should work as well:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
casnix
Member
Member
Posts: 67
Joined: Fri Jan 14, 2011 7:24 pm

Re: ld: section .data [n -> n] overlaps section .text [n ->

Post by casnix »

@XenOS:
Your first suggestion removes the overlapping section error, but leaves the program header error.

Your second suggestion throws a "file format not recognized; treating as linker script" which is okay, but "parse error on line 6" is not (that's the .text phys line).
If I fix the parse errors, I get a whole trunk load of "undefined reference" errors, which apparently I can never evade.

@Chandra: Replacing ALIGN(4096) with ALIGN(8192) doesn't bring improvement.

[edit]

Maybe if I did the code declaration the same as data?
[edit again]
Nope, that causes the initial problem.

Aren't the program headers for elf binaries as opposed to flat binaries (which is my goal)?
So now I'm confused as to why ld is trying to produce elf out put instead of AOUT.

In case it matters, I'm using
GNU ld version 2.12.90.0.15
gcc 3.2
And all on SuSE 8.1
You are a computer.
~ MCS ~
User avatar
casnix
Member
Member
Posts: 67
Joined: Fri Jan 14, 2011 7:24 pm

Re: ld: section .data [n -> n] overlaps section .text [n ->

Post by casnix »

Oh, sorry. Got confused between articles about that (the program headers). I'm guessing that I should increase the size of phys?
You are a computer.
~ MCS ~
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: ld: section .data [n -> n] overlaps section .text [n ->

Post by Combuster »

Not necessary a direct cause to the problem at hand but...
GNU ld version 2.12.90.0.15
Ancient. Are you not using a crosscompiler?
gcc 3.2
Ancient, and known buggy.
And all on SuSE 8.1
Erm, upgrade? Maybe? :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
casnix
Member
Member
Posts: 67
Joined: Fri Jan 14, 2011 7:24 pm

Re: ld: section .data [n -> n] overlaps section .text [n ->

Post by casnix »

Yeah, I deffinately need an upgrade, however both my laptop and PC are about 1year younger than me (they're 14 years old), and my laptop has a usb 2.0 port and a floppy drive, so I'm not sure how to do the upgrade :S And my mac doesnt have gnu ld.

Any way the ld -T option tells me about a whole bunch of undefined reference errors, so missing that option may have been the problem all along.

Thank you,
Casnix

[edit] Reading the ld documentation for -T explains the outpilut in elf format. So again,
Thank you
You are a computer.
~ MCS ~
Post Reply