gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Overlaping sections without being full :-S

#155714 - Ruben - Sat May 03, 2008 9:34 am

Code:
c:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.3.0/../../../../arm-eabi/bin/ld.exe: section .ewram [08038f50 -> 0803928b] overlaps section .bss [08038f50 -> 08039967]
collect2: ld returned 1 exit status


I have no idea what could be causing that. All I did was change from devKitARM r21 to r23, and that happened. Any ideas?

I'm using a modified version of Jeff's crt0.s file and his lnkscript. Also, I don't have ANYTHING in EWRAM so it can't be because it's full (it never said that anyway). Any help appreciated.

This is my modified cart file named '0.s' so it ends up in first position when linking (I use a complicated batch script):

Code:
.text
.global _start
.align
.arm

_start:
 b       rom_header_end
 .fill   0x9C,0x01,0x00            @ NINTENDO logo
 .byte   0x00,0x00,0x00,0x00,0x00  @ \
 .byte   0x00,0x00,0x00,0x00,0x00  @  Game title
 .byte   0x00,0x00                 @ /
 .byte   0x00,0x00,0x00,0x00       @ Game code
 .byte   0x30,0x31                 @ Maker code
 .byte   0x96                      @ Reserved
 .byte   0x00                      @ Main unit code
 .byte   0x00                      @ Device type
 .byte   0x00,0x00,0x00,0x00,0x00  @ \ Reserved
 .byte   0x00,0x00                 @ /
 .byte   0x00                      @ Software version
 .byte   0xF0                      @ Complement
 .byte   0x00,0x00                 @ Checksum

.align
.arm

rom_header_end:
 b       start_vector

.global __boot_method, __slave_number

__boot_method:
 .byte   0x00    @ boot type

__slave_number:
 .byte   0x00    @ slave number

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

.global start_vector
.align
.arm

start_vector:
 mov     r0, #0x12
 msr     cpsr, r0
 ldr     sp, =__sp_irq
 mov     r0, #0x1F
 msr     cpsr, r0
 ldr     sp, =__sp_usr
 adr     r0,1f + 1
 bx      r0

.thumb

1:
 ldr     r1, =__intr_vector_buf
 ldr     r0, =IntrRout
 str     r0, [r1]

.LDoEWRAMClear:
 mov     r1, #0x40
 lsl     r1, #0x0C
 lsl     r0, r1, #0x07
 bl      .LClearMem

.LSkipEWRAMClear:
 mov     r0, #0x03
 lsl     r0, #0x18
 ldr     r1, =__sp_usr_offset - 16
 bl      .LClearMem

 ldr     r1, =__data_lma
 ldr     r2, =__data_start
 ldr     r4, =__data_end
 bl      .LCopyMemChk
 
 ldr     r1, =__iwram_lma
 ldr     r2, =__iwram_start
 ldr     r4, =__iwram_end
 bl      .LCopyMemChk
 
 ldr     r2, =__load_stop_iwram0
 ldr     r1, =__load_start_iwram0
 sub     r3, r2, r1
 beq     .LCIW0Skip
 
 ldr     r2, =__iwram_overlay_start
 bl      .LCopyMem

.LCIW0Skip:
 ldr     r1, =__ewram_lma
 ldr     r2, =__ewram_start
 ldr     r4, =__ewram_end
 bl      .LCopyMemChk
 
 ldr     r2, =__load_stop_ewram0
 ldr     r1, =__load_start_ewram0
 sub     r3, r2, r1
 beq     .LCEW0Skip
 
 ldr     r2, =__ewram_overlay_start
 bl      .LCopyMem

.LCEW0Skip:
 mov     r0, #0x00          @ int argc
 mov     r1, #0x00          @ char *argv[]
 ldr     r3, =start_vector  @ r3 = start_vector
 mov     lr, r3             @ lr = r3
 ldr     r3, =main          @ r3 = main
 bx      r3                 @ bx to r3

