Kamek icon indicating copy to clipboard operation
Kamek copied to clipboard

Bad 'undefined symbol' error when using with GNU assembler or GCC

Open MasterofGalaxies opened this issue 3 years ago • 4 comments

When a string or other data is referenced in the same source file where it is defined, Kamek will throw an undefined symbol error:

$ cat returnstring.s
lis 3, string@h
ori 3, 3, string@l
blr
string:
.string "string"
$ powerpc-eabi-as -o returnstring.o returnstring.s
$ Kamek returnstring.o -static=0x80001900 -output-riiv=returnstring.xml
Kamek 2.0 by Ninji/Ash Wolf - https://github.com/Treeki/Kamek

adding returnstring.o as object..
linking version default...

Unhandled Exception:
System.IO.InvalidDataException: undefined symbol 
  at Kamek.Linker.ResolveSymbol (Kamek.Elf elf, System.String name) [0x00110] in <84417af72b3544ac8bfbaa14b20da809>:0 
  at Kamek.Linker.ProcessRelaSection (Kamek.Elf elf, Kamek.Elf+ElfSection relocs, Kamek.Elf+ElfSection section, Kamek.Elf+ElfSection symtab) [0x000cc] in <84417af72b3544ac8bfbaa14b20da809>:0 
  at Kamek.Linker.ProcessRelocations () [0x00149] in <84417af72b3544ac8bfbaa14b20da809>:0 
  at Kamek.Linker.DoLink (System.Collections.Generic.Dictionary`2[TKey,TValue] externalSymbols) [0x0008d] in <84417af72b3544ac8bfbaa14b20da809>:0 
  at Kamek.Linker.LinkStatic (System.UInt32 baseAddress, System.Collections.Generic.Dictionary`2[TKey,TValue] externalSymbols) [0x00019] in <84417af72b3544ac8bfbaa14b20da809>:0 
  at Kamek.Program.Main (System.String[] args) [0x004a5] in <84417af72b3544ac8bfbaa14b20da809>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.InvalidDataException: undefined symbol 
  at Kamek.Linker.ResolveSymbol (Kamek.Elf elf, System.String name) [0x00110] in <84417af72b3544ac8bfbaa14b20da809>:0 
  at Kamek.Linker.ProcessRelaSection (Kamek.Elf elf, Kamek.Elf+ElfSection relocs, Kamek.Elf+ElfSection section, Kamek.Elf+ElfSection symtab) [0x000cc] in <84417af72b3544ac8bfbaa14b20da809>:0 
  at Kamek.Linker.ProcessRelocations () [0x00149] in <84417af72b3544ac8bfbaa14b20da809>:0 
  at Kamek.Linker.DoLink (System.Collections.Generic.Dictionary`2[TKey,TValue] externalSymbols) [0x0008d] in <84417af72b3544ac8bfbaa14b20da809>:0 
  at Kamek.Linker.LinkStatic (System.UInt32 baseAddress, System.Collections.Generic.Dictionary`2[TKey,TValue] externalSymbols) [0x00019] in <84417af72b3544ac8bfbaa14b20da809>:0 
  at Kamek.Program.Main (System.String[] args) [0x004a5] in <84417af72b3544ac8bfbaa14b20da809>:0

This does not occur when using CodeWarrior, or when the string and the code that references it are in separate files and combined with ld:

$ cat returnstring-1.s
lis 3, string@h
ori 3, 3, string@l
blr
$ cat returnstring-2.s
string:
.string "string"
$ powerpc-eabi-as -o returnstring-1.o returnstring-1.s
$ powerpc-eabi-as -o returnstring-2.o returnstring-2.s
$ powerpc-eabi-ld --relocatable -o returnstring.o returnstring-1.o returnstring-2.o
$ Kamek returnstring.o -static=0x80001900 -output-riiv=returnstring.xml
Kamek 2.0 by Ninji/Ash Wolf - https://github.com/Treeki/Kamek

adding returnstring.o as object..
linking version default...
$ cat returnstring.xml
<memory offset='0x80001900' value='3C6080006063190C4E800020737472696E670000' />

Partial fix

Change line 392 of Kamek/Linker.cs to:

Word dest = (String.IsNullOrEmpty(symName) ? _sectionBases[section] : ResolveSymbol(elf, symName).address) + r_addend;

I don't know how to find the section the data is in, so this will only work if the data and the code that references it are in the same section.

MasterofGalaxies avatar May 30 '21 02:05 MasterofGalaxies

The readme states that there's no plan to ever support any compilers other than CodeWarrior, so I doubt that Ninji (Treeki) would consider this behavior a bug.

RoadrunnerWMC avatar Dec 02 '21 05:12 RoadrunnerWMC

I'm aware of that and the reasons Kamek can't be used with C++ and other compilers, but there's no reason C can't be used, and the fix for this is probably really simple for someone familiar with the code.

MasterofGalaxies avatar Dec 02 '21 23:12 MasterofGalaxies

I just talked with Ninji about it on Discord, and he agreed with you that this would probably be reasonable to fix. I don't know how to fix it myself, but I'll leave the issue open.

RoadrunnerWMC avatar Dec 04 '21 01:12 RoadrunnerWMC

That's awesome. This bug can be really annoying. Thanks!

MasterofGalaxies avatar Dec 04 '21 02:12 MasterofGalaxies

Fixed by #36.

RoadrunnerWMC avatar Mar 05 '23 01:03 RoadrunnerWMC