#171335 - Tommmie - Fri Nov 13, 2009 9:44 pm
Hey everyone,
when I tried to compile Position Independent code I got these undefined references:
Code: |
undefined reference to `__aeabi_ldivmod'
undefined reference to `__errno'
undefined reference to `__aeabi_idiv' |
what's the solution for a proper compilation?
regards,
Tomdev
edit adding -lgcc seems to solve this erros but I still have errors like this:
Code: |
undefined reference to `__ctype_ptr__? |
#171351 - hacker013 - Sun Nov 15, 2009 5:32 pm
remove -nostdlib from the makefile and everything will work again :')
_________________
Website / Blog
Let the nds be with you.
#171354 - Tommmie - Sun Nov 15, 2009 7:02 pm
that let my nds crash, also Wintermute says that fPIC + code out of the standard lib is not stable. So the only solution is to not use code out of the standard lib:(
#171356 - elhobbs - Mon Nov 16, 2009 2:31 am
Why do you want position independent code anyway? Just curious...
#171363 - Tommmie - Mon Nov 16, 2009 7:31 am
For a plugin(I'm working with a pluginloader which works but not with this one)
#171366 - elhobbs - Mon Nov 16, 2009 10:28 am
Tommmie wrote: |
For a plugin(I'm working with a pluginloader which works but not with this one) |
I understand the plugin part, but the rest does not make sense to me.
In any case one approach is to import all of the stdlib funtions into the plugin. You could build a struct with pointers to functions like malloc and free and printf, etc. and pass it to the plugin when it is loaded. Keep on mind that debugging relocated code is really difficult.
#171370 - Tommmie - Mon Nov 16, 2009 4:15 pm
Ok to be clear: I want to compile tremor so that I have a plugin which can decode ogg files. I already have a file called "defines.h" which contains the following:
Code: |
#define malloc x->xmalloc
#define free x->xfree
#define calloc x->xcalloc
#define realloc x->xrealloc
#define fgetc std->fgetc
#define fopen std->fopen
#define fread std->fread
#define fclose std->fclose
#define fseek std->fseek
#define ftell std->ftell
#define fwrite std->fwrite
#define memset std->memset
#undef memcpy
#define memcpy std->memcpy
#define memcmp std->memcmp
#define memchr std->memchr
#define sprintf std->sprintf
#define fprintf std->fprintf
#define vprintf std->vprintf
#define iprintf std->iprintf
#define strcpy std->strcpy
#define strlen std->strlen
#define strchr std->strchr
#define strstr std->strstr
#define strcat std->strcat |
i replace <stdio.h> and <stdlib.h> in the tremor source files with "defines.h". So the files do not use code out of these files. But the problem is, when I've done this I still get this error:
Code: |
d:/dsi/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/li
b\libc.a(lib_a-abort.o): In function `abort':
(.text+0xc): undefined reference to `_exit'
d:/dsi/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/li
b\libc.a(lib_a-signal.o): In function `_raise_r':
(.text+0x7c): undefined reference to `_getpid_r'
d:/dsi/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/li
b\libc.a(lib_a-signal.o): In function `_raise_r':
(.text+0x8c): undefined reference to `_kill_r'
d:/dsi/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/li
b\libc.a(lib_a-mallocr.o): In function `_malloc_r':
(.text+0x400): undefined reference to `_sbrk_r'
d:/dsi/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/li
b\libc.a(lib_a-mallocr.o): In function `_malloc_r':
(.text+0x47c): undefined reference to `_sbrk_r'
d:/dsi/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/li
b\libc.a(lib_a-freer.o): In function `_malloc_trim_r':
(.text+0x48): undefined reference to `_sbrk_r'
d:/dsi/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/li
b\libc.a(lib_a-freer.o): In function `_malloc_trim_r':
(.text+0x78): undefined reference to `_sbrk_r'
d:/dsi/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/li
b\libc.a(lib_a-freer.o): In function `_malloc_trim_r':
(.text+0xbc): undefined reference to `_sbrk_r'
collect2: ld returned 1 exit status |
it's complaining about some malloc thingies, which I've never heard of, but how can that be if the source files do not use the malloc out of stdio.h but just a pointer?
btw, you're the one who made dsli, how did you manage to include division when you have -nostdlib in your makefile ? I've to include the standard lib -lgcc to have support for it. And why do you have selected to tune for the arm7 in the dsli plugin makefile and not the arm9? does the code run from the arm7? To be precise I'm talking about this part:
Code: |
CFLAGS := -g -Wall -O2\
-mcpu=arm7tdmi -mtune=arm7tdmi -fomit-frame-pointer\
-ffast-math \
$(ARCH)
CFLAGS += $(INCLUDE) -DARM9 -fPIC // define arm9 but tune for the arm7? |
Tommmie
#171372 - elhobbs - Mon Nov 16, 2009 5:29 pm
I would image that tremor is using malloc internally to dynamically allocate memory. you will need to replace all of the malloc and free calls using the import method that I described earlier. use a search engine if you do not know what malloc is. malloc_r is used internally for calls to malloc.
like I said before __ctype_ptr__ is defined in libg - you will need to comeup with a solution for that on your own - like removing the depenancy from the source code.
how did I include division? I included libgcc - ignorance is bliss. it appeared to work fine for what I was using it for. A better approach would probably be to include code for the missing functions. the ds does have some sort of coprocessor for division. though it would need to be used carefully - not in interrupts, etc.
why did I build for the arm7? because that is what dldi uses and I knew it worked. I think I made it pretty obvious that I stole nearly all of the code from dldi.
#171374 - Tommmie - Mon Nov 16, 2009 6:41 pm
I know what malloc is even what alloca is so that's not the problem is(I think the problem is my bad English). So why should I replace the malloc calls if I DON'T include <stdio.h> & <stdlib.h> but "defines.h> where malloc is declare d as a pointer to the malloc function in the pluginloader?
and maybe I should use the libnds functions for the division. also I really have no idea what to do with __ctype_ptr__. Furtthermore this is your makefile for the plugin:
Code: |
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM)
endif
include $(DEVKITARM)/ds_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
# SPECS is the directory containing the important build and link files
#---------------------------------------------------------------------------------
export TARGET := $(shell basename $(CURDIR))
BUILD := build
SOURCES := source
DATA := data
INCLUDES := include
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -mthumb-interwork
CFLAGS := -g -Wall -O2\
-mcpu=arm7tdmi -mtune=arm7tdmi -fomit-frame-pointer\
-ffast-math \
$(ARCH)
CFLAGS += $(INCLUDE) -DARM9 -fPIC
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
ASFLAGS := -g $(ARCH)
LDFLAGS = -nostartfiles -nostdlib -T $(CURDIR)/../dsli.ld -g $(ARCH) -Wl,-Map,$(TARGET).map
LIBS := -lnds9
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(LIBNDS)
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT := $(CURDIR)/$(TARGET)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES := $(addsuffix .o,$(BINFILES)) \
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
.PHONY: $(BUILD) clean all
#---------------------------------------------------------------------------------
all: $(BUILD)
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).dsli $(TARGET).elf
#---------------------------------------------------------------------------------
else
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).dsli : $(OUTPUT).elf
$(OUTPUT).elf : $(OFILES)
#---------------------------------------------------------------------------------
%.dsli: %.elf
@$(OBJCOPY) -O binary $< $@
@echo built ... $(notdir $@)
-include $(DEPENDS)
#---------------------------------------------------------------------------------------
endif
#--------------------------------------------------------------------------------------- |
I don't see the include of lgcc, so what trick have you done?
about _malloc_r(that was something I didn't know of, the malloc thingies:)) you say that it's used for internally calls to malloc, can you give me example of a function doing that?
Tommmie
#171375 - elhobbs - Mon Nov 16, 2009 7:21 pm
the files you have are from an older version which actually links to libnds. the files you have are the proof of concept code that I dumped and ran away from. and there are actual problems with it. the main problem being that it does not allocate space for the bss section. so, it dies pretty quickly for all but the most trivial examples.
It does not matter which files you include, at some point you are referencing malloc so the linker is looking for it. are you using -nostartfiles in your makefile? you should be, it prevents the need for main and the code that calls it. malloc and sbrk (which is used to get memory for malloc) may be referenced by this.
#171376 - Tommmie - Mon Nov 16, 2009 7:30 pm
yes I'm using -nostartfiles in my makefile. Shall I upload the code I'm trying to compile on my site?
#171379 - elhobbs - Mon Nov 16, 2009 10:13 pm
Tommmie wrote: |
yes I'm using -nostartfiles in my makefile. Shall I upload the code I'm trying to compile on my site? |
sure, I will take a look - not that I can promise a solution ;)
#171380 - Dwedit - Mon Nov 16, 2009 10:28 pm
Compile to ASM (use the -S switch) and see where those calls actually occur. GCC often sticks in library function calls during compilation.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."
#171381 - Tommmie - Tue Nov 17, 2009 7:30 am
Thanks for the tip:) will try it in the afternoon