.LClearMem:
 mov     r2, #0x03          @ r2 = 0x03
 add     r1, r2             @ r1 += r2
 bic     r1, r2             @ \
 beq     .LClearMX          @  if(!((r1 += 0x03) &~ 0x03)) branch to .LClearMX

 mov     r2, #0x40
 lsl     r2, r2, #0x14
 add     r2, r2, #0xD0
 lsr     r1, #0x02
 
 adr     r3, .LClrVal
 str     r3, [r2, #0x4]
 str     r0, [r2, #0x8]
 strh    r1, [r2, #0xC]
 mov     r1, #0x85
 lsl     r1, r1, #0x08
 strh    r1, [r2, #0xE]

.LClearMX:
 bx      lr                 @ bx to lr

.LCopyMemChk:
 sub     r3, r4, r2         @ r3 = r4 - r2

.LCopyMem:
 mov     r0, #0x03          @ r0 = 0x03
 add     r3, r0             @ r3 += r0
 bic     r3, r0             @ \
 beq     .LCIDExit          @  if(r3 == 0) branch to .LCIDExit

 mov     r0, #0x40
 lsl     r0, r0, #0x14
 add     r0, r0, #0xD0
 lsr     r3, #0x02
 
 str     r1, [r0, #0x4]
 str     r2, [r0, #0x8]
 strh    r3, [r0, #0xC]
 mov     r3, #0x84
 lsl     r3, r3, #0x08
 strh    r3, [r0, #0xE]

.LCIDExit:
 bx      lr

.align

.LClrVal:
 .word 0x00000000

.align
.pool

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

.section .iwram, "ax", %progbits
.extern  IntrTable
.global  IntrRout
.align
.arm

IntrRout:
 mov     r2, #0x04000000          @ r2 = REG_BASE
 ldr     r3, [r2,#0x0200]!        @ r2 = REG_IE, r3 = REG_IF<<16 | REG_IE
 mrs     r0, spsr                 @ save spsr to r0
 stmfd   sp!, {r0-r2, lr}         @ push {r0-r2, lr}
 and     r1, r3, r3, lsr #0x10    @ r1 = REG_IE & REG_IF
 ldr     r12, =IntrTable          @ r12 = &IntrTable
 
 ands    r0, r1, #0x01            @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0001) VBL intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x02            @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0002) HBL intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x04            @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0004) VCNT intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x08            @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0008) TM0 intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x10            @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0010) TM1 intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x20            @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0020) TM2 intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x40            @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0040) TM3 intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x80            @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0080) serial intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x100           @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0100) DMA0 intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x200           @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0200) DMA1 intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x400           @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0400) DMA2 intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x800           @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x0800) DMA3 intr
 add     r12,r12, #0x04           @ r12 += 4
 ands    r0, r1, #0x1000          @ \
 bne     .LIntrGo                 @  if(r0 = r1 & 0x1000) keypad intr
 
 ands    r0, r1, #0x2000
 strneb  r0, [r2, #-0x0284]       @ REG_SNDCNT1 = r0

.LsndLoop:
 swine   0x02
 bne     .LsndLoop                @ branch to .LsndLoop

.LIntrGo:
 strh    r0, [r2, #0x02]          @ REG_IF = r0
 ldr     r3, [r2, #-0x0208]       @ r3 = BIOS_IF
 orr     r3, r3, r0               @ r3 |= r0
 str     r3, [r2, #-0x0208]       @ BIOS_IF = r3
 
 mrs     r3, cpsr                 @ save cpsr
 bic     r3, r3, #0xDF            @ r3 &= ~0xDF (clear)
 orr     r3, r3, #0x1F            @ r3 |= 0x1F (IRQ + system mode?)
 msr     cpsr, r3                 @ restore cpsr
 
 ldr     r0, [r12]                @ get the handler's address
 cmp     r0, #0x00                @ \
 beq     .LIntrPostRet            @  if(adr == 0x00) goto .LIntrPostRet
 
 stmfd   sp!, {lr}                @ push {lr}
 adr     lr, .LIntrRet            @ lr = .LIntrRet
 bx      r0                       @ bx to r0

.LIntrRet:
 ldmfd   sp!, {lr}                @ pop {lr}

.LIntrPostRet:
 mrs     r3, cpsr                 @ save cpsr
 bic     r3, r3, #0xDF            @ r3 &= ~0xDF (clear)
 orr     r3, r3, #0x92            @ r3 |= 0x92 (FIQ + IRQ mode?)
 msr     cpsr, r3                 @ store cpsr
 
 ldmfd   sp!, {r0-r2, lr}         @ pop {r0-r2, lr}
 msr     spsr, r0                 @ restore spsr
 bx      lr                       @ bx to lr

.align
.end


And this is the batch script:

Code:
@echo off
setlocal enabledelayedexpansion
path=%path%;C:\devKitPro\devKitARM\bin;

set pre=arm-eabi
set gas=%pre%-as
set gcc=%pre%-gcc
set fix=gbafix
set cpy=%pre%-objcopy
set nm=%pre%-nm
set sflags=-mthumb-interwork
set cflags=-c -mthumb -mthumb-interwork -O2 -ffast-math -ffreestanding -Wall

cd source
for %%f in (*.s) do (
 %gas% %sflags% %%f -o %%f.o
 move %%f.o ..
)
for %%f in (*.c) do (
 %gcc% %cflags% -I../include %%f -o %%f.o
 move %%f.o ..
)
cd..

set obj=
for %%f in (*.o) do set obj=!obj! %%f
%gcc% %obj% -o main.elf -nostartfiles -T misc/lnkscript.ld
%cpy% -O binary main.elf main.gba
%nm% -n -S main.elf > main.txt
%fix% main.gba -p
del *.o *.elf /q
pause

#155717 - eKid - Sat May 03, 2008 9:55 am

Did you try without a custom linkscript? (ie remove the -T options) The crt0 code doesn't look like it would break if it used the normal linkscript.

#155718 - Ruben - Sat May 03, 2008 9:57 am

Yeah, I've tried that. Lot's and lots of undefined references. This is the result:

Code:
0.s.o: In function `start_vector':
(.text+0x18c): undefined reference to `__sp_irq'
0.s.o: In function `start_vector':
(.text+0x190): undefined reference to `__sp_usr'
0.s.o: In function `start_vector':
(.text+0x194): undefined reference to `__intr_vector_buf'
0.s.o: In function `start_vector':
(.text+0x19c): undefined reference to `__sp_usr_offset'
0.s.o: In function `start_vector':
(.text+0x1a0): undefined reference to `__data_lma'
0.s.o: In function `start_vector':
(.text+0x1a8): undefined reference to `__data_end'
0.s.o: In function `start_vector':
(.text+0x1ac): undefined reference to `__iwram_lma'
0.s.o: In function `start_vector':
(.text+0x1b0): undefined reference to `__iwram_start'
0.s.o: In function `start_vector':
(.text+0x1b4): undefined reference to `__iwram_end'
0.s.o: In function `start_vector':
(.text+0x1b8): undefined reference to `__load_stop_iwram0'
0.s.o: In function `start_vector':
(.text+0x1bc): undefined reference to `__load_start_iwram0'
0.s.o: In function `start_vector':
(.text+0x1c0): undefined reference to `__iwram_overlay_start'
0.s.o: In function `start_vector':
(.text+0x1c4): undefined reference to `__ewram_lma'
0.s.o: In function `start_vector':
(.text+0x1c8): undefined reference to `__ewram_start'
0.s.o: In function `start_vector':
(.text+0x1cc): undefined reference to `__ewram_end'
0.s.o: In function `start_vector':
(.text+0x1d0): undefined reference to `__load_stop_ewram0'
0.s.o: In function `start_vector':
(.text+0x1d4): undefined reference to `__load_start_ewram0'
0.s.o: In function `start_vector':
(.text+0x1d8): undefined reference to `__ewram_overlay_start'
collect2: ld returned 1 exit status

#155719 - eKid - Sat May 03, 2008 10:07 am

Hmm, those symbols are defined in the latest gba linkscripts. Maybe try adding -specs=gba_cart.specs or -specs=gba_mb.specs (for multiboot)? :\

#155720 - Ruben - Sat May 03, 2008 10:13 am

Not all of them

Code:
0.s.o: In function `start_vector':
(.text+0x194): undefined reference to `__intr_vector_buf'
0.s.o: In function `start_vector':
(.text+0x19c): undefined reference to `__sp_usr_offset'
0.s.o: In function `start_vector':
(.text+0x1d0): undefined reference to `__load_stop_ewram0'
0.s.o: In function `start_vector':
(.text+0x1d4): undefined reference to `__load_start_ewram0'
0.s.o: In function `start_vector':
(.text+0x1d8): undefined reference to `__ewram_overlay_start'
collect2: ld returned 1 exit status

#155722 - eKid - Sat May 03, 2008 10:20 am

Hmm, looks like __intr_vector_buf is the user irq vector. You can change
Code:
ldr     r1, =__intr_vector_buf

to
Code:
ldr    r1,=0x3007FFC

For __sp_usr_offset..... and the rest... you'll have to find the addresses for them... :D
Why aren't you using the r23 crt0.s? :)

#155725 - Ruben - Sat May 03, 2008 10:37 am

Heh. Couldn't find the address for all of them and when I use the "gba_crt.s" file, it gets stuck clearing something. Technically:

Code:
.Lloop:
 stmia r0!, {r2}
 sub   r1, #0x04
 bne   .Lloop

#155733 - strager - Sat May 03, 2008 4:17 pm

Ruben wrote:
Heh. Couldn't find the address for all of them and when I use the "gba_crt.s" file, it gets stuck clearing something. Technically:

Code:
.Lloop:
 stmia r0!, {r2}
 sub   r1, #0x04
 bne   .Lloop


Looks like that sub should be a subs (not exactly sure):
Code:
.Lloop:
 stmia r0!, {r2}
 subs   r1, #0x04
 bne   .Lloop


Check if the latest CVS/SVN has this issue, or file a bug report.

#155751 - wintermute - Sat May 03, 2008 9:14 pm

Are you using devkitARM release 23?

Unfortunately the vast majority of code using the old custom crt0 & linkscripts will break horribly with devkitARM these days.

It's also best not to use .bat files.

From the look of your path you appear to have installed the toolchain manually. On windows it's really best that you use the devkitPro updater and let it handle all the finer points of installation.

What I would suggest doing is deleting what you have installed so far and running the devkitPro Updater instead. Once that has done it's thing your best course of action is probably to work through the Tonc documentation. There is a template and some libgba examples provided through the installer but I lack the time to update and maintain that library - libtonc is much more complete and better documented.

http://www.coranac.com/tonc/text/
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#155781 - Ruben - Sun May 04, 2008 5:26 am

Quote:
Looks like that sub should be a subs (not exactly sure)

Nah, it's fine using 'sub' since that section runs in THUMB mode.

Quote:
From the look of your path you appear to have installed the toolchain manually.

... ... ... all I did was use the devKitPro updater and it extracted there... :-S

Quote:
...your best course of action is probably to work through the Tonc documentation.

Yeah, TONC's really good but I'm using my one custom library (yeah, I'm a bit odd lol) with in-built support for S3M, XM, MOD, palette fading and all the other stuff I need. For some reason I really like using my own code. Lol.

#155782 - wintermute - Sun May 04, 2008 5:56 am

Ruben wrote:

Quote:
From the look of your path you appear to have installed the toolchain manually.

... ... ... all I did was use the devKitPro updater and it extracted there... :-S


Oh I see, taking advantage of Windows case insensitivity for random capitalisation then ;)

Quote:

Quote:
...your best course of action is probably to work through the Tonc documentation.

Yeah, TONC's really good but I'm using my one custom library (yeah, I'm a bit odd lol) with in-built support for S3M, XM, MOD, palette fading and all the other stuff I need. For some reason I really like using my own code. Lol.


Well, there's nothing particularly wrong with using your own code. Using a custom crt0 & linkscript from an obsolete tool is going to cause you nothing but grief though.

The crt0 and linkscript files in devkitARM were designed to give people more control over code without having to mess with the system files that should never be modified by end users. I really do recommend you use the built in files and makefiles in preference to .bat, this will save you from much heartache in the long run.

For interrupt code feel free to steal what you need from libgba, the dispatcher used there is considerably simpler and actually more flexible than the madness in that original crt0. Cearn also has a pretty good writeup on interrupt handling in Tonc somewhere

The template libgba project distributed with the gba examples is also pretty flexible and will allow you to use make without having to understand the Makefile syntax. You'll also have the advantage of advanced auto dependency generation which is pretty much impossible to provide with bat files. It doesn't force you to use libgba, just remove -lgba from LIBS & $(LIBGBA) from LIBDIRS. Code goes in the source folder, makefile does the rest.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog